Java Reference
In-Depth Information
Timer instances are obtained from the javax.ejb.TimerService . The TimerSer-
vice contains numerous methods for programmatically creating timers, as well as a meth-
od for retrieving all running timers. It's important to note that the list of timers is transi-
ent—a timer retrieved from this interface may be invalid by the time you get around to
inspecting it. Subsequent sections will cover the TimerService in more detail because
it's primarily used for creating timers programmatically.
Two topics we haven't yet touched on are transactions and security with regard to timers.
The topic of transactions is two-fold: how timer creations are impacted by transactions and
how a time-out method participates within the context of a transaction. To answer the first
question, see the following code snippet. If the call to the entityManager fails, the
transaction will be marked for rollback, and this will result in the timer being cancelled
provided that it hasn't already executed. This also applies to timer cancellation: if a method
that cancels a timer is subsequently rolled back, so is the request to cancel the timer.
public void addBid(Bid bid) {
timerService.createTimer(15*60*1000,15*60*1000,bid);
entityManager.persist(bid);
...
}
A time-out method is invoked within the context of a transaction. If the transaction is
rolled back, the container retries the time-out. This happens in the case of a bean with
container-managed transactions and with the time-out method marked with REQUIRED or
REQUIRES_NEW .
With security, the time-out method is invoked with no security context. This means that if
the getCallerPrincipal is invoked, it'll return the container's representation of an
unauthenticated user. Thus, if the time-out method attempts to call any methods that have
security requirements, the method invocation will fail. Remember that timers can be con-
figured via configuration files and thus might have no connection with any user. Now that
we've covered the basics of timers, it's time to look at the different types of timers.
7.1.5. Types of timers
There are two types of timers for EJBs: time-delayed timers and calendar-based timers.
The time-delayed timers were added in EJB 2.1 and can be thought of as sophisticated egg
timers. You can specify that you want a method to execute at regular intervals, at a specific
Search WWH ::




Custom Search