We will look at how to schedule a process in JBoss like crontab in UNIX. This can be done in JBoss by using Schedulers. Schedulers provides a simple callback method by implementing the Schedulable interface in custom Java class. It is very easy to use this for scheduling even though we have many options like crontab in UNIX, Timers, etc because of it's easy implementation.
Lets see how to create a Scheduler in JBoss in 2 simple steps
1. Create a simple XML file for the configuration of the Scheduler. The attributes allowed in the configuration xml and their descriptions are
2. Create a class and implement Schedulable Interface. We need to implement only one method perform which takes two parameters. One is Date type, the actual date when it is being called and the other is number of times the scheduler invokes the callback method(The parameter of attribute InitialRepetitions)
That's it. Its time to deploy and run it. Deploy the class into Jboss via jar or ear project. Put the xml file in jboss deploy directory and start JBoss Server.
Once started, you will be able to see the following Log in the JBoss server log. The Scheduler calls the callback function once in every 5 seconds as defined in configuration XML.
The timer will be running, until you stop the Jboss
Lets see how to create a Scheduler in JBoss in 2 simple steps
1. Create a simple XML file for the configuration of the Scheduler. The attributes allowed in the configuration xml and their descriptions are
o InitialStartDate : Date when the initial call is scheduled. It can be either:
o
NOW
: date will be the current time plus 1 seconds
o A number representing the milliseconds since 1/1/1970
o Date as String able to be parsed by
SimpleDateFormat
o 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.
o StartAtStartup : A flag that determines if the
Scheduler
will start when it receives its startService life cycle notification. If true the Scheduler
starts on its startup. If false, an explicit startSchedule
operation must be invoked on the Scheduler
to begin.
o SchedulePeriod : The interval between scheduled calls in milliseconds. This value must be bigger than 0.
o SchedulableClass : The implementation class of the
org.jboss.varia.scheduler.Schedulable
interface.
o SchedulableArguments : A comma separated list of arguments passed to implementation class(Only primitives and String types are supported).
o SchedulableArgumentTypes : The list of argument types passed in the above attribute.
o SchedulableMBean : Specifies the fully qualified JMX
ObjectName
name of the schedulable MBean to be called. When using SchedulableMBean
the SchedulableMBeanMethod
must also be specified.
o SchedulableMBeanMethod : Specifies the operation name to be called on the schedulable MBean.
<?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="org.jboss.varia.scheduler.Scheduler" name=":service=My-Scheduler"> <attribute name="StartAtStartup">true</attribute> <attribute name="SchedulableClass">com.test.scheduler.MyScheduler</attribute> <attribute name="SchedulableArguments">MyScheduler</attribute> <attribute name="SchedulableArgumentTypes">java.lang.String</attribute> <attribute name="InitialStartDate">0</attribute> <attribute name="SchedulePeriod">5000</attribute> <attribute name="InitialRepetitions">-1</attribute> </mbean> </server>
2. Create a class and implement Schedulable Interface. We need to implement only one method perform which takes two parameters. One is Date type, the actual date when it is being called and the other is number of times the scheduler invokes the callback method(The parameter of attribute InitialRepetitions)
package com.test.scheduler; import java.util.Date; import org.jboss.varia.scheduler.Schedulable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyScheduler implements Schedulable { private static final Logger LOG = LoggerFactory.getLogger(MyScheduler.class); private String name; public MyScheduler(String name) { this.name = name; } @Override public void perform(Date arg0, long arg1) { LOG.info("Started "+name); LOG.info("Date "+arg0+" Time "+arg1); } }
That's it. Its time to deploy and run it. Deploy the class into Jboss via jar or ear project. Put the xml file in jboss deploy directory and start JBoss Server.
Once started, you will be able to see the following Log in the JBoss server log. The Scheduler calls the callback function once in every 5 seconds as defined in configuration XML.
22:21:05,066 INFO [MyScheduler] Started MyScheduler 22:21:05,066 INFO [MyScheduler] Date Sat Mar 16 22:21:05 IST 2013 Time -1 22:21:10,067 INFO [MyScheduler] Started MyScheduler 22:21:10,067 INFO [MyScheduler] Date Sat Mar 16 22:21:10 IST 2013 Time -1 22:21:15,068 INFO [MyScheduler] Started MyScheduler 22:21:15,068 INFO [MyScheduler] Date Sat Mar 16 22:21:15 IST 2013 Time -1
The timer will be running, until you stop the Jboss
No comments:
Post a Comment