QuartzController.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.example.demo.controller;
  2. import com.example.demo.base.Constants;
  3. import com.example.demo.domain.QrtzTask;
  4. import com.example.demo.model.dto.QuartzTaskDTO;
  5. import com.example.demo.service.JobService;
  6. import com.example.demo.service.QrTaskService;
  7. import com.example.demo.util.Response;
  8. import io.swagger.v3.oas.annotations.Operation;
  9. import io.swagger.v3.oas.annotations.tags.Tag;
  10. import lombok.RequiredArgsConstructor;
  11. import org.quartz.SchedulerException;
  12. import org.springframework.context.annotation.Lazy;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.util.List;
  15. @RestController
  16. @Tag(name = "任务管理")
  17. @RequestMapping(Constants.QUARTZ_API + "/task")
  18. @RequiredArgsConstructor(onConstructor_ = {@Lazy})
  19. public class QuartzController {
  20. private final JobService jobService;
  21. private final QrTaskService qrTaskService;
  22. @Operation(summary = "添加任务")
  23. @PostMapping("/addTask")
  24. public String addTask(@RequestBody QuartzTaskDTO quartzTaskDTO) throws SchedulerException {
  25. //添加任务
  26. QrtzTask qrtzTask = qrTaskService.addQuartzTask(quartzTaskDTO);
  27. //任务添加完成后,判断是定时任务则再由定时任务定时添加该任务
  28. if (quartzTaskDTO.getTaskType().equals(1)) {
  29. jobService.addJob(qrtzTask);
  30. }
  31. return Response.success("添加任务完成");
  32. }
  33. @Operation(summary = "删除任务")
  34. @PostMapping("/deleteTasks")
  35. public String deleteTasks(@RequestBody List<Integer> taskIds) throws SchedulerException {
  36. return Response.success(qrTaskService.deleteQuartzTask(taskIds));
  37. }
  38. @Operation(summary = "查询任务")
  39. @PostMapping("/getTasks")
  40. public Response getTasks(@RequestBody QuartzTaskDTO quartzTaskDTO) {
  41. return Response.success2(qrTaskService.getTasks(quartzTaskDTO));
  42. }
  43. @Operation(summary = "更新任务")
  44. @PostMapping("/update")
  45. public void update(@RequestBody QuartzTaskDTO quartzTaskDTO) throws InterruptedException, SchedulerException {
  46. QrtzTask qrtzTask = qrTaskService.updateQuartzTask(quartzTaskDTO);
  47. jobService.updateJob(qrtzTask);
  48. }
  49. @Operation(summary = "暂停任务")
  50. @GetMapping("/pauseJob")
  51. public void pauseJob(Integer taskId) throws SchedulerException {
  52. jobService.pauseJob(taskId);
  53. }
  54. @Operation(summary = "继续任务")
  55. @GetMapping("/resumeJob")
  56. public void resumeJob(Integer taskId) throws SchedulerException {
  57. jobService.resumeJob(taskId);
  58. }
  59. }