Our web app has few scheduled tasks and we like this feature of Spring so much, many have started relying on it. We have a 'pilot' machine which shares the same configuration/db as prod machines. Since this machine points to the same db as prod machines, when it runs a scheduled task - it may affect prod data. Is there a way to not run Spring Scheduled task on this machine? We thought of relying on the machine name, but dont want to introduce a check each time a task starts. Any suggestions?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
With Spring 3.1 Profiles it will be really easy, but here is a way you can do it in Spring 3.0.
In your context:
<task:annotation-driven executor="taskExecutor" scheduler="configScheduler"/>
<task:scheduler id="taskScheduler"/>
<task:executor id="taskExecutor"/>
Use @Bean
to define configScheduler
, using a dummy scheduler if a system property noScheduler
is set.
@Configuration
public class SchedulerConfig {
@Resource(name="taskScheduler")
ThreadPoolTaskScheduler taskScheduler;
@Bean
ThreadPoolTaskScheduler configScheduler() {
ThreadPoolTaskScheduler scheduler =
System.getProperty("noScheduler") == null : taskScheduler ?
new ThreadPoolTaskScheduler() {
@Override public ScheduledFuture schedule(Runnable task, Trigger trigger) { return null; } // Cron
@Override public ScheduledFuture scheduleAtFixedRate(Runnable task, long period) { return null; }
@Override public ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay) { return null; }
};
return scheduler;
}
}
回答2:
With Spring 3.1, you'll get profiles, which might help you out.