Show Table of Contents
12.7. 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 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.
12.7.1. org.jboss.varia.scheduler.Scheduler
The Scheduler differs from the
TimerMBean in that the Scheduler directly invokes a callback on an instance of a user defined class, or an operation of a user specified MBean.
- InitialStartDate: Date when the initial call is scheduled. It can be either:
NOW: date will be the current time plus 1 seconds- A number representing the milliseconds since 1/1/1970
- Date as String able to be parsed by
SimpleDateFormatwith default format pattern "M/d/yy h:mm a". If the date is in the past theSchedulerwill 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 (restarting JBoss etc.) it will start at the next scheduled time. When no start date is available in the future theSchedulerwill not start.
For example, if you start yourSchedulableeveryday at Noon and you restart your JBoss server then it will start at the next Noon (the same if started before Noon or the next day if start after 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 if the
Schedulerwill start when it receives its startService life cycle notification. If true theSchedulerstarts on its startup. If false, an explicitstartScheduleoperation must be invoked on theSchedulerto begin. - SchedulePeriod: The interval between scheduled calls in milliseconds. This value must be bigger than 0.
- SchedulableClass: The fully qualified class name of the
org.jboss.varia.scheduler.Schedulableinterface implementation that is to be used by theScheduler. TheSchedulableArgumentsandSchedulableArgumentTypesmust be populated to correspond to the constructor of theSchedulableimplementation. - SchedulableArguments: A comma separated list of arguments for the
Schedulableimplementation class constructor. Only primitive data types,Stringand classes with a constructor that accepts aStringas its sole argument are supported. - SchedulableArgumentTypes: A comma separated list of argument types for the
Schedulableimplementation class constructor. This will be used to find the correct constructor via reflection. Only primitive data types,Stringand classes with a constructor that accepts aStringas its sole argument are supported. - SchedulableMBean: Specifies the fully qualified JMX
ObjectNamename 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 usingSchedulableMBeantheSchedulableMBeanMethodmust also be specified. - SchedulableMBeanMethod: Specifies 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:
NOTIFICATIONwhich will be replaced by the timers notification instance (javax.management.Notification)DATEwhich will be replaced by the date of the notification call (java.util.Date)REPETITIONSwhich will be replaced by the number of remaining repetitions (long)SCHEDULER_NAMEwhich will be replaced by theObjectNameof theScheduler- Any fully qualified class name which the
Schedulerwill set to null.
A given Scheduler instance only support a single schedulable instance. If you need to configure multiple scheduled events you would use multiple
Scheduler instances, each with a unique ObjectName. The following is an example of configuring a Scheduler to call a Schedulable implementation as well as a configuration for calling a MBean.
<server>
<mbean code="org.jboss.varia.scheduler.Scheduler"
name="jboss.docs:service=Scheduler">
<attribute name="StartAtStartup">true</attribute>
<attribute name="SchedulableClass">org.jboss.book.misc.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
SchedulableClassorg.jboss.book.misc.ex2.ExSchedulable example class is given below.
package org.jboss.book.misc.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.1 $
*/
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);
}
}
Deploy the timer SAR by running:
[examples]$ ant -Dchap=misc -Dex=2 run-example
The server console shows the following which includes the first two timer invocations, separated by 60 seconds:
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, name: TheName, value: 123456789 21:10:28,899 INFO [ExSchedulable] perform, now: Mon Dec 20 21:10:28 CST 2004, remainingRepetitions: -1, name: TheName, value: 123456789 21:11:28,897 INFO [ExSchedulable] perform, now: Mon Dec 20 21:11:28 CST 2004, remainingRepetitions: -1, name: TheName, value: 123456789

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.