Formatting code for JbossScheduler


show source only

{{parent page="Jboss"}}

===Scheduling tasks on jboss===
Jboss comes with a scheduler. I'm not familiar with the terms so it's hard to provide a highlevel description. The following example works on jboss-4.3.2-GA

===Create a Scheduler class===
Nothing too fancy here, it implements the jboss Schedulable class, and writes a log whenever the perform() is called. This class file needs to be packaged as a jar file, and saved to the default/deploy/lib directory under jboss so jboss is able to access it on startup.

%%(java;MyTask.java)
import java.util.Date;
import org.jboss.varia.scheduler.Schedulable;
import org.apache.log4j.Logger;

public class MyTask implements Schedulable
{
private static final Logger log = Logger.getLogger(MyTask.class);

private String name;
private long value;

public MyTask(String name, long value)
{
this.name = name;
this.value = value;
log.info("constructor name: " + name + ", value: " + value);
}

public void perform(Date now, long remainingRepetitions)
{
log.info("perform, now: " + now +
", remainingRepetitions: " + remainingRepetitions +
", name: " + name + ", value: " + value);
}
}
%%

===Deploy the scheduler service===
In jboss's deploy directory, create a service.xml file, let's just call it myscehduler-service.xml. The Timer mbean is needed for every scheduler. If you plan on implementing more than 1 scheduler, you need another Timer mbean. The second mbean calls the MyTask scheduler. It also tell the scheduler when to start and how often it should be waken.

%%(xml;myscheduler-service.xml)
<server>
<mbean code="org.jboss.mx.timer.JBossTimer" name="jboss:service=Timer"></mbean>
<mbean code="org.jboss.varia.scheduler.ScheduleManager" name="jboss:service=ScheduleManager">
<attribute name="StartAtStartup">true</attribute>
<attribute name="FixedRate">true</attribute>
</mbean>
<mbean code="org.jboss.varia.scheduler.Scheduler"
name="jboss.docs:service=Scheduler">
<attribute name="StartAtStartup">true</attribute>
<attribute name="SchedulableClass">MyTask</attribute>
<attribute name="SchedulableArguments">TaskFoo,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>
%%

===Start jboss===
That's it. Once you have the jar file and xml file in place, you should start seeing these messages in server.log:
%%
21:22:55,083 INFO [MyTask] perform, now: Thu Feb 25 21:22:55 HKT 2010, remainingRepetitions: -1, name: TaskFoo, value: 123456789
21:23:55,083 INFO [MyTask] perform, now: Thu Feb 25 21:23:55 HKT 2010, remainingRepetitions: -1, name: TaskFoo, value: 123456789
21:24:55,084 INFO [MyTask] perform, now: Thu Feb 25 21:24:55 HKT 2010, remainingRepetitions: -1, name: TaskFoo, value: 123456789
%%
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki