The session bean class for this example is called CartBean. Like any stateful session bean, the CartBean class must meet these requirements:
The class implements the business methods defined in the business interface.
Stateful session beans also may:
Implement the business interface, a plain Java interface. It is good practice to implement the bean’s business interface.
Implement any optional life cycle callback methods, annotated @PostConstruct, @PreDestroy, @PostActivate, and @PrePassivate.
The source code for the CartBean class follows.
package com.sun.tutorial.javaee.ejb; import java.util.ArrayList; import java.util.List; import javax.ejb.Remove; import javax.ejb.Stateful; @Stateful public class CartBean implements Cart { String customerName; String customerId; List<String> contents; public void initialize(String person) throws BookException { if (person == null) { throw new BookException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new ArrayList<String>(); } public void initialize(String person, String id) throws BookException { if (person == null) { throw new BookException("Null person not allowed."); } else { customerName = person; } IdVerifier idChecker = new IdVerifier(); if (idChecker.validate(id)) { customerId = id; } else { throw new BookException("Invalid id: " + id); } contents = new ArrayList<String>(); } public void addBook(String title) { contents.add(title); } public void removeBook(String title) throws BookException { boolean result = contents.remove(title); if (result == false) { throw new BookException(title + " not in cart."); } } public List<String> getContents() { return contents; } @Remove public void remove() { contents = null; } }
Methods in the bean class may be declared as a life-cycle callback method by annotating the method with the following annotations:
javax.annotation.PostConstruct
javax.annotation.PreDestroy
javax.ejb.PostActivate
javax.ejb.PrePassivate
Life-cycle callback methods must return void and have no parameters.
@PostConstruct methods are invoked by the container on newly constructed bean instances after all dependency injection has completed and before the first business method is invoked on the enterprise bean.
@PreDestroy methods are invoked after any method annotated @Remove has completed, and before the container removes the enterprise bean instance.
@PostActivate methods are invoked by the container after the container moves the bean from secondary storage to active status.
@PrePassivate methods are invoked by the container before the container passivates the enterprise bean, meaning the container temporarily removes the bean from the environment and saves it to secondary storage.
The primary purpose of a session bean is to run business tasks for the client. The client invokes business methods on the object reference it gets from dependency injection or JNDI lookup. From the client’s perspective, the business methods appear to run locally, but they actually run remotely in the session bean. The following code snippet shows how the CartClient program invokes the business methods:
cart.create("Duke DeEarl", "123"); ... cart.addBook("Bel Canto"); ... List<String> bookList = cart.getContents(); ... cart.removeBook("Gravity’s Rainbow");
The CartBean class implements the business methods in the following code:
public void addBook(String title) { contents.addElement(title); } public void removeBook(String title) throws BookException { boolean result = contents.remove(title); if (result == false) { throw new BookException(title + "not in cart."); } } public List<String> getContents() { return contents; }
The signature of a business method must conform to these rules:
The method name must not begin with ejb to avoid conflicts with callback methods defined by the EJB architecture. For example, you cannot call a business method ejbCreate or ejbActivate.
The access control modifier must be public.
If the bean allows remote access through a remote business interface, the arguments and return types must be legal types for the Java RMI API.
If the bean is a web service endpoint, the arguments and return types for the methods annotated @WebMethod must be legal types for JAX-WS.
The modifier must not be static or final.
The throws clause can include exceptions that you define for your application. The removeBook method, for example, throws the BookException if the book is not in the cart.
To indicate a system-level problem, such as the inability to connect to a database, a business method should throw a javax.ejb.EJBException. The container will not wrap application exceptions such as BookException. Because EJBException is a subclass of RuntimeException, you do not need to include it in the throws clause of the business method.