Tuesday, August 07, 2007

Making it easy to schedule work in Synapse

I've just checked in code into the trunk that makes it easy to schedule jobs in Synapse. This is my second attempt at integrating Quartz.

Quartz is a popular open-source scheduling package, and it makes it simple to schedule work.
Quartz has all sorts of options including persistent storage of schedules, etc. But for my first steps, I've just used the two most popular approaches in Quartz: a simple trigger (repeat the job n times every m milliseconds), and a cron-like scheduler (run the job every 12:00:00 on the last day of the month).

Quartz defines its own XML syntax for scheduling work, and it also defines an interface org.quartz.Job which you need to implement. My first attempt used those, but I felt we could make it even easier to schedule jobs using Quartz.

The latest code defines a simple interface you can implement:
public Interface org.apache.synapse.startup.Job {
public void execute();

Most jobs will also want to implement ManagedLifecycle, which gives them the ability to "inject" messages into Synapse through the environment.

I've included a simple sample Job that allows you to send a given message to a given address at the scheduled times. Its called MessageInjector and you can see the code here.

The config is pretty simple too. It lives in the <startup> element.
Here is an example using the MessageInjector:

<job class="MessageInjector">
<simpletrigger forever="true" interval="5000"/>
<property name="message">
<test xmlns="urn:paul"/>
<property name="to" value="urn:test"/>

The properties are injected into the object before execute() is called. You can use any String or OMElement property - just the same as the class mediator. In this case it will inject a new message (with body &lttest xmlns="urn:paul"/>) addressed to urn:test every 5 seconds forever.

Please let us know what you think - either on this blog or at synapse-user@ws.apache.org.