Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Simplify enterprise Java development with EJB 3.0, Part 1

Use annotations to develop POJO services

  • Print
  • Feedback

Page 4 of 6

But what if the application needs finer control over the session object? For instance, the application might need to perform database initialization when the container creates the session bean, or close external connections when the bean is destroyed. You can do these by implementing lifecycle callback methods in the bean class. These methods are called by the container at various stages of the bean's lifecycle (e.g., bean creation and destruction). In EJB 3.0, you can specify any bean method as a callback by annotating it with the following annotations. Unlike EJB 2.1, where all callback methods must be implemented even if they are empty, EJB 3.0 beans can have any number of callback methods with any method name.

  • @PostConstruct: The container immediately calls the annotated method after a bean instance is instantiated. This annotation applies 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 might 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 applies 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 tagged by 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 differs 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 useful annotation for a lifecycle method, especially for stateful session beans, is @Remove. When the application calls the @Remove tagged method through the stub, the container knows to remove the bean instance from the object pool after the method executes. Here is an example of those lifecycle method annotations in CalculatorBean:

 @Stateful
public class CalculatorBean implements Calculator, Serializable {

// ... ... @PostConstruct public void initialize () { // Initializes the history records and load // necessary data from database etc. } @PreDestroy public void exit () { // Save history records into database if necessary. } @Remove public void stopSession () { // Call to this method signals the container // to remove this bean instance and terminates // the session. The method body can be empty. } // ... ... }


Message-driven beans

The session bean services are provided over synchronous method invocations. Another important type of loosely coupled service is an asynchronous service triggered by incoming messages (e.g., email or Java Message Service messages). The EJB 3.0 message-driven beans (MDBs) are components designed to handle message-based service requests.

  • Print
  • Feedback

Resources