How to restart scheduled task on runtime with EnableScheduling annotation in spring?

后端 未结 4 511
南笙
南笙 2021-01-01 21:25

I have been investigating how to change the frequency of a job on runtime with Java 8 and spring. This question was very useful but it did not totally solve my issue.

<
4条回答
  •  别那么骄傲
    2021-01-01 22:02

    One simple approach is to only ever add new tasks, not to try and cancel or restart the scheduler.

    Each time the configuration changes, just add a new task with its new configuration.

    Then, whenever a task runs, it must first check some state (by querying database, or lookup up in a concurrent map, or whatever) to decide if it is the latest version. If it is, then it should proceed. Otherwise, it should end immediately.

    The only downside is that if you are changing job configuration frequently compared to how often they run, then of course the list of scheduled tasks will keep growing in memory.

提交回复
热议问题