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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using theTimer Service<br />

2. Enter the admin username and password to log in to the Admin Console.<br />

3. Click Web Services in the left pane of the Admin Console.<br />

4. Click helloservice.<br />

5. Click Test.<br />

6. Under Methods, enter a name as the parameter to the sayHello method.<br />

7. Click the sayHello button.<br />

This will take you to the sayHello Method invocation page.<br />

8. Under Method returned, you’ll see the response from the endpoint.<br />

Using theTimer Service<br />

666<br />

Applications that model business work flows often rely on timed notifications. <strong>The</strong> timer<br />

service of the enterprise bean container enables you to schedule timed notifications for all types<br />

of enterprise beans except for stateful session beans. You can schedule a timed notification to<br />

occur at a specific time, after a duration of time, or at timed intervals. For example, you could set<br />

timers to go off at 10:30 AM on May 23, in 30 days, or every 12 hours.<br />

When a timer expires (goes off), the container calls the method annotated @Timeout in the<br />

bean’s implementation class. <strong>The</strong> @Timeout method contains the business logic that handles the<br />

timed event.<br />

<strong>The</strong> Timeout Method<br />

Methods annotated @Timeout in the enterprise bean class must return void and take a<br />

javax.ejb.Timer object as the only parameter. <strong>The</strong>y may not throw application exceptions.<br />

@Timeout<br />

public void timeout(Timer timer) {<br />

System.out.println("TimerBean: timeout occurred");<br />

}<br />

CreatingTimers<br />

To create a timer, the bean invokes one of the createTimer methods of the TimerService<br />

interface. (For details on the method signatures, see the javax.ejb.TimerService API<br />

documentation.) When the bean invokes createTimer, the timer service begins to count down<br />

the timer duration.<br />

<strong>The</strong> bean described in “<strong>The</strong> timersession Example” on page 668 creates a timer as follows:<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!