bean to bean communication

Discussions

EJB design: bean to bean communication

  1. bean to bean communication (3 messages)

    Hello!
        I want to know how to communicate one bean to the other.please let me know.
              Bye
                vijayagopal.

    Threaded Messages (3)

  2. bean to bean communication[ Go to top ]

     can u elaborate ur requirement

    there are 2 type of communication
    session to entity
    session to session

    -------------------
    feel free to mail me at
    p dot gokulnath at chennai dot transys dot net
    ----------------------
    gokulnath.p
  3. bean to bean communication[ Go to top ]

    Beans communicate to each other the same way that clients do so. They do an JNDI lookup for the home interface and call a create operation on it.

    Dave Wolf
    Internet Applications Division
    Sybase
  4. bean to bean communication[ Go to top ]

    Hi Vijaygopal,
       Pls read the following artincle. This is somewhat weblogic specific. But i think similar thing will be there in other servers too.

    thanks...hasan
    (ahamed at aztec dot soft dot net)
    ------------------

    Referencing one bean from another bean
    ======================================

    The following sections discuss two best practices in EJB, which everyone should be aware of and use.

    1. EJB environment:
    ==================

    Every EJB has its own environment space. The EJB environment allows defining configurable variables without hard coding the value for those variables in the bean code. For example, the oracle sequence name is a variable. The code should not use hard-coded value for the sequence name like:
         long id = sequence.nextValue
                           ("WORKFLOW_SEQ"); //AVOID

    Instead, define an environment variable for the sequence name in ejb-jar.xml file.

         <env-entry>
           <env-entry-name>sequenceName</env-entry-name>
           <env-entry-type>java.lang.String</env-entry-type>
           <env-entry-value>WORKFLOW_SEQ</env-entry-value>
         </env-entry>

    Once this entry is available, the bean should include the following code to extract the value for a given env variable name :

         private String getSequenceName()
                     throws NamingException {
             Context envCtx = new InitialContext();
             String seqName =
               (String) envCtx.lookup(
                          "java:comp/env/sequenceName");
             return seqName;
         }


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

    2. EJB References:
    =================

    If the EJB refers to another, the JNDI of the EJB name should *not* be hard-coded within the bean code. For example, every entity bean of ours refers to com.util.ejb.sequence.Sequence bean. Currently most of our code will look like this:

         Hashtable env = new Hashtable();
         env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "weblogic.jndi.T3InitialContextFactory");
         InitialContext ctx = new InitialContext(env);
         Object objref =
             ctx.lookup("com.util.ejb.sequence");
         SequenceHome seqHome =
             (SequenceHome)PortableRemoteObject.narrow(
                 objref, SequenceHome.class);
         Sequence seq = seqHome.create();
         ...


    This should be avoided. EJB provides a feature by which you can refer to other EJBs without hard-coding the JNDI name. This is very similar to env entries, but the XML element names are different. The ejb-jar.xml file should include the following,

         <ejb-ref>
           <ejb-ref-name>ejb/Sequence</ejb-ref-name>
           <ejb-ref-type>Entity</ejb-ref-type>
           <home>com.util.ejb.sequence.SequenceHome</home>
           <remote>com.util.ejb.sequence.Sequence</remote>
         </ejb-ref>

    The EJB spec recommends that all reference names start with &#8220;ejb/&#8221; so that a separate JNDI context is allocated. If you notice, this still does not capture the target bean's JNDI name.

    Now, open weblogic-ejb-jar.xml file and include the following element:

         <reference-descriptor>
           <ejb-reference-description>
              <ejb-ref-name>ejb/Sequence</ejb-ref-name>
              <jndi-name>
                   com.util.ejb.sequence.Sequence
              </jndi-name>
           </ejb-reference-description>
         </reference-descriptor>


    The code now becomes much simpler, like this:

         private Sequence getSequenceBean() {
              throws CreateException, NamingException,
                                     RemoteException
             Context jndiCtx = new InitialContext();
             Object objref =
    jndiCtx.lookup("java:comp/env/ejb/Sequence");

             SequenceHome seqHome =
                 (SequenceHome) PortableRemoteObject.narrow(
                      objref, SequenceHome.class);

             return seqHome.create();
         }

    What are the advantages of using EJB environment and EJB references?

    * We are completely eliminating hard-coded values from within our programs.
    * The EJB vendors provide tools by which Application Assember and Deployer (these are roles as defined by EJB spec) can change these values according to the environment available in production setup.
    * They are called 'best practices'.

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