File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes where to create helper objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "where to create helper objects" Watch "where to create helper objects" New topic
Author

where to create helper objects

Mahesh Asrani
Greenhorn

Joined: Mar 03, 2005
Posts: 15
Hi All

I have to access some helper objects in my enterprise beans.
I would like to know where it would be most appropriate to create instances of these helper classes inside the EJB i.e. exactly in which method.

Should i create all the instances in the business method or should it go in the ejbcreate() method or would it be in other ejb methods like setSessionContext or ejbactivate.

I am using SLSB which invokes these plain Java classes.

Bye
Mahesh
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Mahes,


Should i create all the instances in the business method or should it go in the ejbcreate() method or would it be in other ejb methods like setSessionContext or ejbactivate.

I am using SLSB which invokes these plain Java classes.

Let�s see now� A SLSB has only two states: Does Not Exist state and Method-Ready Pool state. Usually the container creates a number of SLSB instances and moves them to the pool (this could be configured through deployment descriptors). When the container moves the SLSB to the pool, three thinks happen (in this order):
  • The container invokes Class.newInstance() on the SLSB class.
  • The container invokes the setSessionContext() method.
  • The container invokes the ejbCreate() method.


  • As you can see, SLSB are never activated/passivated, since they have no state. Hence the two corresponding ejb methods are ignored in the case of SLSBs. The next question to ask is whether the instance variables are either local to the method, or global (being used by more than one method). If the first one is true then you might consider creating the instances locally. Otherwise you have to choose between initializing them either inside of setSessionContext or ejbCreate. I would take the ejbCreate, because of two reasons:
  • It makes a lot of sense from a design standpoint; it looks like the internal state is initialized when the bean is instantiated.
  • The bean�s context is initialized and you can access all information available in the SessionContext object.


  • Regards.


    I think, therefore I exist -- Rene Descartes
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: where to create helper objects
     
    Similar Threads
    which design pattern is suitable for my problem
    business methods & EJBHome interface
    REWARD : pass a2a parameter, involves using getCodeBase()
    Question about helper class of session bean
    helper functions good design?