その作品:
@Controller
@RequestMapping("/scheduler")
public class SchedulerController {
@Autowired
AbstractApplicationContext context;
@Autowired
ThreadPoolTaskScheduler scheduler;
@RequestMapping("/stop")
@ResponseBody
public String stop() {
// scheduler.shutdown();
BeanDefinitionRegistry factory = (BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();
factory.removeBeanDefinition("MyScheduler");
return "stoped";
}
}
@Service("MyScheduler")
@EnableScheduling
public class MyScheduler {
@Scheduled(cron = "0/10 * * * * ?")
public void schedule() {
System.out.println("> Running scheduler .... " + System.currentTimeMillis());
}
}
代替は、いくつかのフラグを「有効にする」スケジューラに追加しました:
@Service("MyScheduler")
@EnableScheduling
public class MyScheduler {
boolean enable = true;
@Scheduled(cron = "0/10 * * * * ?")
public void schedule() {
if(enable){
System.out.println("> Running scheduler .... " + System.currentTimeMillis());
}
}
}
そして、有効または無効にすることが可能に簡単ですスケジューラ。