11
votes

I am trying to integrate a Quartz job in my spring application. I got this example from here. The example shows jobs executing at repeated intervals using a simpletrigger and at a specific time using a crontrigger.

My requirement is to run the job only once on application startup. I removed the property repeatInterval, but the application throws an exception :

org.quartz.SchedulerException: Repeat Interval cannot be zero

Is there any way to schedule a job just once ?

Thanks..

2

2 Answers

7
votes

Found the answer here

Ignoring the repeatInterval and setting repeatCount = 0 does what I wanted.

1
votes

Spring SimpleTriggerFactoryBean does the job: if you don't specify the start time, it will set it to 'now'.

Yet I think that long-running one-time job should be considered an anti-pattern, since it will not work even in 2-node cluster: if the node that runs the job goes down, there will be no one that would restart the job.

I prefer to have a job that repeats e.g. every hour, but annotated with @DisallowConcurrentExecution. This way you guarantee that precisely one job will be running, both when the node that originally hosted the job is up, and after it goes down.