|
Each Schedule annotation for persistent timers corresponds to a single timer, regardless the number of JVMs across which the application is distributed. This has a tremendous impact on the way timers are used in clustered environments. Lets assume that an application needs to keep an active timer pointing to the next active event. This timer gets updated as soon as new events are submitted. Before EJB 3.1 this would be quite easy to achieve by using the Timer Service, unless the application got deployed in a container distributed in more that one JVM. In that case, whenever a timer got created in one JVM it would not visible in the remaining JVMs. That means that it must have been adopted a strategy which allows the existing timers to be visible in all JVMs. The code becomes aware of the environment where it gets deployed, which leads to bad practices. With EJB 3.1 the developer does not have to care if the application being developed is going to be deployed across multiple JVMs, such task is left up to the container.
An automatically created non-persistent timer gets a new timer created for each JVM across which the container is deployed.
The signature of the timeout callback method can be one of two options- void <METHOD> (Timer timer) or void <METHOD> ().
In terms of timer scheduling there were major improvements. The callback schedule can be expressed using a calendar-based syntax modeled after the UNIX cron. There are eight attributes which can be used in such expressions:
Attribute
Allowable Values
Example
second
[0, 59]
second = "10"
minute
[0, 59]
minute = "30"
hour
[0, 23]
hour = "10"
dayOfMonth
- [1, 31] - day of the month
- Last - last day of the month
- -[1, 7] - number of days before end of month
- {"1st", "2nd", "3rd", "4th", "5th", ..., "Last"} {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}- identifies a single occurrence of a day of the month
dayOfMonth = "3"
dayOfMonth = "Last"
dayOfMonth = "-5"
dayOfMonth = "1st Tue"
month
- [1, 12] - month of the year
- {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}- month name
month = "7"
month = "Jan"
dayOfWeek
- [0, 7]- day of the week where both 0 and 7 refer to Sunday
- {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}- day's name
dayOfWeek = "5"
dayOfWeek = "Wed"
year
Four digit calendar year
year = "1978"
timezone
Id of the related timezone
timezone = "America/New_York"
The values provided for each attribute can be expressed in different forms:
Expression Type
Description
Example
Single Value
Constraints the attribute to just one value
dayOfWeek = "Wed"
Wild Card
Represents all possible values for a given attribute
month = "*"
List
Constraints the attribute to two or more allowable values, separated by a comma
DayOfMonth = "3,10,23"
dayOfWeek = "Wed,Sun"
Range
Constraints the attribute to an inclusive range of values
year = "1978-1984"
Increments
Defines an expression x/y where the attribute is constrained to every yth value within the set of allowable values, beginning at time x.
second = "*/10" - every 10 seconds
hour = "12/2"- every second hour starting at noon
Every Tuesday at 7.30am
@Schedule(hour = "7", minute = "30", dayOfWeek = "Tue")
From Monday to Friday, at 7, 15 and 20
@Schedule(hour = "7, 15, 20", dayOfWeek = "Mon-Fri")
Every hour on Sundays
@Schedule(hour = "*", dayOfWeek = "0")
Last Friday of December, at 12
@Schedule(hour = "12", dayOfMonth = "Last Fri", month="Dec")
Three days before the last day of the month, for every month of 2009, at 8pm
@Schedule(hour = "20", dayOfMonth = "-3", year="2009")
Every 5 minutes of every hour, starting at 3pm
@Schedule(minute = "*/5", hour = "15/1")
The TimerService interface has been enhanced in order to allow programatically created timers to make use of cron alike expressions. Such expressions can be represented by an instance of the class ScheduleExpression, which can be passed as parameter during the timer creation. |
|