Wednesday, July 04, 2012

Life cycle for Stateless and Stateful beans

Life cycle for stateless session beans

  • When the application server is not started or just starting there are no instances of beans.
  • EJB Container looks in to the pooling policy and instantiates new beans accordingly. For instance if the pooling policy says to create a pool of 10 object it will createaccordingly.
  • Container instantiates the bean using “Class.newInstance(“MyBean.class”).
  • After the object is created EJB container calls the “setSessionContext()” to set the context object so that bean can make a call back to the container if it ever needs to.
  • Container then calls “ejbCreate()” method and this initializes you bean. Just a note to make when a client makes a new object or destroys an EJB object. It’s not necessary that EJB container will do it instantly. It will decide that whether it should really destroy this object or move it pool or should not create an object and take one from the pool.
  • Finally container makes calls to the business methods of the bean. All the business methods are called depending on the client makes calls. All the method calls are treated in the same way as session beans are in the same state after the method call. When a request comes in from client1 the container can take bean1 from the pool process the request and bring bean1 to a previous state. Later when again client1 calls container can take bean2 process the request and bring bean2 to the previous state. So the object treats every request as a new request and does not care to save the previous data of the request.
  • Finally when the container is about the remove the object it calls the “ejbRemove()” of the bean.

Life cycle of stateful session beans 
Life cycle of the stateful session beans is same as stateless only leaving the below points:-
  • First as its stateful there is no pool of objects.
  • Second with every client call there are “ejbPassivate” and “ejbActivate” methods to save and retain the conversational state.

No comments: