views:

20

answers:

1

Timers are created in EJB3 using the TimerService.createTimer(), and are then run via whatever callback method that's annotated by the @Timeout annotation, i.e:

@Resource
private TimerService timerService;

public void createHampster() {
    Hampster hampster = new Hampster("Fluffy III");
    timerService.createTimer(3000, 3000, hampster);
}

(...)

@Timeout
public void feedHampster(Timer timer) {
    Hampster hampster = (Hampster) timer.getInfo()
    //(...)
}

So, my question is, do these two blocks of code need to be within the same bean? Are timers inherit to the bean in which they were created, or are they global? My own testing suggests but former, and I haven't found anything definite in the documentation.

+2  A: 

They are tied to the bean that creates them. EJB 3 specification sections 18.2 says:

The bean class of an enterprise bean that uses the timer service must provide a timeout callback method.

and

When the time specified at timer creation elapses, the container invokes the timeout callback method of the bean.

bkail
Good one. I didn't know about the EJB3 spec.
mikek
You can get them here if the license agreement isn't a problem for you: http://java.sun.com/products/ejb/docs.html
bkail