spring 我可以使用属性设置关闭石英调度程序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7818875/
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
Can I turn off quartz scheduler with a property setting?
提问by blank
We disable the quartz scheduler locally by commenting out the shceduler factory bean in the jobs.xml file. Is there a setting for doing something similar in the quartz.properties file?
我们通过注释掉jobs.xml 文件中的shceduler factory bean 来在本地禁用quartz 调度程序。在quartz.properties 文件中是否有类似的设置?
回答by ?ukasz Kmiecik
If you use Spring Framework you can make subclass from org.springframework.scheduling.quartz.SchedulerFactoryBean and override afterPropertiesSet() method.
如果您使用 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();
}
}
}
Then change declaration of factory in xml file and set "enable.quartz.tasks" property in properties file. That's all.
然后更改 xml 文件中工厂的声明并在属性文件中设置“enable.quartz.tasks”属性。就这样。
Of course, instead using @Autowired you can write and use setter method and add
当然,代替使用@Autowired,您可以编写和使用setter 方法并添加
<property name="enableQuartzTasks" value="${enable.quartz.tasks}"/>
to MySchedulerFactoryBean declaration in xml.
到 xml 中的 MySchedulerFactoryBean 声明。
回答by jhouse
No. But the properties file doesn't start the scheduler.
否。但属性文件不会启动调度程序。
The scheduler doesn't start until/unless some code invokes scheduler.start().
调度程序直到/除非某些代码调用 scheduler.start() 才会启动。
回答by Mi?o
Seems there is property autoStartupin org.springframework.scheduling.quartz.SchedulerFactoryBeanso you can configure it in xml config like this:
似乎有属性autoStartup,org.springframework.scheduling.quartz.SchedulerFactoryBean因此您可以在 xml config 中配置它,如下所示:
<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>
<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
感谢https://chrisrng.svbtle.com/configure-spring-to-turn-quartz-scheduler-onoff
回答by Demis Gallisto
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:
如果您使用 Spring Framework 3.1 来创建和启动 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>
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:
只有当我想启动调度程序时(例如在生产环境中),我才设置“spring.profiles.active”系统属性,以及活动配置文件的列表:
-Dspring.profiles.active="production"
-Dspring.profiles.active="生产"
More info here:
更多信息在这里:
http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/
回答by morecore
I personally like the answer from Demis Gallisto. If you can work with profiles, this would be my recommendation.
我个人喜欢Demis Gallisto的回答。如果您可以使用配置文件,这将是我的建议。
Nowadays people most likely prefer to work with Annotations, so as an addition to his answer.
现在人们最有可能更喜欢使用 Annotations,作为他答案的补充。
@Configuration
@Profile({ "test", "prod" })
public class SchedulerConfig {
@Bean
// ... some beans to setup your scheduler
}
This will trigger the scheduler only when the profile testOR prodis active. So if you set an different profile, e.g. -Dspring.profiles.active=devnothing will happen.
这只会在配置文件testORprod处于活动状态时触发调度程序。因此,如果您设置了不同的配置文件,例如-Dspring.profiles.active=dev什么都不会发生。
If for some reasons you cannot use the profile approach, e.g. overlap of profiles ...
如果由于某些原因您不能使用配置文件方法,例如配置文件的重叠...
The solution from miso.belicaseems also to work.
来自miso.belica的解决方案似乎也有效。
Define a property. e.g. in application.properties: dailyRecalculationJob.cron.enabled=falseand use it in your SchedulerConfig.
定义一个属性。例如在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
}
回答by Jason
the simplest way I've found in a spring boot context for tests is to simply:
@MockBean
Scheduler scheduler;
我在用于测试的 Spring Boot 上下文中找到的最简单的方法是:
@MockBean
Scheduler scheduler;
回答by Vladimir Filipchenko
I had similar issue: disable scheduler in test scope. Here is part of my applicationContext.xml
我有类似的问题:在测试范围内禁用调度程序。这是我的 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
我已经使用“主要”属性和 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>
Hope this help!
希望这有帮助!

