如何配置多个 Quartz 单线程作业
我有两个不同的工作,必须同时触发。
我想为他们每个人提供一个单独的线程。 当然,我可以将 Quartz 配置为仅使用一个线程,设置属性
org.quartz.threadPool.threadCount = 1
但这意味着两个作业将使用同一线程。如果我设置threadCount = 2,有可能第一个作业会被触发两次,而另一个作业会等待。
那么,我怎样才能在单独的线程中独立运行这些作业呢?
我的Spring配置是这样的:
<bean name="Job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="test.job1"/>
</bean>
<bean id="CronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="Job1"/>
<property name="cronExpression" value="0 * 6-21 * * ?" />
</bean>
<bean name="Job2" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="test.job2"/>
</bean>
<bean id="CronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="Job2"/>
<property name="cronExpression" value="0 * 6-21 * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown">
<value>true</value>
</property>
<property name="configLocation">
<value>classpath:quartz.properties</value>
</property>
<property name="triggers">
<list>
<ref bean="CronTrigger1"/>
<ref bean="CronTrigger2"/>
</list>
</property>
</bean>
I have 2 different jobs, what must be triggered in the same time.
I'd like to give a separate thread for every of them.
Of course i can configure Quartz to use only one thread, setting property
org.quartz.threadPool.threadCount = 1
But it means, that both jobs will be using the same thread. If I set threadCount = 2, it is possible that the first job will be triggered twice, and the other job will wait.
So, how could I run these jobs in separate threads independently?
My Spring configuration is like that:
<bean name="Job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="test.job1"/>
</bean>
<bean id="CronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="Job1"/>
<property name="cronExpression" value="0 * 6-21 * * ?" />
</bean>
<bean name="Job2" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="test.job2"/>
</bean>
<bean id="CronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="Job2"/>
<property name="cronExpression" value="0 * 6-21 * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method="destroy">
<property name="waitForJobsToCompleteOnShutdown">
<value>true</value>
</property>
<property name="configLocation">
<value>classpath:quartz.properties</value>
</property>
<property name="triggers">
<list>
<ref bean="CronTrigger1"/>
<ref bean="CronTrigger2"/>
</list>
</property>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是您使用不同的 SchedulerFactoryBean。
My recommendation is that you use different SchedulerFactoryBeans.
我的建议是使用 @DisallowConcurrentExecution 注释您的作业类。请参阅Quartz 文档。
My recommendation is that annotate your job class with @DisallowConcurrentExecution. See the Quartz documentation.