views:

134

answers:

2
+1  Q: 

Quartz job tunning

hello there is something i've realized with quartz when working.Say a cron is set to wake up every 2min with the expression 0 0/2 * * * ? .

When you run the project at say 13:10:30, the first action happens at 13:12:00 and the second 13:14:00 and every 2min 0 second for the rest. Obviously between the startup of the project and the first occurence of the action there have been 1mn:30s only.

Is there a way to for the first occurrence to respect the 2min no matter which at seconds the project starts?

A: 

Quartz may use cron for the scheduling, which is based on date and time, not duration. This means that the cron expression you define is directly related to the current time on the machine, not on when the application started.

I am not aware of a Quartz configuration that will help you to solve your problem. However, a solution is to create your own Thread, which started during the launch of your application and that basically waits 2 minutes before calling a method:

while (running) {
    Thread.sleep(1000 * 120);
    doStuff();
}
romaintaz
man you just gave me an idea! thanks but i'll dig a bit more on a way to do it properly.
black sensei
Quartz is not based on cron. It supports the syntax, but does not require it.
skaffman
edited to reflect that point.
romaintaz
+2  A: 

Cron jobs are configured in Quartz using the CronTrigger class. The alternative is to use SimpleTrigger, which you can construct using fixed delay intervals. SimpleTrigger has various constructors, allowing you to specify the start time, end time, number of repeats, repeat interval, and so on.

Having said that, I'd recommend against using Quartz for this kind of scheduling, and use java.util.concurrent.Executors.newScheduledThreadPool(). It's much easier than Quartz when it comes to simple repeating tasks.

skaffman