Discussions

EJB design: how to call a bean instance from another bean

  1. can anyone plz tell me how to call a bean instance from another bean instance
  2. All the business logic of a bean should be invoked through its component interface(Remote/Local) only, even if your beans are in the same container and same ear file.

    You should JNDI lookup another bean and execute method calls. And also if you want to pass your bean instance reference to another bean, you should pass the reference of EJBObject or EJBLocalObject, like ctx.getEJBObject() or ctx.getEJBLocalObject().
  3. Local Lookup[ Go to top ]

    Hi,
    I've read the comment and believe that there should be some more explanation on how EJB communicate with each other.

    Remember that your beans are running inside the container and that you can access them using the Home and Remote interfaces. This is in fact using RMI (Remote Method Invocation)... communication between the web-container and the EJB-container for example.
    This means that when accessing an EJB from a POJO, use the Home interface to retrieve an instance of that EJB.

    If you are communicating between two EJB's however (running in the same EJB container), you should use the LocalHome object to retrieve an instandce of that EJB. At that moment no RMI is used but just plain old class loading. This will result in quite an increase of speed.

    Hope this helps a bit ??

    Rgds,
    Les.
  4. can anyone plz tell me how to call a bean instance from another bean instance
  5. why dont you use <ejb-ref> for this
  6. why dont you use <ejb-ref> for this
    <ejb-ref> is a logical name for an existing ejb. To avoid hardcoding JNDI names inside your bean code, you will use <ejb-ref>. In ejb-ref also, the container will look up the bean instance.
  7. I think this link might help you. http://java.sun.com/blueprints/qanda/ejb_tier/index.html#references