Oracle® Application Server Containers for J2EE Enterprise JavaBeans Developer's Guide
10g Release 2 (10.1.2) Part No. B15505-01 |
|
![]() Previous |
![]() Next |
You can schedule a timer to execute regularly at specified intervals. In the UNIX world, these are known as cron timers.
The following are examples of the different methods you can use in scheduling a cron timer. Where there is an asterisk, all values are valid.
Example 11-1 How to Configure Different Timers
20 * * * * --> 20 minutes after every hour, such as 00:20, 01:20, and so on 5 22 * * * --> Every day at 10:05 P.M. 0 8 1 * * --> First day of every month at 8:00 A.M. 0 8 4 7 * --> The fourth of July at 8:00 A.M. 15 12 * * 5 --> Every Friday at 12:15 P.M.
The format of a cron time variable includes five time fields:
Minute: 0-59
Hour: 0-23
Day of the Month: 1-31
Month: 1-12 or specify with the following strings: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
Day of the Week: 0-7 or with the following strings: Sun, Mon, Tue, Wed, Thu, Fri, Sat. Both 0 and 7 signify Sunday.
You can define complex timers by specifying multiple values in a field, separated by commas or a dash.
Example 11-2 Complex Timers
0 8 * * 1,3,5 --> Every Monday, Wednesday, and Friday at 8:00 A.M. 0 8 1,15 * * --> The first and 15th of every month at 8:00 A.M. 0 8-17 * * 1-5 --> Every hour from 8 A.M. through 5 P.M., Monday through Friday
You can create cron timers either through the createTimer
method that takes a String
with the previous five fields in it—separated by spaces—or with the createTimer
method that has variables for each field. To create the cron timers, use the following Oracle-specific createTimer
APIs:
EJBTimer createTimer(String cronline, Serializable info) throws IllegalArgumentException, IllegalStateException; EJBTimer createTimer(int minute, int hour, int dayOfMonth, int month, int dayOfWeek, int year, Serializable info) throws IllegalArgumentException, IllegalStateException;
Create the cron timers in the same manner as other timers by retrieving the extended Oracle-specific timer service, and schedule a cron timer using the createTimer
method. However, since cron timers are Oracle-specific, you cast the returned object as an EJBTimerService
object. The following example provides a String
with the five variables separated by spaces. The timer is scheduled to execute every minute.
import oracle.ias.container.timer.EJBTimer; import oracle.ias.container.timer.EJBTimerService; ... String cron = "1 * * * *"; EJBTimerService ets = (EJBTimerService) ctx.getTimerService(); EJBTimer et = ets.createTimer(cron, info);
You can also provide a class that is to be invoked within the createTimer
method, as follows:
EJBTimer createTimer(String cronline, String className, Serializable info) throws IllegalArgumentException, IllegalStateException; EJBTimer createTimer(int minute, int hour, int dayOfMonth, int month, int dayOfWeek, String className, Serializable info) throws IllegalArgumentException, IllegalStateException;
For arbitrary Java classes, the info variable can be either null or be a String[]
of parameters to pass to the main method of the class.
For example, you can have the mypackage.MyClass
invoked when the et
timer fires:
EJBTimerService ets = (EJBTimerService) ctx.getTimerService(); EJBTimer et = ets.createTimer(cron,"mypackage.MyClass", info);
You must provide a main method within the mypackage.MyClass
, which is used as the entry point, as follows:
public static void main( String args[] )