Specifying a Stateful Session Bean

Specifying a stateful Session bean is similar to specifying a stateless Session bean. The remote interface defines access to the bean by remote clients, and every method of the remote interface must throw an RemoteException. The primary difference (from a specification viewpoint) is that there can be multiple create() methods in the home interface.

You will recall that a stateless Session bean allows only for a single create() method in the home interface, and this corresponds to the ejbCreate() method of the bean itself. For a stateful Session bean, the create() method can be overloaded, so that the stateful bean can be given some initial state. From the client's viewpoint, this is somewhat analogous to invoking a constructor on the bean.

For example, the Advertise bean in the case study is stateful. It represents an advertiser of job positions. The home interface for this bean is as follows:

package agency;

import java.rmi.*;
import javax.ejb.*;

public interface AdvertiseHome extends EJBHome
{
    Advertise create (String login) throws RemoteException, CreateException;
}

Obviously, the create() method has a corresponding ejbCreate() method in the AdvertiseBean class itself. This ejbCreate() method must instantiate the bean with any appropriate state, as shown in listing 5.4.

Listing 5.4. AdvertiseBean.ejbCreate() Method
 1: package agency;
 2: // imports omitted
 3:
 4: public class AdvertiseBean implements SessionBean
 5: {
 6:     private String login;
 7:     private String name;
 8:     private String email;
 9:     private String[] address;
10:     private Collection jobs = new TreeSet();
11:
12:     public void ejbCreate (String login) throws CreateException {
13:         this.login = login;
14:
15:         // database detail not shown
16:         name = ...;
17:         email = ...;
18:         address[0] = ...;
19:         address[1] = ...;
20:         jobs = new TreeSet();
21:         while(...) {
22:             jobs.add( ... );
23:         }
24:      }
25: }

Alternatively, the EJB specification allows for methods named createXXX() to be defined in the home interface, with corresponding methods ejbCreateXXX(). These methods can take parameters if required. Whether you choose to use this facility or just use regular overloaded versions of create()/ejbCreate() is up to you.

Other than this one change of being able to pass in state in the create() method, there really is little difference in the specification of a stateful bean compared to that of a stateless Session bean. The remote interface of the stateful Advertise Session bean is shown in Listing 5.5.

Listing 5.5. The Remote Interface for the Stateful Advertise Bean
 1: package agency;
 2:
 3: import java.rmi.*;
 4: import javax.ejb.*;
 5:
 6: public interface Advertise extends EJBObject
 7: {
 8:     void updateDetails (String name, String email, String[] Address) throws
 RemoteException;
 9:     String getName() throws RemoteException;
10:     String getEmail() throws RemoteException;
11:     String[] getAddress() throws RemoteException;
12:     String[] getJobs() throws RemoteException;
13:
14:     void createJob (String ref) throws RemoteException, DuplicateException,
 CreateException;
15:     void deleteJob (String ref) throws RemoteException, NotFoundException;
16: }

The ejbCreate() method from the home interface supplies the information to the bean so that it can retrieve the data about the advertiser from the database. The remote interface allows this information to be accessed and be updated.

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

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