Monday, July 12, 2010

How to implement a unparallel job and dynamically add another trigger

Sometimes, a job is not allow to run parallel, e.g. a file loading task. It is not a good idea to allow multiple job to load a same file in same time and it will lead to create duplicate records in table.

How to implement a job to exclusively load the data from a file? Quartz provides a stateful interface and we can use it to implement a stateful job to avoid run a same job parallelly. For a stateful job, one job must wait another finished and then to run.

If we need to dynamically add a trigger to fire a job immediately exclusively, how to implement it? First, use the stateful job interface to avoid the same job running parallelly. Then we create a new trigger and set proper job group and job name (the name of job you want to run) and schedule it by scheduleJob() method of scheduler.