background image

Business Methods

<< Life-Cycle Callback Methods | The Remove Method >>
<< Life-Cycle Callback Methods | The Remove Method >>

Business Methods

@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.
Business Methods
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 cart Example
Chapter 22 · Session Bean Examples
661