开发者

Task scheduling with Spring with cron data stored in entities

I would like to run cron tasks based on an entity, lets call that entity TaskEntity

The TaskEntity can have 开发者_如何转开发info about what needs to be done (domain specific) and when. The when part can be specified in cron based manner

Initially I thought of looking into Quartz but I could not find clear examples

I am not sure how to proceed from here. I just want to give the end user the ability to add as many tasks as they want on run time. If the end user changes a task (ie disabled it, deletes it or changes the time) then it should behave accordingly.


I think the easiest solution is to use a Scheduler.

On startup, read all your entities and calculate the next start time of a task.

When that time is reached, fire off the task, calculate the next starting time and go to sleep. You have to listen to updates on your entities to recalculate the next starting time and reset the scheduler if necessary.

You can user org.quartz.Scheduler for that. Check the documentation for details: http://www.quartz-scheduler.org/docs/index.html

It will look something like this. (Your Job must implement the org.quartz.Job interface)

Trigger trigger = new SimpleTrigger( "jobname", "scheduler group name", dateTime );
scheduler.scheduleJob( jobDetail, trigger );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜