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.

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

660<br />

■ <strong>The</strong> modifier must not be static or final.<br />

<strong>The</strong> throws clause can include exceptions that you define for your application. <strong>The</strong> removeBook<br />

method, for example, throws the BookException if the book is not in the cart.<br />

To indicate a system-level problem, such as the inability to connect to a database, a business<br />

method should throw a javax.ejb.EJBException. <strong>The</strong> container will not wrap application<br />

exceptions such as BookException. Because EJBException is a subclass of RuntimeException,<br />

you do not need to include it in the throws clause of the business method.<br />

<strong>The</strong> Remove Method<br />

Business methods annotated with javax.ejb.Remove in the stateful session bean class can be<br />

invoked by enterprise bean clients to remove the bean instance. <strong>The</strong> container will remove the<br />

enterprise bean after a @Remove method completes, either normally or abnormally.<br />

In CartBean, the remove method is a @Remove method:<br />

@Remove<br />

public void remove() {<br />

contents = null;<br />

}<br />

Helper Classes<br />

<strong>The</strong> CartBean session bean has two helper classes: BookException and IdVerifier. <strong>The</strong><br />

BookException is thrown by the removeBook method, and the IdVerifier validates the<br />

customerId in one of the create methods. Helper classes may reside in the EJB JAR file that<br />

contains the enterprise bean class, or in an EAR that contains the EJB JAR.<br />

Building, Packaging, Deploying, and Running the cart<br />

Example<br />

You can build, package, deploy, and run the cart application using either NetBeans IDE or the<br />

Ant tool.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

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

Saved successfully!

Ooh no, something went wrong!