Setup Scheduler Service on JBoss Application Server

 Step1). Create a directory somewhere in your file system like "/home/username/SchedulerDemo/SchedulerDemo".
Step 2). Configure a scheduler mbean in jboss-server.xml or create a "myScheduler-service.xml" inside "/home/username/SchedulerDemo/SchedulerDemo" directory as following:

xml version="1.0" encoding="UTF-8"?>
<server>
    <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>
    </mbean>
</server>
 
 
Step3). Write the Schedulable class “MySchedulable.java” inside “/home/username/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);
    }
}
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

Reference: http://middlewaremagic.com/jboss/?p=320



留言

這個網誌中的熱門文章

Disable ionic's sidemenu content drag to toggle menu

java.lang.NoClassDefFoundError: org/apache/xerces/jaxp/datatype/XMLGregorianCalendarImpl$Parser

Multiple writable mappings exist for the field. Only one may be defined as writable, all others must be specified read-only.