Using @DisallowConcurrentExecution in Quartz scheduler

萝らか妹 提交于 2021-01-01 09:17:08

问题


I am sorry if this question is too naive, I am expecting the jobs to be scheduled so that it executes one by one, and not parallely.It is executed only once.

From docs, @DisallowConcurrentExecution is

  • An annotation that marks a {@link Job} class as one that must not have multiple instances executed concurrently (where instance is based-upon a {@link JobDetail} definition - or in other words based upon a {@link JobKey}).

But when I schedule a job with same JobKey, I am getting Failed to schedule a job org.quartz.ObjectAlreadyExistsException

If I generate a different JobKey, it is not heeding to @DisallowConcurrentExecution and the job is getting executed in parallel(as mentioned in docs).

Please suggest how can I achieve this, any pointers would really help!

PS: I do not know the jobs that would be scheduled. So, I need some method to dynamically link up the jobs,if the job is already running.


回答1:


Same JobKey = same job.
Different JobKey = different job.

Quartz won't let you use the same JobKey more than once because that'd be two jobs with the same key. Like having two users with the same ID.

What you need to do is schedule different JobTriggers for the same JobKey.

@DisallowConcurrentExecution avoids overlapping executions of the same job. If you use a different JobKey, it's not the same job anymore, so the annotation doesn't have any effect. But for a given JobKey with several JobTriggers, @DisallowConcurrentExecution will keep the triggers from launching a new execution of the job, if the previous one hasn't finished yet.

I suggest having a look at Quartz's documentation to get a deeper understanding of the above concepts.



来源:https://stackoverflow.com/questions/55882079/using-disallowconcurrentexecution-in-quartz-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!