- Back to Home »
- Task Scheduling in JBoss Server ...
Posted by : Unknown
Sunday, August 7, 2011
First thing I would like to admit is - Sorry for being away for so long !!!
Next let's quickly move to second thing which is the topic of today's very simple post - scheduling jobs in JBoss :)
In many projects there is a requirement for cron-like task scheduling, be it for batch processing, automatic system maintenance or some other regular job. Few days back I had to create a cron job to sync data from external source to the local database. I was having so many options to implement the functionality like:
org.jboss.varia.scheduler.Scheduler
----------------------------------------
The good thing is, this Scheduler directly invokes a callback on an instance of a user defined class, or an operation of a user specified MBean. Let's say we want to print 'Hello World!' (our so called task) after every 30 seconds from the the time of deployment.
HelloWorld class now has to define a constructor as follow:
Note: This example has dependency on scheduler-plugin.jar which can be obtained from /common/lib folder of $JBOSS_HOME (it is sad that it's not available in maven or jboss repository)
Give it a Try !!!!
Next let's quickly move to second thing which is the topic of today's very simple post - scheduling jobs in JBoss :)
In many projects there is a requirement for cron-like task scheduling, be it for batch processing, automatic system maintenance or some other regular job. Few days back I had to create a cron job to sync data from external source to the local database. I was having so many options to implement the functionality like:
- java.util.Timer
- EJB Timers
- Unix Cron jobs
- Quartz Scheduler
- Custom Timer
org.jboss.varia.scheduler.Scheduler
----------------------------------------
The good thing is, this Scheduler directly invokes a callback on an instance of a user defined class, or an operation of a user specified MBean. Let's say we want to print 'Hello World!' (our so called task) after every 30 seconds from the the time of deployment.
package com.gognamunish.test; import org.jboss.varia.scheduler.Schedulable; /** * Implement Schedulable interface and override perform() function. * @author Munish Gogna */ public class HelloWorld implements Schedulable { public void perform( Date now, long remainingRepetitions ) { System.out.println("Hello World"); } }Next just copy the following mbean definition in /deploy/scheduler-service.xml file of the Jboss profile.
Once the deployment is done, this task will be called after every 30 seconds, so easy and simple right? We can also pass input parameters to our target class using 'SchedulableArguments' attribute, just make sure you define the right constructor in the class that implements Schedulable interface. e.g If we want to pass the location and environment of the server, we have to add following lines to the mbean definition:true com.gognamunish.test.HelloWorld 0 30000 -1 true
Singapore,Test java.lang.String,java.lang.String
HelloWorld class now has to define a constructor as follow:
public HelloWorld (String location, String environment){ this.location=location; this.environment=environment; }
Note: This example has dependency on scheduler-plugin.jar which can be obtained from /common/lib folder of $JBOSS_HOME (it is sad that it's not available in maven or jboss repository)
Give it a Try !!!!
Hello and first of all I have to say nice post. I've tried to implement this solution but I'm getting an error when deploying it:
ReplyDelete--- MBeans waiting for other MBeans ---
ObjectName: jboss:service=Scheduler
State: FAILED
Reason: org.jboss.deployment.DeploymentException: Exception setting attribute SchedulableClass = com.example.task.Task on mbean jboss:service=
Scheduler; - nested throwable: (java.security.InvalidParameterException: Given class com.example.task.Task is not not found)
Can you help me with this issue I have, there is no much information about it.
Hi Zuleta,
DeleteLooks like com.gognamunish.test.HelloWorld is not in the classpath.
How are you deploying this scheduler? Try to package that class in deploy able unit (war/ear/client jar etc.) and try again.
cheers,
Munish Gogna
and change the scheduler name as shown below:
Deletename=":service=HelloWorldScheduler" (this is to avoid any conflict that you may have with any other mbean defined in your jboss)