http:://in.geocities.com/rsramsam/linker.htm

 

        Invoking an EntityBean

        by using a SessionBean

         ( Façade Pattern)

 

R.S.RAMASWAMY

(rsramsam@yahoo.co.in)

 

 

( PUBLISHED IN DEVELOPERIQ.....DECEMBER-2003)

  ( in continuation of Entity beans..BMP).

 

 

======================================================================================

    So far, in all our examples, the client program for invoking the EJBean has either been a console mode program or a servlet.Sometimes, it will be a better method if we make a SessionBean invoke other beans in the EJB server. This will reduce network traffic to a great extent.

 

    In this case, the end user sends data to the servlet, the servlet invokes a session bean and the session bean  invokes the session and entity beans in the EJB server. As both the invoking and invoked EJB's are available within the EJB server itself, it speeds up things and reduces repeated calls over the network, thereby resulting in quicker response.

 

      In our example, we will however use a console-mode client

to invoke a stateless  sessionbean which in-turn invokes a CMP.

 

     Let us call our  stateless sessionbean in this demo 'linker',as we use it to link with the CMP bean already in the EJB server.

 

     Please review the CMPBean code already dealt with. In that demo, the client was a console mode program.This console mode client located the CMPBean by the following code fragment:

   

    ----------------------------------------------------

 

    Properties    props = new Properties();

    props.put(Context.INITIAL_CONTEXT_FACTORY,

               "weblogic.jndi.WLInitialContextFactory");

    props.put(Context.PROVIDER_URL,

                "t3://127.0.0.1:7001" );

 

    Context      context = new InitialContext(props);

 

    customerHome   home=    (customerHome) context.lookup

                                        ("customerJndi");

 

     -------------------------------------------------

   We will be using the same code to invoke the customerHome

 by using  'linker' session bean. This can be seen from the code

 given for 'linkerBean', below.

 

   Apart from this , there is no difference. However, we will have to modify the ejb-jar.xml file as well as weblogic-ejb-jar.xml files,suitably as shown, to reflect the fact that there is reference to ejb   from another ejb.

 

-------------------------------------------------------------

 

 

 //**************    linkerRemote.java   *********************        

import javax.ejb.*;

import java.rmi.*;

 

public interface linkerRemote extends EJBObject

 {

  public     String      getdata(String s) throws RemoteException;

 }

-------------------------------------------------------------

 

 

//  *****************   linkerHome.java  ******************

 

import javax.ejb.*;

import java.rmi.*;

 

public interface linkerHome extends EJBHome

{

  public linkerRemote create() throws RemoteException, CreateException;                       

}

---------------------------------------------------------

 

 

//***************     linkerBean.java   **********************

 

import javax.ejb.*;

import javax.naming.*;

import customerRemote;

import customerHome;

import java.util.*;

 

 

public class linkerBean implements SessionBean

{  

 

  public void ejbCreate() { }

  public void ejbRemove() { }

 

  public void ejbActivate() { }

  public void ejbPassivate() { }

 

  public void setSessionContext(SessionContext context) { }

 

  //=============================================

 

  public String   getdata(String s)

  {

 

    String    s3="";

 

    try

    {

    Properties   props = new Properties();

 

    props.put(Context.INITIAL_CONTEXT_FACTORY,

               "weblogic.jndi.WLInitialContextFactory");         

 

 

    props.put(Context.PROVIDER_URL,

                "t3://127.0.0.1:7001");

 

    Context    context = new InitialContext(props);

 

    customerHome   home =

           (customerHome)  context.lookup("customerJndi");

 

    customerRemote   remote=home.findByPrimaryKey(s);

 

    String  s1 = remote.getName();

    String  s2 = remote.getPlace();

 

      s3=s1+"......."+s2;     

 

 

    }catch(Exception  e1){System.out.println(""+e1);}

 

    return   s3;    

  } 

}

 

=======================================================================

We now create the required xml files. Pay particular attention to

the ejb-ref   section in ejb-jar.xml   file! This is how we link with    the CMPBean.

 

Similarly, note the section 'referencedescriptor ' in weblogic-ejb-jar.xml  file.

 

 

//    ******************    ejb-jar.xml  ***************

  -------------------------------------------------

<?xml version="1.0" ?>

 

<!DOCTYPE ejb-jar PUBLIC

"-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"

"http://java.sun.com/dtd/ejb-jar_1_1.dtd">

 

<ejb-jar>

     <enterprise-beans>

       <session>

          <ejb-name>linkerBean</ejb-name>

          <home>linkerHome</home>

          <remote>linkerRemote</remote>

          <ejb-class>linkerBean</ejb-class>

          <session-type>Stateless</session-type>

          <transaction-type>Container</transaction-type>

 

 

       <ejb-ref>

          <ejb-ref-name>customerJndi</ejb-ref-name>

          <ejb-ref-type>Entity</ejb-ref-type>

          <home>customerHome</home>

          <remote>customerRemote</remote>

          <ejb-link></ejb-link>

       </ejb-ref>

 

 

       </session>

     </enterprise-beans>                          

</ejb-jar>

 

 

======================================================================

 

 

                   weblogic-ejb-jar.xml

                   ===================

 

 

 

<?xml version="1.0"  ?>

 

<!DOCTYPE weblogic-ejb-jar PUBLIC

'-//BEA Systems, Inc.//DTD WebLogic 5.1.0 EJB//EN'

'http://www.bea.com.servers/wls510/dtd/weblogic-ejb-jar.dtd'>

 

<weblogic-ejb-jar>                                           

 <weblogic-enterprise-bean>

 <ejb-name>linkerBean</ejb-name>

 

 <reference-descriptor>

 

   <ejb-reference-description>

       <ejb-ref-name>customerJndi</ejb-ref-name>

       <jndi-name>customerJndi</jndi-name>

   </ejb-reference-description>

 

 </reference-descriptor>

 

 

 <jndi-name>linkerJndi</jndi-name>                           

 </weblogic-enterprise-bean> 

</weblogic-ejb-jar>

--------------------------------------------------------------------

 

Finally, we have to modify the weblogic.properties file as follows:

 

 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# WEBLOGIC EJB DEMO PROPERTIES

# --------------------------------------------------

 

weblogic.ejb.deploy=\ 

     c:/weblogic/beans/cmp/customer1.jar,\

     c:/weblogic/beans/linker/linker1.jar

-------------------------------------------------------

 WEBLOGIC DEMO CONNECTION POOL PROPERTIES

# --------------------------------------------------

weblogic.jdbc.connectionPool.demoPool=\

#       url=jdbc:cloudscape:demo,\

                            url=jdbc:odbc:customer,\

#       driver=COM.cloudscape.core.JDBCDriver,\

                       driver=sun.jdbc.odbc.JdbcOdbcDriver,\

       initialCapacity=3,\

       maxCapacity=5,\

       capacityIncrement=3,\

       props=user=none;password=none;server=none

#

# Add a TXDataSource for the connection pool:

#  weblogic.jdbc.TXDataSource.weblogic.jdbc.jts.demoPool=demoPool

         weblogic.jdbc.TXDataSource.customer=demoPool

#

# Add an ACL for the connection pool:

weblogic.allow.reserve.weblogic.jdbc.connectionPool.demoPool=everyone

 

-------------------------------------------------------------------

 

The procedure for compiling the source files, creating the jar file and  deploying the jar file in weblogic server is the same as before.

 

-------------------------------------------

After successfully deploying the two EJ-beans, ( check up that you  get the message that customerBean and linkerBean have been deployed).  we create the following client file .

 

//   ***************    linkerClient.java  *********************

  

import javax.ejb.*;

import java.rmi.*;

import javax.rmi.*;

import javax.naming.*;

import java.util.*;

 

import linkerRemote;

import linkerHome;

 

import java.io.*;

 

public class linkerClient

{

 

 

 public static void main(String args[])

 {

   try

    {

       Properties   props = new Properties();

 

   props.put(Context.INITIAL_CONTEXT_FACTORY,

                    "weblogic.jndi.WLInitialContextFactory");

   props.put(Context.PROVIDER_URL,

               "t3://127.0.0.1:7001");  

 

 

    System.out.println("please wait: contacting remote server!");

 

     Context    ctx = new InitialContext(props);

     

     linkerHome      home = (linkerHome)ctx.lookup("linkerJndi");

    

     

     linkerRemote     remote = home.create();

     

 

      String   a = remote.getdata(args[0]);

 

      System.out.println(a);

    

     } catch(Exception e1) { System.out.println(" " + e1);    }

 

   }  // main

 

}// class

 

//******************************************************************

 

 We compile this file and then run the program.

 

   >java linkerClient  543

 

( We have already created a record "543","SAM", "MADRAS")

 

So, we get the response as :

SAM......MADRAS

 

 

 

That completes the lesson on invoking an EntityBean using a SessionBean.

 

                          HOME PAGE

 

 

 

 

1