Java Reference
In-Depth Information
There are two types of timers in Java EE 7: automatic and programmatic. Automatic timers are set
upon the deployment of an enterprise Java bean (EJB) that contains a method annotated with either
@Schedule( ... ) or @Schedule ( ... ). The annotated method is invoked by the container's scheduler
at the specii ed times, or time intervals dei ned within the arguments of the annotations. Such
methods are referred to as callback methods. The timer starts ticking as soon as the EJB is deployed.
A programmatic timer is set at run time by a method called from within the business logic. The time
can be coni gured on the l y and invoked at anytime (or not at all). The timer start ticking when the
programming logic determines that it should start.
The EJB container implements the timer service. An enterprise bean can access this
service in three ways: by means of dependency injection, through the EJBContext
interface, or through lookup in the Java Naming and Directory Interface (JNDI)
namespace. This topic only examines the way by means of dependency injection,
because it's the newest and most efi cient.
Automatic Timers
The container invokes any method appropriately annotated with @Schedule and applies the
schedule coni guration specii ed in the annotation's attributes. The attributes of the annotation are
set following the calendar‐based timer attributes in the “Timer Expression” section that follows.
Here is a simple example:
@Schedule(second="*/1", minute="*", hour="*")
public void executeTask(){
System.out.println("Task performed");
In this code snippet, the method executeTask is annotated @Schedule ; this indicates to the
container to set a timer upon deployment based on the time values specii ed in the annotations
attributes. In this example the container invokes the executeTask method once every second.
By default, all timers are persisted and restored after a server shutdown or crash. If you set the
optional attribute p ersistent
to false , the timer is reset on server restart. You can set two additional
attributes: info and timezone . If you set timezone , that time zone is respected when executing the
timer; otherwise, the server time zone is used. The info attribute allows a developer to provide a
description of the time that you can retrieve by calling the getInfo method of the Timer interface.
@Schedule(hour = "23", minute = "59", timezone = "CET",
info = "Generates nightly
public void executeTask(){
System.out.println("Task performed");
Search WWH ::

Custom Search