bean to bean communication


EJB design: bean to bean communication

  1. bean to bean communication (3 messages)

        I want to know how to communicate one bean to the other.please let me know.

    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
  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
  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.

    (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.


    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(
             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();
         InitialContext ctx = new InitialContext(env);
         Object objref =
         SequenceHome seqHome =
                 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,


    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:


    The code now becomes much simpler, like this:

         private Sequence getSequenceBean() {
              throws CreateException, NamingException,
             Context jndiCtx = new InitialContext();
             Object objref =

             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'.