Flylib.com

Books Software

 
 
 

Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days - page 151

Quiz

1:

List the three different types of Enterprise JavaBeans available.

2:

What is a session bean?

3:

What is the difference between stateful and stateless session bean?

4:

List the classes and interfaces required to build a session bean.

Exercises

1:

Set up and execute the Customer EJB client of the restaurant application on a separate machine. What would be the changes required to access the WaiterBean located in the WebLogic Server on a different machine? How would you generate the client-side stubs?

2:

Write the Waiter bean of the restaurant application to be accessed locally. What would be the changes involved in this case?

3:

What impact does writing a Java servlet as an EJB client have on the design of the application?

Day 11. Creating Stateless Session Beans

Yesterday, you gained an understanding of Enterprise JavaBeans (EJBs) and studied the first type of EJB, the stateful session bean. Yesterday 's lesson also covered concepts such as components of a stateful session bean, the stateful session bean life cycle, and steps involved in building and deploying stateful session beans. You wrote a sample application that modeled the functions and activities in a restaurant with the customer, waiter, and chef as the primary actors. This example made you aware of how real-life entities can be easily modeled into EJBs and helped you identify the EJB to which these entities are mapped based on their activities (whether state maintenance is required or not required, and so on).

Today you will be looking at the other type of session EJB, the stateless session bean. You will be covering concepts such as the life cycle of stateless session beans, their components, and so on. Also, you will be converting the chef classes of the restaurant application from yesterday into a stateless session bean. In addition to this, you will be covering concepts about accessing a stateless session bean from a CORBA or RMI client. The final part of the day will be devoted to building the transactional parts of the MVC Airline Ticket Booking System application as stateful and stateless session beans.

Life Cycle of a Stateless Session EJB

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.

graphics/11fig01.gif

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.