Schedule a job using a custom trigger and options

I use the Grails Quartz plugin and want to schedule my tasks using a software-generated trigger. I do not know in advance what the execution interval will be. I want the task to be carried out endlessly.

The docs provide some examples on how to schedule / run tasks:

== Dynamic work planning ==

Starting with version 0.4.1, you have the ability to dynamically schedule tasks.

The following methods are available:

  • MyJob.schedule(String cronExpression, Map params?) Creates a cron trigger;
  • MyJob.schedule(Long repeatInterval, Integer repeatCount?, Map params?) Creates a simple trigger: repeats the task repeatCount + 1 times with a delay of repeatInterval milliseconds;
  • MyJob.schedule(Date scheduleDate, Map params?) one task to a specific date;
  • MyJob.schedule(Trigger trigger) schedules the task using a custom trigger;
  • MyJob.triggerNow(Map params?) job.

    Each method (except one for a custom trigger) takes an optional argument "params". You can use it to transfer some data to your work, and then access it from the job.

Print Version Grails 1.3.7 Quartz Plugin version 0.4.2

So why does MyJob.schedule(Trigger trigger) not accept parameters? And how can I achieve what I want using a custom trigger and map or options for the job?

+4
source share
2 answers

If you look where these methods are defined in the Quartz source code of the plugin , you can see that all functions that accept Map params are shells that create Trigger and then run it in the scheduler.

The MyJob.schedule(Trigger trigger) method just fires the trigger that you pass, so you need to add your parameters to the Triggers property jobDataMap before you call this method, jobDataMap .:

 trigger.jobDataMap.putAll [foo:"It Works!"] MyJob.schedule( trigger ) 
+3
source

The answer to the question is correct, it is slightly updated here.

 Trigger trigger = TriggerBuilder .newTrigger() .startNow() .withIdentity("triggerName", "groupName") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5000).repeatForever() ).build(); trigger.jobDataMap.putAll([foo:"bar"]) MyJob.schedule(trigger) 
+1
source

Source: https://habr.com/ru/post/1390612/


All Articles