Search code examples
javaspringtriggersquartz

Spring + Quartz: programmatic schedule of one-time, misfire proof jobs


I am working on an enterprise application that uses Quartz 2.2.1. Trigger beans are configured in their XML, which included the relative cron expression.

Now, I have the need of programmatically creating triggers with custom fire times; these triggers also have to handle misfiring (I have to be sure that they get executed, even if the server is down at fire time). Is there a way to make quartz do the 'dirty' work without handling trigger persistency manually?

Thank you.


Solution

  • Easier than I thought, solution lies within the SimpleTrigger class.

    // Update trigger infos

    SimpleTrigger trigger = (SimpleTrigger)builder
                            .startAt( whenever you want it to start )
                            .withSchedule(simpleSchedule()
                                    .withIntervalInMinutes( or any interval ).repeatForever() )
                            .endAt( whenever you want it to end)
                            .build()
    

    This creates a quartz trigger living between start and end, and executing following the specified interval.

    Misfire strategy can be specified as well.