如何配置多个 Quartz 单线程作业

发布于 2024-11-28 16:52:22 字数 1576 浏览 6 评论 0原文

我有两个不同的工作,必须同时触发。

我想为他们每个人提供一个单独的线程。 当然,我可以将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-12-05 16:52:22

我的建议是您使用不同的 SchedulerFactoryBean。

My recommendation is that you use different SchedulerFactoryBeans.

极度宠爱 2024-12-05 16:52:22

我的建议是使用 @DisallowConcurrentExecution 注释您的作业类。请参阅Quartz 文档

My recommendation is that annotate your job class with @DisallowConcurrentExecution. See the Quartz documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文