java 当有多个quartz线程时,让quartz只在一个线程中执行一项工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Having quartz execute a job only in one thread when there are multiple quartz threads
提问by John
I was wondering if one can configure quartz to execute a long processing job run only in one thread at any given time. In another words, say I have quartz configured with a SimpleThreadPool of size 5. And I have a job that fires every 10 seconds but that could take longer than 10 seconds to complete in certain situations. Is there a way to configure quartz trigger/job/scheduler so that this trigger won't fire again as it is already in a running state in another thread. When the trigger fires again, another thread from the pool will pick it up and have two instances of the same job run at the same time. Thanks for your input.
我想知道是否可以将石英配置为在任何给定时间仅在一个线程中执行长处理作业。换句话说,假设我为quartz 配置了一个大小为5 的SimpleThreadPool。我有一个作业每10 秒触发一次,但在某些情况下可能需要10 秒以上才能完成。有没有办法配置石英触发器/作业/调度程序,以便此触发器不会再次触发,因为它已经在另一个线程中处于运行状态。当触发器再次触发时,池中的另一个线程将拿起它并同时运行同一作业的两个实例。感谢您的输入。
Clarification: (for the suggestions about using a threadpool of size 1). Requirement is to configure the threadpool with 5 threads and have any single job to execute only in a single thread at any given time, in other words an instance of a job should be executed by only one thread.
说明:(关于使用大小为 1 的线程池的建议)。要求是将线程池配置为 5 个线程,并且任何单个作业在任何给定时间都只能在单个线程中执行,换句话说,一个作业的实例应该只由一个线程执行。
回答by jhouse
If you're using Quartz 1.x make the Job class implement StatefulJob
. If you're using Quartz 2.x then add the @DisallowConcurrentExecution
annotation to the job class.
如果您使用的是 Quartz 1.x,请让 Job 类实现StatefulJob
. 如果您使用的是 Quartz 2.x,则将@DisallowConcurrentExecution
注释添加到作业类。
回答by blob
set
放
org.quartz.threadPool.threadCount=1
There will be a single quartz worker thread at a time
一次将有一个石英工作线程