Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
The life cycle of a stateless session bean varies from the life cycle of a stateful session bean. Figure 11.1 shows that the life cycle of a stateless session bean is fairly simple. There are three stages in the life cycle of a stateless session bean: creation, process operations, and termination. Figure 11.1. Life cycle of a stateless session bean.Creation
Similar to a stateful session bean, the stateless session bean is created by the EJB container as a new instance of the stateless session bean after the client application invokes the create() method on the home interface of the stateless session bean. As a part of the creation process, the setSessionContext() method of the stateless session bean is invoked by the EJB container. The final step in the creation of a stateless session bean is the EJB container's invoking the ejbCreate() callback method on the bean instance. Because no state information about the client application is maintained by the stateless session bean, the ejbCreate() method is not overloaded and does not need to have any state information passed as parameters to the ejbCreate() method. This is a significant difference from a stateful session bean, in which the state information of the client application was passed as parameters to the ejbCreate() method and stored in the instance of the stateful session bean. After invoking the ejbCreate() method, the reference of the remote object of the stateless session bean is returned to the client application. The client application uses this remote object reference to invoke business methods published in the remote interface. The stateless session bean is now active and ready to process client application requests. Normally, to improve performance, the EJB container creates an instance pool of stateless session beans to avoid the overhead of creating a stateless session bean on every client request. Process Operations
Stateless session beans are transactional components in an MVC architecture. Another major difference with stateless beans in this stage of the life cycle is that no activation or passivation of the stateless session bean can occur. Because no state is maintained, a stateless session bean is never associated with any client application. Hence, different instances of a stateless session bean can be used to service different requests from the same client application, avoiding the need for activating or passivating the stateless session bean. A stateless session bean remains active in the EJB container until it processes the client application's request. At the end of processing the request, the EJB container returns the stateless session bean to the stateless session bean instance pool. Termination
A client application can terminate a stateless session bean by invoking the remove() method on the home interface of the stateless session bean. When the remove() method is invoked, the EJB container may either destroy the instance of the stateless session bean or return the instance to the stateless session bean pool. An EJB container can return the instance to the bean pool because no state information is communicated between the stateless session bean and the client application. This enables the EJB container to minimize the overhead associated with destroying a stateless session bean instance. The steps involved in developing stateless session beans are exactly the same as those described for stateful session beans. Hence, today's lesson will not go over the flowchart for developing a stateless session bean. The only differences between the two are that a stateless session bean contains only one ejbCreate() method without parameters in the bean implementation class and in the deployment descriptor file ejb-jar.xml and that the bean is registered as a stateless session bean to distinguish it from a stateful session bean. Now that you understand the life cycle of a stateless session bean and the steps involved in building a stateless session bean, you are ready to study the concepts of stateless session beans in detail. |