Chaining job execution in Quartz

Quartz javaThere isn’t explicit way to chain jobs in Quartz. However this is still possible, by doing some tricks. Here is an explanation on how to do that (in very few words).

There are two ways to put job in chain:
– first one, by using Listeners( for example JobChainingJobListener),
– and the second one, by using JobDataMap which every job contains.
I prefer the second way with JobDataMaps. Why ? Because DataMaps are persistent so information about chained jobs survives restart of the server.

Chain is nothing more than normal queue of the jobs, so to put two jobs into chain we need to create queue from them. Queue should be bidirectional to allow easy manipulation. The idea is to put 4 properties into JobDataMaps:
– nextJobName, nextJobGroup – identify next job in chain
– previousJobName, previousJobGroup – identify previous job in chain

Those 4 variables are enough to implement standard bi-directional queue operations like: addJobToQueue(..), removeFromQueue(..), moveUp(..) and moveDown(..). One thing to remeber: adding job to chain should stop it, to avoid triggering jobs in unspecified order.

Last thing to do is to create own implementation of the Quartz Job interface which, in execute() method, will fire/trigger next job in chain, if there is any.

That’s all.

3 thoughts on “Chaining job execution in Quartz”

  1. Hi,
    Just wanted to point readers to QuartzDesk ( which provides a GUI for remote Quartz scheduler management and monitoring. One of the core features of QuartzDesk are job chains. With job chains you can easily orchestrate execution of Quartz jobs within a particular Quartz scheduler instance or across multiple Quartz scheduler instances. Job chains can be added and modified at runtime without the need to modify the application code / configuration and without application restarts.

    Free Lite edition, 30-day trial licenses and online demo are available.

    [QuartzDesk Founder]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.