Looking Under the Hood of the Stateful Session Bean

Figure 6.1 shows the interactions between the client, the EJB container, and the stateful session bean.

Figure 6.1. Under the hood of a stateful session bean.


The following steps describe the sequence of interactions in detail:

1.
At startup, the EJB container registers enterprise beans with the Java Naming and Directory Interface (JNDI) service.

2.
The client looks up the home interface of the installed enterprise bean via JNDI. For example, the remote home interface for the enrollment cart session bean can be located using the following code segment:

Context initialContext = new InitialContext();
Object obj = initialContext.lookup("day06/EnrollmentCartHome");
EnrollmentCartHome eCartHome = (EnrollmentCartHome)
javax.rmi.PortableRemoteObject.narrow(obj, EnrollmentCartHome.class);

3.
The client uses the remote home interface to create an enrollment cart session object. For example,

EnrollmentCart eCart =  (EnrollmentCart) eCartHome.create();

The container creates a session bean instance on behalf of the client. The creation process involves setting the context and calling the appropriate ejbCreate<method> method. For example, the container calls the setSessionContext and the ejbCreate methods of the session bean instance.

4.
The client calls business methods on the remote object For example, the client adds courses to the enrollment cart as follows:

String[] courseIds = { "CS101", "CS102", "CS103"};
enrollmentCart.addCourses(courseIds);

The container calls the appropriate business method on the session bean. For example, it calls session bean's method:

addCourses(String[] courseIds).

5.
The client calls the remove method of the remote object. For example, the client removes the enrollment cart object as follows:

enrollmentCart.remove();

The container calls the ejbRemove method on the session bean instance.

Note

A client never directly accesses instances of the session bean's class.


During the life cycle of the stateful session bean, the container can passivate and activate the session bean instance. This usually occurs when the number of instances reaches a certain limit specified by the developer in the deployment descriptor. During this process, the container calls the session bean's ejbPassivate and ejbActivate methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.73.127