10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

@PrePassivate methods are invoked by the container before the container passivates the<br />

enterprise bean, meaning the container temporarily removes the bean from the environment<br />

and saves it to secondary storage.<br />

Business Methods<br />

<strong>The</strong> primary purpose of a session bean is to run business tasks for the client. <strong>The</strong> client invokes<br />

business methods on the object reference it gets from dependency injection or JNDI lookup.<br />

From the client’s perspective, the business methods appear to run locally, but they actually run<br />

remotely in the session bean. <strong>The</strong> following code snippet shows how the CartClient program<br />

invokes the business methods:<br />

cart.create("Duke DeEarl", "123");<br />

...<br />

cart.addBook("Bel Canto");<br />

...<br />

List bookList = cart.getContents();<br />

...<br />

cart.removeBook("Gravity’s Rainbow");<br />

<strong>The</strong> CartBean class implements the business methods in the following code:<br />

public void addBook(String title) {<br />

contents.addElement(title);<br />

}<br />

public void removeBook(String title) throws BookException {<br />

boolean result = contents.remove(title);<br />

if (result == false) {<br />

throw new BookException(title + "not in cart.");<br />

}<br />

}<br />

public List getContents() {<br />

return contents;<br />

}<br />

<strong>The</strong> signature of a business method must conform to these rules:<br />

■ <strong>The</strong> method name must not begin with ejb to avoid conflicts with callback methods defined<br />

by the EJB architecture. For example, you cannot call a business method ejbCreate or<br />

ejbActivate.<br />

■ <strong>The</strong> access control modifier must be public.<br />

<strong>The</strong> cart Example<br />

■ If the bean allows remote access through a remote business interface, the arguments and<br />

return types must be legal types for the <strong>Java</strong> RMI API.<br />

■ If the bean is a web service endpoint, the arguments and return types for the methods<br />

annotated @WebMethod must be legal types for JAX-WS.<br />

Chapter 22 • Session Bean Examples 659

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!