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 Certification (SCBCD/OCPJBCD) and the fly likes Question regarding a session bean's life cycle ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question regarding a session bean Watch "Question regarding a session bean New topic
Author

Question regarding a session bean's life cycle ...

Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

I have a simple stateless session bean named MySessionBean.

This bean has a @PostConstruct callback defined.

I have written a client for this bean using the @EJB DI annotation.

The @PostConstruct annotated method is not being invoked until i use the bean in the client, as follows, mySessionBean.someMethod();

My question : Will the container create the instance and put it in the pool as soon as i use the bean and invoke methods on it ? Why not in application server startup time ?

Please advice,

Best regards,


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

It depends on the EJB Container's implementation. Section 4.5.1 of the ejb core specification states:


A stateless session bean instance’s life starts when the container invokes the newInstance method on the session bean class to create a new session bean instance. Next, the container injections the bean’s SessionContext, if applicable, and performs any other dependency injection as specified by metadata annotations on the bean class or by the deployment descriptor. The container then calls the PostConstruct lifecycle callback interceptor methods for the bean, if any. The container can perform the instance creation at any time—there is no direct relationship to a client’s invocation of a business method or the create method.


At any time... which may be at startup (e.g. creation of the pool, if there is one), when needed (first method invocation on that type of stateless bean) or at any other time. The important thing to remember is that you should not assume that it gets called immediately before the business method invocation.
[ September 21, 2008: Message edited by: Sergio Tridente ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Thanks
 
GeeCON Prague 2014
 
subject: Question regarding a session bean's life cycle ...