我可以通过属性设置关闭quartz调度程序吗?

发布于 2024-12-11 04:02:02 字数 101 浏览 0 评论 0原文

我们通过在 jobs.xml 文件中注释掉调度程序工厂 bean 来在本地禁用quartz 调度程序。

quartz.properties 文件中是否有执行类似操作的设置?

We disable the quartz scheduler locally by commenting out the scheduler factory bean in the jobs.xml file.

Is there a setting for doing something similar in the quartz.properties file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

居里长安 2024-12-18 04:02:02

如果您使用 Spring Framework,您可以从 org.springframework.scheduling.quartz.SchedulerFactoryBean 创建子类并覆盖 afterPropertiesSet() 方法。

public class MySchedulerFactoryBean extends org.springframework.scheduling.quartz.SchedulerFactoryBean {

    @Autowired
    private @Value("${enable.quartz.tasks}") boolean enableQuartzTasks;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (enableQuartzTasks) {
            super.afterPropertiesSet();
        } 
    }
}

然后更改 xml 文件中工厂的声明,并在属性文件中设置“enable.quartz.tasks”属性。就这样。

当然,您可以编写和使用setter方法,然后添加

<property name="enableQuartzTasks" value="${enable.quartz.tasks}"/>

到xml中的MySchedulerFactoryBean声明中,而不是使用@Autowired。

If you use Spring Framework you can make subclass from org.springframework.scheduling.quartz.SchedulerFactoryBean and override afterPropertiesSet() method.

public class MySchedulerFactoryBean extends org.springframework.scheduling.quartz.SchedulerFactoryBean {

    @Autowired
    private @Value("${enable.quartz.tasks}") boolean enableQuartzTasks;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (enableQuartzTasks) {
            super.afterPropertiesSet();
        } 
    }
}

Then change declaration of factory in xml file and set "enable.quartz.tasks" property in properties file. That's all.

Of course, instead using @Autowired you can write and use setter method and add

<property name="enableQuartzTasks" value="${enable.quartz.tasks}"/>

to MySchedulerFactoryBean declaration in xml.

我是有多爱你 2024-12-18 04:02:02

看来org.springframework.scheduling.quartz.SchedulerFactoryBean中有一个属性autoStartup。所以你可以像这样在 XML 配置中配置它:

<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup" value="${cron.enabled}"/>
    <property name="triggers">
        <list>
            <ref bean="someTriggerName"/>
        </list>
    </property>
</bean>

感谢 https ://chrisrng.svbtle.com/configure-spring-to-turn-quartz-scheduler-onoff

It seems that there is a property autoStartup in org.springframework.scheduling.quartz.SchedulerFactoryBean. So you can configure it in XML config like this:

<bean id="quartzFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup" value="${cron.enabled}"/>
    <property name="triggers">
        <list>
            <ref bean="someTriggerName"/>
        </list>
    </property>
</bean>

Thanks to https://chrisrng.svbtle.com/configure-spring-to-turn-quartz-scheduler-onoff

凉月流沐 2024-12-18 04:02:02

不。但是属性文件不会启动调度程序。

除非某些代码调用scheduler.start(),否则调度程序不会启动。

No. But the properties file doesn't start the scheduler.

The scheduler doesn't start until/unless some code invokes scheduler.start().

凯凯我们等你回来 2024-12-18 04:02:02

如果您使用 Spring Framework 3.1 创建和启动 Quartz Scheduler,则可以禁用 Quartz Scheduler。
在我的 Spring 配置文件中,我以这种方式使用 Spring 3.1 的新配置文件功能:

<beans profile="production,test">
    <bean name="bookingIndexerJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.xxx.indexer.scheduler.job.BookingIndexerJob" />
        <property name="jobDataAsMap">
            <map>
                <entry key="timeout" value="10" />
            </map>
        </property>
    </bean>

    <bean id="indexerSchedulerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="bookingIndexerJob" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="5000" />
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="indexerSchedulerTrigger" />
            </list>
        </property>
        <property name="dataSource" ref="ds_quartz-scheduler"></property>
        <property name="configLocation" value="classpath:quartz.properties" />
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    </bean>
</beans>

仅当我想启动 Scheduler 时(例如在生产环境中),我设置 spring.profiles.active系统属性,包含活动配置文件列表:

-Dspring.profiles.active="product"

更多信息:

You can disable Quartz Scheduler if you use Spring Framework 3.1 for creating and starting it.
On my Spring configuration file I use the new profiles feature of Spring 3.1 in this way:

<beans profile="production,test">
    <bean name="bookingIndexerJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="com.xxx.indexer.scheduler.job.BookingIndexerJob" />
        <property name="jobDataAsMap">
            <map>
                <entry key="timeout" value="10" />
            </map>
        </property>
    </bean>

    <bean id="indexerSchedulerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="bookingIndexerJob" />
        <property name="startDelay" value="1000" />
        <property name="repeatInterval" value="5000" />
    </bean>

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="indexerSchedulerTrigger" />
            </list>
        </property>
        <property name="dataSource" ref="ds_quartz-scheduler"></property>
        <property name="configLocation" value="classpath:quartz.properties" />
        <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    </bean>
</beans>

Only when I want to start the Scheduler (for example on the production environment), I set the spring.profiles.active system property, with the list of active profiles:

-Dspring.profiles.active="production"

More info here:

恋你朝朝暮暮 2024-12-18 04:02:02

我个人喜欢 Demis Gallisto 的答案。如果您可以使用配置文件,这将是我的建议。

如今,人们很可能更喜欢使用注释,因此作为他的答案的补充。

@Configuration
@Profile({ "test", "prod" })
public class SchedulerConfig {
  @Bean
  // ... some beans to setup your scheduler
}

仅当配置文件 testprod 处于活动状态时,才会触发调度程序。因此,如果您设置不同的配置文件,例如 -Dspring.profiles.active=dev 则不会发生任何情况。

如果由于某些原因您无法使用配置文件方法,例如配置文件重叠...

miso.belica 的解决方案似乎也去工作。

定义一个属性。例如在 application.properties 中:dailyRecalculationJob.cron.enabled=false 并在您的 SchedulerConfig 中使用它。

@Configuration
public class SchedulerConfig {
  @Value("${dailyRecalculationJob.cron.enabled}")
  private boolean dailyRecalculationJobCronEnabled;

  @Bean
  public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger trigger) throws 
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setAutoStartup(dailyRecalculationJobCronEnabled);
    // ...
    return factory;
  }
  // ... the rest of your beans to setup your scheduler
}

I personally like the answer from Demis Gallisto. If you can work with profiles, this would be my recommendation.

Nowadays people most likely prefer to work with Annotations, so as an addition to his answer.

@Configuration
@Profile({ "test", "prod" })
public class SchedulerConfig {
  @Bean
  // ... some beans to setup your scheduler
}

This will trigger the scheduler only when the profile test OR prod is active. So if you set an different profile, e.g. -Dspring.profiles.active=dev nothing will happen.

If for some reasons you cannot use the profile approach, e.g. overlap of profiles ...

The solution from miso.belica seems also to work.

Define a property. e.g. in application.properties: dailyRecalculationJob.cron.enabled=false and use it in your SchedulerConfig.

@Configuration
public class SchedulerConfig {
  @Value("${dailyRecalculationJob.cron.enabled}")
  private boolean dailyRecalculationJobCronEnabled;

  @Bean
  public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, Trigger trigger) throws 
    SchedulerFactoryBean factory = new SchedulerFactoryBean();
    factory.setAutoStartup(dailyRecalculationJobCronEnabled);
    // ...
    return factory;
  }
  // ... the rest of your beans to setup your scheduler
}
〆一缕阳光ご 2024-12-18 04:02:02

我有类似的问题:在测试范围中禁用调度程序。
这是我的 applicationContext.xml 的一部分

<task:annotation-driven scheduler="myScheduler" />
<task:scheduler id="myScheduler" pool-size="10" />

,我已经使用“primary”属性和 Mockito 禁用了调度程序。这是我的 applicationContext-test.xml

<bean id="myScheduler" class="org.mockito.Mockito" factory-method="mock" primary="true">
     <constructor-arg value="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"/>
</bean>

希望有帮助!

I had similar issue: disable scheduler in test scope.
Here is part of my applicationContext.xml

<task:annotation-driven scheduler="myScheduler" />
<task:scheduler id="myScheduler" pool-size="10" />

And I've disabled scheduler using 'primary' attribute and Mockito. Here is my applicationContext-test.xml

<bean id="myScheduler" class="org.mockito.Mockito" factory-method="mock" primary="true">
     <constructor-arg value="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler"/>
</bean>

Hope this help!

一个人的旅程 2024-12-18 04:02:02

我在 Spring Boot 上下文中发现的最简单的测试方法就是:

@MockBean
Scheduler scheduler;

The simplest way I've found in a spring boot context for tests is to simply:

@MockBean
Scheduler scheduler;
姐不稀罕 2024-12-18 04:02:02

这个scala代码有效:

 @Bean
 def schedulerFactoryBean(): SchedulerFactoryBean = {
   new SchedulerFactoryBean {
     override def afterPropertiesSet(): Unit = {}
   }
 }

This scala code works:

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