Scheduling Tasks


Java includes a simple timer-based capability through the java.util.Timer and java.util.TimerTask utility classes. JMX also includes a mechanism for scheduling JMX notifications at a given time with an optional repeat interval as the javax.management.timer.TimerMBean agent service.

JBoss includes two variations of the JMX timer service in the org.jboss.varia.scheduler.Scheduler and org.jboss.varia. scheduler.ScheduleManager MBeans. Both of these MBeans rely on the JMX timer service for the basic scheduling. They extend the behavior of the timer service as described in the following sections.

The org.jboss.varia.scheduler.Scheduler MBean

The Scheduler MBean differs from TimerMBean in that Scheduler directly invokes a callback on an instance of a user-defined class or an operation of a user-specified MBean.

The following are the configurable attributes of Scheduler MBean:

  • InitialStartDate The date when the initial call is scheduled. It can be one of the following:

    • NOW, in which case the date will be the current time plus 1 second.

    • A number representing the milliseconds since 1/1/1970.

    • The date as a string that is able to be parsed by SimpleDateFormat, with the default format pattern "M/d/yy h:mm a". If the date is in the past, the Scheduler MBean will search a start date in the future with respect to the initial repetitions and the period between calls. This means that when you restart the MBean (that is, restart JBoss and so on), it will start at the next scheduled time. When no start date is available in the future, Scheduler will not start.

    For example, if you start your Schedulable class every day at noon and you restart your JBoss server, it will start at the next noon.

  • InitialRepetitions The number of times the scheduler will invoke the target's callback. If -1, then the callback will be repeated until the server is stopped.

  • StartAtStartup A flag that determines whether Scheduler will start when it receives its startService life cycle notification. If TRue, the Scheduler MBean starts on its startup. If false, an explicit startSchedule operation must be invoked on the Scheduler to begin.

  • SchedulePeriod The interval between scheduled calls, in milliseconds. This value must be bigger than 0.

  • SchedulableClass The fully qualified classname of the org.jboss.varia. scheduler.Schedulable interface implementation that is to be used by the Scheduler Mbean. SchedulableArguments and SchedulableArgumentTypes must be populated to correspond to the constructor of the Schedulable implementation.

  • SchedulableArguments A comma-separated list of arguments for the Schedulable implementation class constructor. Only primitive data types, String, and classes with a constructor that accepts a String as its sole argument are supported.

  • SchedulableArgumentTypes A comma-separated list of argument types for the Schedulable implementation class constructor. This will be used to find the correct constructor via reflection. Only primitive data types, String, and classes with a constructor that accepts a String as its sole argument are supported.

  • SchedulableMBean The fully qualified JMX ObjectName name of the schedulable MBean to be called. If the MBean is not available, it will not be called, but the remaining repetitions will be decremented. When you're using SchedulableMBean, SchedulableMBeanMethod must also be specified.

  • SchedulableMBeanMethod The operation name to be called on the schedulable MBean. It can optionally be followed by an opening bracket, a comma-separated list of parameter keywords, and a closing bracket. The supported parameter keywords include the following:

    • NOTIFICATION, which will be replaced by the timer's notification instance (javax.management.Notification)

    • DATE, which will be replaced by the date of the notification call (java.util.Date)

    • REPETITIONS, which will be replaced by the number of remaining repetitions (long)

    • SCHEDULER_NAME, which will be replaced by the ObjectName of the Scheduler

    • Any fully qualified classname, which the Scheduler will set to null. A given Scheduler instance supports only a single schedulable instance. If you need to configure multiple scheduled events, you use multiple Scheduler instances, each with a unique ObjectName. The following is an example of configuring a Scheduler MBean to call a Schedulable implementation as well as a configuration for calling an MBean:

 <server>     <mbean code="org.jboss.varia.scheduler.Scheduler"            name="jboss.docs.chap10:service=Scheduler">         <attribute name="StartAtStartup">true</attribute>         <attribute name="SchedulableClass">org.jboss.chap10.ex2.ExSchedulable</attribute>         <attribute name="SchedulableArguments">TheName,123456789</attribute>         <attribute name="SchedulableArgumentTypes">java.lang.String,long</attribute>        <attribute name="InitialStartDate">NOW</attribute>        <attribute name="SchedulePeriod">60000</attribute>        <attribute name="InitialRepetitions">-1</attribute>    </mbean> </server> 

The SchedulableClass org.jboss.chap10.ex2.ExSchedulable example class is shown here:

 package org.jboss.chap10.ex2; import java.util.Date; import org.jboss.varia.scheduler.Schedulable; import org.apache.log4j.Logger; /**  * A simple Schedulable example.  * @author Scott.Stark@jboss.org  * @version $Revision: 1.5 $  */ public class ExSchedulable implements Schedulable {     private static final Logger log = Logger.getLogger(ExSchedulable.class);     private String name;     private long value;     public ExSchedulable(String name, long value)     {         this.name = name;         this.value = value;         log.info("ctor, name: " + name + ", value: " + value);     }     public void perform(Date now, long remainingRepetitions)     {         log.info("perform, now: " + now +                  ", remainingRepetitions: " + remainingRepetitions +                  ", name: " + name + ", value: " + value);     } } 

You deploy the timer SAR by running this command:

 [examples]$ ant -Dchap=chap10 -Dex=2 run-example 

The server console shows the following, which includes the first two timer invocations, separated by 60 seconds:

[View full width]

21:09:27,716 INFO [ExSchedulable] ctor, name: TheName, value: 123456789 21:09:28,925 INFO [ExSchedulable] perform, now: Mon Dec 20 21:09:28 CST 2004, remainingRepetitions: -1, 21:10:28,899 INFO [ExSchedulable] perform, now: Mon Dec 20 21:10:28 CST 2004, remainingRepetitions: -1, 21:11:28,897 INFO [ExSchedulable] perform, now: Mon Dec 20 21:11:28 CST 2004, remainingRepetitions: -1,



JBoss 4. 0(c) The Official Guide
JBoss 4.0 - The Official Guide
ISBN: B003D7JU58
EAN: N/A
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net