java - 按需停止和启动springboot调度器服务
问题描述
我按照下面的文章来启动和停止调度程序。
https://stackoverflow.com/a/50216003/4770397
@RestController
@RequestMapping("/test")
public class TestController {
private static final String SCHEDULED_TASKS = "scheduledTasks";
@Autowired
private ScheduledAnnotationBeanPostProcessor postProcessor;
@Autowired
private ScheduledTasks scheduledTasks;
@Autowired
private ObjectMapper objectMapper;
@GetMapping(value = "/stopScheduler")
public String stopSchedule(){
postProcessor.postProcessBeforeDestruction(scheduledTasks, SCHEDULED_TASKS);
return "OK";
}
@GetMapping(value = "/startScheduler")
public String startSchedule(){
postProcessor.postProcessAfterInitialization(scheduledTasks, SCHEDULED_TASKS);
return "OK";
}
@GetMapping(value = "/listScheduler")
public String listSchedules() throws JsonProcessingException{
Set<ScheduledTask> setTasks = postProcessor.getScheduledTasks();
if(!setTasks.isEmpty()){
return objectMapper.writeValueAsString(setTasks);
}else{
return "No running tasks !";
}
}
但是postProcessor.postProcessBeforeDestruction()
方法在我的项目中不可用。
请帮忙。
解决方案
推荐阅读
- cloud - 可扩展的虚拟主机解决方案
- angular - Firestore/ionic 错误 - 调用 .map 时方法 snapshotChanges 返回未定义
- user-interface - javafx 按钮从 txt 读取行到文本字段
- ansible - 执行 MFA 政策后无法担任角色
- javascript - 如何将数组 json 插入到其他 const 的索引中?
- java - 从资源文件加载目录并将文件加载到列表中
- swift - 在 Swift 5.0 中从 AppDelegate 实例化窗口失败
- ruby-on-rails - 通过 Ruby on Rails 中的 HTTP 标头自定义授权
- terminal - 如何在两个终端之间通话,一个使用 Ncat,一个使用 Nping?
- ios - 点击segmentedControl时SwiftUI关闭键盘