An Introduction to the Enterprise JavaBeans 3.0 (EJB 3) Specification
Stateful Session Beans
The stateful session bean is a session bean that maintains its internal states. If the client invokes method calls against the same bean stub, the calls are always tunneled to the same bean instance in the container. So, all field variables in the bean instance retain their values as long as the client application retains the bean stub (or reference for a local client).
Business interface
The business interface of a stateful session bean on the EJB 3.0 API is also a plain Java interface. Business interfaces are required for stateful session beans. It is not always necessary to define one. When undefined they will be automatically generated for you. The type of generated interface, either local or remote, is dependent on the annotation you used in the bean class and will be a local interface if there is no annotation. All the public methods of the bean class will be included as part of the automatically generated business interface.
Home interface
Stateful session beans do not need home interfaces.
Bean class
A stateful session bean must be annotated with the stateful annotation or denoted in the deployment descriptor as a stateful session bean. The bean class does not need to implement the
javax.ejb.Session
Bean interface. A stateful session bean may implement the
SessionSynchronization
interface.
The implementation of the stateful
TraderBean
is straightforward. We annotated the implementation class as
@Stateful
and used Java objects (like Integer, String) to back up the bean properties defined in the session bean interface. The Java objects are initialized for each bean instance when it is created, at the beginning of a client session. Below is the complete code for the
TraderBean
class. It is important to note that the stateful session bean class must implement the serializable interface so that the container can serialize the bean instances and store them to preserve the state information when the instances are not in use.
@Stateful
public class TraderBean implements Trader, Serializable {
public String symbol = "";
public int quantity = 0;
public void buy (String symbol, int quantity){
System.out.println("Buying "+quantity+ " of "+ symbol);
}
public void sell (String symbol, int quantity);{
System.out.println("Selling "+quantity+ " of "+ symbol);
}
public String getSymbol(){
return symbol;
}
public int getQuantity(){
return quantity;
}
// Other getter methods for the attributes ...
}
The Session Bean Client
Here is a sample client:
Trader tr = null;
if (tr == null) {
try {
InitialContext ctx = new InitialContext();
tr = (Trader) ctx.lookup(
Trader.class.getName());
} catch (Exception e) {
e.printStackTrace ();
}
}
// Make use of the tr object
Callbacks for stateful session beans
Stateful session beans support callbacks for the following life cycle events: construction, destruction, activation, and passivation. The EJB 3.0 specification defines several annotations the bean can use to specify callback methods during the life cycle of the bean. The container automatically calls the annotated methods at different stages of the session bean life cycle. You can use the following annotations to tag any method in the bean class:
-
@PostConstruct
: The annotated method is called by the container immediately after a bean instance is instantiated. This annotation is applicable to both stateless and stateful session beans. -
@PreDestroy
: The annotated method is called before the container destroys an unused or expired bean instance from its object pool. This annotation is applicable to both stateless and stateful session beans. -
@PrePassivate
: If a stateful session bean instance is idle for too long, the container may passivate it and store its state to a cache. The method tagged by this annotation is called before the container passivates the bean instance. This annotation is applicable only to stateful session beans. -
@PostActivate
: When the client uses the passivated stateful session bean again, a new instance is created and the bean state is restored. The method that tagged this annotation is called when the activated bean instance is ready. This annotation is only applicable to stateful session beans. -
@Init
: This annotation designates initialization methods for a stateful session bean. It is different from the@PostConstruct
annotation in that multiple methods can be tagged with@Init
in a stateful session bean. However, each bean instance can have only one@Init
method invoked. The EJB 3.0 container determines which@Init
method to invoke depending on how the bean is created (see the EJB 3.0 specification for details). The@PostConstruct
method is called after the@Init
method.
Another life cycle method annotation for a stateful session bean is the
@Remove
tag. It is not a callback method since the application, not the container, calls the
@Remove
method on the bean stub to remove the bean instance in the container object pool.