Many times it in production scenarios it is required to implement our Jobs to be scheduled and those jobs should run in a frequent interval. JBoss provides a Scheduler service which can be used in order to make it work. The Scheduler directly invokes a callback on an instance of a user defined class, or an operation of a user specified MBean.

JBoss provides an interface “org.jboss.varia.scheduler.Schedulable” This interface defines the manageable interface for a Scheduler Service and allows the client to create a Schedulable instance which is then run by this service at given times (InitialRepetitions). The InitialRepetitions count starts from the value given and in every iteration the value of InitialRepetitions decreases by 1 in every iteration, until the InitialRepetitions value becomes 0.

A developers responsibility is to implement the org.jboss.varia.scheduler.Schedulable interface and then provide the implementation to the void perform(Date timeOfCall, long remaining Repetitions) method. What kind of activities we want to schedule can be written inside the perform() based on our requirement. In this sample we will create a simple Schedulable abd then we will schedule the task for repetition count as 10 and each repetition should happen in the interval of 5 seconds.

Step1). Create a directory somewhere in your file system like “/home/userone/SchedulerDemo/SchedulerDemo”.

Step2). Write the Schedulable class “MySchedulable.java” inside “/home/userone/SchedulerDemo/SchedulerDemo” directory as following:

package test;
import java.util.Date;
import org.jboss.varia.scheduler.Schedulable;
import org.apache.log4j.Logger;
public class MySchedulable implements Schedulable
    private static final Logger log = Logger.getLogger(MySchedulable.class);

    private String name;
    private long value;

    public MySchedulable(String name, long value)
        this.name = name;
        this.value = value;
        log.info("nt name: " + name + ", value: " + value);

    public void perform(Date now, long repetitions)
        log.info("perform(), time: " + now +", repetitions: " + repetitions +", name: " + name + ", value: " + value);

Step3). Create a scheduler description service XML file as “myScheduler-service.xml” inside “/home/userone/SchedulerDemo/SchedulerDemo” directory as following:

<?xml version="1.0" encoding="UTF-8"?>
    <mbean code="org.jboss.varia.scheduler.Scheduler" name="jboss.test:service=MyScheduler">
        <attribute name="StartAtStartup">true</attribute>
        <attribute name="SchedulableClass">test.MySchedulable</attribute>
        <attribute name="SchedulableArguments">MySchedulable,100</attribute>
        <attribute name="SchedulableArgumentTypes">java.lang.String,long</attribute>
        <attribute name="InitialStartDate">NOW</attribute>
        <attribute name="SchedulePeriod">5000</attribute>
        <attribute name="InitialRepetitions">10</attribute>

InitialRepetitions=10 means in each repetition of perform() the value will be decreased by 1 and the total repetition will be 10.
MySchedulable,100 are the String and long value which we want to pass to our Schedulable class constructor while initialization.
test.MySchedulable: is the name of our Schedulable class.
java.lang.String,long: Are the argument types of the constructor of our Schedulable class.
NOW : represents the current Data/Time stamp
5000: Is the SchedulePeriod which means the perform() of our schedulable class will be invoked at every 5 seconds interval.
InitialRepetitions=10 means in each repetition of perform() the value will be decreased by 1 and the total repetition will be 10.

Step4). Make sure that your CLASSPATH and PATH is set as following before compiling the above program:

export JAVA_HOME=/home/userone/jdk1.6.0_21
export PATH=$JAVA_HOME/bin:$PATH
export JBOSS_HOME=/home/userone/jboss-5.1/jboss-as
export CLASSPATH=$JBOSS_HOME/common/lib/scheduler-plugin.jar:$JBOSS_HOME/client/log4j.jar:$CLASSPATH:.:

Step5). Now compile the MySchedulable.java as following and then make a Jar of it:

javac -d . MySchedulable.java
jar cvf myscheduler.jar test MySchedulable.java myScheduler-service.xml

Step6). Now deploy the “myscheduler.jar” inside the “$PROFILE/deploy” directory. you will see the following kind of output after deploying the Schedulable Service:

15:02:21,786 INFO  [MySchedulable]
	 name: MySchedulable, value: 100
15:02:22,787 INFO  [MySchedulable] perform(), time: Sun  15:02:22 IST 2011, repetitions: 9, name: MySchedulable, value: 100
15:02:27,787 INFO  [MySchedulable] perform(), time: Sun  15:02:27 IST 2011, repetitions: 8, name: MySchedulable, value: 100
15:02:32,788 INFO  [MySchedulable] perform(), time: Sun  15:02:32 IST 2011, repetitions: 7, name: MySchedulable, value: 100
15:02:37,788 INFO  [MySchedulable] perform(), time: Sun  15:02:37 IST 2011, repetitions: 6, name: MySchedulable, value: 100
15:02:42,789 INFO  [MySchedulable] perform(), time: Sun  15:02:42 IST 2011, repetitions: 5, name: MySchedulable, value: 100
15:02:47,790 INFO  [MySchedulable] perform(), time: Sun  15:02:47 IST 2011, repetitions: 4, name: MySchedulable, value: 100
15:02:52,790 INFO  [MySchedulable] perform(), time: Sun  15:02:52 IST 2011, repetitions: 3, name: MySchedulable, value: 100
15:02:57,791 INFO  [MySchedulable] perform(), time: Sun  15:02:57 IST 2011, repetitions: 2, name: MySchedulable, value: 100
15:03:02,791 INFO  [MySchedulable] perform(), time: Sun  15:03:02 IST 2011, repetitions: 1, name: MySchedulable, value: 100
15:03:07,792 INFO  [MySchedulable] perform(), time: Sun  15:03:07 IST 2011, repetitions: 0, name: MySchedulable, value: 100

Step7). Once the Scheduling is over and repetition is 0 you can restart your Scheduled task using the jmx-console “jboss.test—>service=MyScheduler”
operation “restartSchedule” similarly you can change the schedule interval and repetition etc from jmx-console.

More information about the different attribute which can be defined inside the myScheduler-service.xml file can be found in the following link: http://docs.jboss.org/jbossas/docs/Server_Configuration_Guide/4/html/Scheduling_Tasks-org.jboss.varia.scheduler.Scheduler_.html

Middleware Magic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.