Announcement Announcement Module
No announcement yet.
Dynamic Jobs and rescheduling Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Jobs and rescheduling

    I am pretty new to Spring, so i have lots of questions.

    Hi, i have following requirements:

    1. Jobs descriptions are stored in database, including CRON expression and params for the Task.
    2. New jobs can be added through UI, so they should be fetched from DB and need to be run.
    3. Any already running and existing job's CRON expression can be changed through UI, so it should be rescheduled after it is changed.

    So my plan was to make one constant job that runs for example each minute and checks if there are any changes in database. It is simple. And if there is, it should run a new ones jobs/tasks and reschedule already existing ones if they were changed. How can i do that?

    I was reading Spring documentation and found that i could use

    to run new dynamic tasks from DB.


    TaskScheduler taskScheduler;
    String cronExpFromDb = readCronExpFromDb(jobId);
    ScheduledFuture schedule = getTaskScheduler().schedule(new Task(jobId), new CronTrigger(cronExpFromDb));
    Where Task is my runnable job.

    Then i thought that i could hold all those running ScheduledFutures in case i need to cancel them or reschedule.
    But when it comes to reschedule with a new CRON expr, i am confused about how can i do it... How can i reschedule ScheduledFuture and rerun it with new CRON expr?

    I started thinking that if CRON expr is changed in DB, and if i can't reschedule it, i can just cancel it and run new Task with new CRON expr. BUT here comes another question: what happens to an old ScheduledFuture if it was canceled? It stays somewhere... How can i destroy it then so it won't stay in mem?

    May be there is a better solution for my requirments, any help is appritiated!

  • #2
    Why are you reinventing the wheel?! Quartz with a database store can already do this out-of-the-box so why implement your own solution?!


    • #3
      Ok. Thanks, i will try search for info about it.
      Have never been using it, that's why.


      • #4
        You can use the task abstraction but use quartz underneath that basically gives you the best of both worlds. More information on quartz and check the reference guide for the integration of the two. We used in in multiple projects to achieve just what you want (by using a database as the store that way you can simply insert/modify entries quartz has some what of an API for this).