Automatic timers are created by the EJB container when an enterprise bean that contains methods annotated with the @Schedule or @Schedules annotations is deployed. An enterprise bean can have multiple automatic timeout methods, unlike a programmatic timer, which allows only one method annotated with the @Timeout annotation in the enterprise bean class.
Automatic timers can be configured through annotations or through the ejb-jar.xml deployment descriptor.
Adding a @Schedule annotation on an enterprise bean marks that method as a timeout method according to the calendar schedule specified in the attributes of @Schedule. The @Schedule annotation has elements that correspond to the calendar expressions detailed in Creating Calendar-Based Timer Expressions and the persistent, info, and timezone elements.
Example: The following timeout method uses @Schedule to set a timer that will expire every Sunday at midnight:
package com.sample; import javax.ejb.Stateless; import java.util.Date; import javax.ejb.Schedule; @Stateless(name="AutomaticTimer") public class AutomaticTimerBean { @Schedule(dayOfWeek="Sun", hour="0") public void automaticTimer() { System.out.println("\n\n\t Automatic Timer invoked at: "+new Date()); } }
If you want a reference to the Timer attributes, check the following table:
Attribute | Description | Default Value | Allowable Values and Examples |
---|---|---|---|
second | One or more seconds within a minute | 0 | 0 to 59. For example: second="30". |
minute | One or more minutes within an hour | 0 | 0 to 59. For example: minute="15". |
hour | One or more hours within a day | 0 | 0 to 23. For example: hour="13". |
dayOfWeek | One or more days within a week | * | 0 to 7 (both 0 and 7 refer to Sunday). For example: dayOfWeek="3".
Sun, Mon, Tue, Wed, Thu, Fri, Sat. For example: dayOfWeek="Mon". |
dayOfMonth | One or more days within a month | * | 1 to 31. For example: dayOfMonth="15".
–7 to –1 (a negative number means the nth day or days before the end of the month). For example: dayOfMonth="–3". Last. For example: dayOfMonth="Last". [1st, 2nd, 3rd, 4th, 5th, Last] [Sun, Mon, Tue, Wed, Thu, Fri, Sat]. For example: dayOfMonth="2nd Fri". |
month | One or more months within a year | * | 1 to 12. For example: month="7".
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec. For example: month="July". |
year | A particular calendar year | * | A four–digit calendar year. For example: year="2011". |