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.

Using theTimer Service<br />

668<br />

To retrieve all of a bean’s active timers, call the getTimers method of the TimerService<br />

interface. <strong>The</strong> getTimers method returns a collection of Timer objects.<br />

Transactions andTimers<br />

An enterprise bean usually creates a timer within a transaction. If this transaction is rolled back,<br />

the timer creation is also rolled back. Similarly, if a bean cancels a timer within a transaction<br />

that gets rolled back, the timer cancellation is rolled back. In this case, the timer’s duration is<br />

reset as if the cancellation had never occurred.<br />

In beans that use container-managed transactions, the @Timeout method usually has the<br />

Required or RequiresNew transaction attribute to preserve transaction integrity. With these<br />

attributes, the EJB container begins the new transaction before calling the @Timeout method. If<br />

the transaction is rolled back, the container will call the @Timeout method at least one more<br />

time.<br />

<strong>The</strong> timersession Example<br />

<strong>The</strong> source code for this example is in the<br />

tut-install/javaeetutorial5/examples/ejb/timersession/timersession-ejb/src/java/<br />

directory.<br />

TimerSessionBean is a stateless session bean that shows how to set a timer. In the source code<br />

listing of TimerSessionBean that follows, note the createTimer and @Timeout methods.<br />

Because it’s a business method, createTimer is defined in the bean’s remote business interface<br />

(TimerSession) and can be invoked by the client. In this example, the client invokes<br />

createTimer with an interval duration of 30,000 milliseconds. <strong>The</strong> createTimer method<br />

creates a new timer by invoking the createTimer method of TimerService.ATimerService<br />

instance is injected by the container when the bean is created. Now that the timer is set, the EJB<br />

container will invoke the timeout method of TimerSessionBean when the timer expires, in<br />

about 30 seconds. Here’s the source code for the TimerSessionBean class:<br />

package com.sun.tutorial.javaee.ejb;<br />

import java.util.logging.Logger;<br />

import javax.annotation.Resource;<br />

import javax.ejb.Stateless;<br />

import javax.ejb.Timeout;<br />

import javax.ejb.Timer;<br />

import javax.ejb.TimerService;<br />

@Stateless<br />

public class TimerSessionBean implements TimerSession {<br />

@Resource<br />

TimerService timerService;<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!