3

Or, failing that, removing a task from the scheduler and adding a new one.

I'm afraid this is a part of Spring I'm more or less 100% ignorant on, and spending an afternoon reading articles about it hasn't elucidated things much.

Basically, I have a program with three scheduled tasks, one that runs every 500ms and two that run based on cron expressions. My customers want the ability to change those cron expressions on the fly.

Near as I can tell this means I need to take the cron expressions out of the application context and move them to the database. That part is easy. The hard part is figuring out how to get a reference to the already existing scheduled task in Spring and change when it is next supposed to run. Every article I've found on this subject just describes how to set up application context or annotations.

The only answer I've been able to find would only work for regular intervals (and, according to the comments, may not actually work). I'm hoping to not have to write something that interprets Cron expressions, so I'm considering that solution "plan B".

Thanks for any help!

Community
  • 1
  • 1
John M
  • 310
  • 4
  • 15
  • Are you working with `@Scheduled` methods? – Sotirios Delimanolis Jul 10 '14 at 15:33
  • The ones in use right now are scheduled in the applicationContext.xml, rather than using the annotations. I could switch to the annotations, but I still wouldn't know how to change the periodicity at runtime. – John M Jul 10 '14 at 15:34
  • 1
    For dynamic tasks, I wouldn't use this mechanism. Consider using a `ExecutorService` and submitting the tasks yourself. Or better yet, use quartz and manage the tasks through that. [Spring doesn't let you play with scheduled tasks](http://stackoverflow.com/questions/21791853/how-are-spring-taskscheduled-objects-represented-at-runtime). – Sotirios Delimanolis Jul 10 '14 at 15:37
  • Possible duplicate of [Scheduling a job with Spring programmatically (with fixedRate set dynamically)](http://stackoverflow.com/questions/14630539/scheduling-a-job-with-spring-programmatically-with-fixedrate-set-dynamically) – Steve Chambers Oct 05 '15 at 09:54

1 Answers1

0

You should be able to use the Quartz scheduler API to manipulate jobs and their triggers.

In your Spring applicationContext you most likely have a scheduler bean (org.springframework.scheduling.quartz.SchedulerFactoryBean) and in your application you can invoke various methods on this bean to:

  • Get a list of all jobs (see getJobGroupNames + getJobKeys + getJobDetail methods)
  • Get a list of all triggers (see getTriggerGroupNames + getTriggerKeys + getTrigger methods)
  • Get a list of triggers of a particular job (see getTriggersOfJob method)
  • Update trigger definition (see rescheduleJob method)
  • etc.

Please refer to http://quartz-scheduler.org/api/2.2.0/ for the API details. It is not clear what Quartz scheduler version you are using, but the API is not so much different for other versions.

You may also consider using an external Quartz scheduler GUI that allows you to do this a more and you do not have to code this logic into the application. You may want to check QuartzDesk (of which I am the original author so I am a bit biased here), or other similar products like (just google for "quartz scheduler gui").

Jan Moravec
  • 1,808
  • 1
  • 15
  • 18