4.9、编写 contoller 服务
- 先创建一个请求参数实体类
public class QuartzConfigDTO implements Serializable { private static final long serialVersionUID = 1L; /** * 任务名称 */ private String jobName; /** * 任务所属组 */ private String groupName; /** * 任务执行类 */ private String jobClass; /** * 任务调度时间表达式 */ private String cronExpression; /** * 附加参数 */ private Map<String, Object> param; public String getJobName() { return jobName; } public QuartzConfigDTO setJobName(String jobName) { this.jobName = jobName; return this; } public String getGroupName() { return groupName; } public QuartzConfigDTO setGroupName(String groupName) { this.groupName = groupName; return this; } public String getJobClass() { return jobClass; } public QuartzConfigDTO setJobClass(String jobClass) { this.jobClass = jobClass; return this; } public String getCronExpression() { return cronExpression; } public QuartzConfigDTO setCronExpression(String cronExpression) { this.cronExpression = cronExpression; return this; } public Map<String, Object> getParam() { return param; } public QuartzConfigDTO setParam(Map<String, Object> param) { this.param = param; return this; } }
- 编写 web 服务接口
@RestController @RequestMapping("/test") public class TestController { private static final Logger log = LoggerFactory.getLogger(TestController.class); @Autowired private QuartzJobService quartzJobService; /** * 添加新任务 * @param configDTO * @return */ @RequestMapping("/addJob") public Object addJob(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.addJob(configDTO.getJobClass(), configDTO.getJobName(), configDTO.getGroupName(), configDTO.getCronExpression(), configDTO.getParam()); return HttpStatus.OK; } /** * 暂停任务 * @param configDTO * @return */ @RequestMapping("/pauseJob") public Object pauseJob(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.pauseJob(configDTO.getJobName(), configDTO.getGroupName()); return HttpStatus.OK; } /** * 恢复任务 * @param configDTO * @return */ @RequestMapping("/resumeJob") public Object resumeJob(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.resumeJob(configDTO.getJobName(), configDTO.getGroupName()); return HttpStatus.OK; } /** * 立即运行一次定时任务 * @param configDTO * @return */ @RequestMapping("/runOnce") public Object runOnce(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.runOnce(configDTO.getJobName(), configDTO.getGroupName()); return HttpStatus.OK; } /** * 更新任务 * @param configDTO * @return */ @RequestMapping("/updateJob") public Object updateJob(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.updateJob(configDTO.getJobName(), configDTO.getGroupName(), configDTO.getCronExpression(), configDTO.getParam()); return HttpStatus.OK; } /** * 删除任务 * @param configDTO * @return */ @RequestMapping("/deleteJob") public Object deleteJob(@RequestBody QuartzConfigDTO configDTO) { quartzJobService.deleteJob(configDTO.getJobName(), configDTO.getGroupName()); return HttpStatus.OK; } /** * 启动所有任务 * @return */ @RequestMapping("/startAllJobs") public Object startAllJobs() { quartzJobService.startAllJobs(); return HttpStatus.OK; } /** * 暂停所有任务 * @return */ @RequestMapping("/pauseAllJobs") public Object pauseAllJobs() { quartzJobService.pauseAllJobs(); return HttpStatus.OK; } /** * 恢复所有任务 * @return */ @RequestMapping("/resumeAllJobs") public Object resumeAllJobs() { quartzJobService.resumeAllJobs(); return HttpStatus.OK; } /** * 关闭所有任务 * @return */ @RequestMapping("/shutdownAllJobs") public Object shutdownAllJobs() { quartzJobService.shutdownAllJobs(); return HttpStatus.OK; } }
4.10、服务接口测试
运行 SpringBoot 的Application
类,启动服务!
创建一个每5秒钟执行一次的定时任务