• Springboot 整合 xxljob 动态API调度任务(进阶篇)


    前言

    之前写了一篇 xxljob的新手入门篇:


    Springboot 整合 xxljob 使用定时任务调度(新手入门篇)_小目标青年的博客-CSDN博客

    这一篇非常非常简单,就是非常快速的springboot整合 xxljob,相当于拿来即用,能够通过页面配合代码去实现定时任务的使用。

    这一篇,我们将在上一篇的基础上,做一些进阶使用,实现动态地调度定时任务。

    我们平时工作经常会遇到这些业务使用场景(举例):

    执行某个业务后,需要产生一个定时任务;

    怎么怎么判断成功后,需要停止某个任务;

    怎么怎么判断符合条件后,需要重新执行某个任务;

    怎么怎么....移除某个任务;

    摊牌了,实现的效果就是:

    通过API方式(或者方法函数),我们动态随意地去 增删改查、设置定时规则等等去调度任务。

    上一篇几乎是基于HTML页面去对任务创建、启动、停在、删除等等, 非常需要有人去处理,这一篇就是解放我们双手!

    正文

    惯例,瞎话一张简图:

    大致就是admin上面写一些开放接口,各个接入xxl job的demo服务都能通过接口调用,完成动态调度,至于啥时候调度,看自己的业务场景,自己使用。

    ① admin 服务 加接口,其实说实话,原先也提供了很多api接口,但是我这次非得自己搞一下。

     提供的接口:

     MyDynamicApiController.java :
     

    1. import com.xxl.job.admin.controller.annotation.PermissionLimit;
    2. import com.xxl.job.admin.core.cron.CronExpression;
    3. import com.xxl.job.admin.core.model.XxlJobInfo;
    4. import com.xxl.job.admin.core.model.XxlJobQuery;
    5. import com.xxl.job.admin.core.thread.JobScheduleHelper;
    6. import com.xxl.job.admin.core.util.I18nUtil;
    7. import com.xxl.job.admin.service.LoginService;
    8. import com.xxl.job.admin.service.XxlJobService;
    9. import com.xxl.job.core.biz.model.ReturnT;
    10. import org.slf4j.Logger;
    11. import org.slf4j.LoggerFactory;
    12. import org.springframework.beans.factory.annotation.Autowired;
    13. import org.springframework.web.bind.annotation.*;
    14. import javax.servlet.http.HttpServletRequest;
    15. import javax.servlet.http.HttpServletResponse;
    16. import java.text.ParseException;
    17. import java.util.Date;
    18. import java.util.Map;
    19. /**
    20. * @Author: JCccc
    21. * @Date: 2022-6-2 14:23
    22. * @Description: xxl job rest api
    23. */
    24. @RestController
    25. @RequestMapping("/api/jobinfo")
    26. public class MyDynamicApiController {
    27. private static Logger logger = LoggerFactory.getLogger(MyDynamicApiController.class);
    28. @Autowired
    29. private XxlJobService xxlJobService;
    30. @Autowired
    31. private LoginService loginService;
    32. @RequestMapping(value = "/pageList",method = RequestMethod.POST)
    33. public Map<String, Object> pageList(@RequestBody XxlJobQuery xxlJobQuery) {
    34. return xxlJobService.pageList(
    35. xxlJobQuery.getStart(),
    36. xxlJobQuery.getLength(),
    37. xxlJobQuery.getJobGroup(),
    38. xxlJobQuery.getTriggerStatus(),
    39. xxlJobQuery.getJobDesc(),
    40. xxlJobQuery.getExecutorHandler(),
    41. xxlJobQuery.getAuthor());
    42. }
    43. @PostMapping("/save")
    44. public ReturnT<String> add(@RequestBody(required = true)XxlJobInfo jobInfo) {
    45. // next trigger time (5s后生效,避开预读周期)
    46. long nextTriggerTime = 0;
    47. try {
    48. Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS));
    49. if (nextValidTime == null) {
    50. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire"));
    51. }
    52. nextTriggerTime = nextValidTime.getTime();
    53. } catch (ParseException e) {
    54. logger.error(e.getMessage(), e);
    55. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage());
    56. }
    57. jobInfo.setTriggerStatus(1);
    58. jobInfo.setTriggerLastTime(0);
    59. jobInfo.setTriggerNextTime(nextTriggerTime);
    60. jobInfo.setUpdateTime(new Date());
    61. if(jobInfo.getId()==0){
    62. return xxlJobService.add(jobInfo);
    63. }else{
    64. return xxlJobService.update(jobInfo);
    65. }
    66. }
    67. @RequestMapping(value = "/delete",method = RequestMethod.GET)
    68. public ReturnT<String> delete(int id) {
    69. return xxlJobService.remove(id);
    70. }
    71. @RequestMapping(value = "/start",method = RequestMethod.GET)
    72. public ReturnT<String> start(int id) {
    73. return xxlJobService.start(id);
    74. }
    75. @RequestMapping(value = "/stop",method = RequestMethod.GET)
    76. public ReturnT<String> stop(int id) {
    77. return xxlJobService.stop(id);
    78. }
    79. @RequestMapping(value="login", method=RequestMethod.GET)
    80. @PermissionLimit(limit=false)
    81. public ReturnT<String> loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password, String ifRemember){
    82. boolean ifRem = (ifRemember!=null && ifRemember.trim().length()>0 && "on".equals(ifRemember))?true:false;
    83. ReturnT<String> result= loginService.login(request, response, userName, password, ifRem);
    84. return result;
    85. }
    86. }

    XxlJobQuery.java  

    (这里有说法,为什么我这篇特意写了一个这个查询类,抛砖引玉,在新手篇里面我介绍过,xxl job 是有数据库的,意味着我们可以很容易拓展)

    1. /**
    2. * @Author: JCccc
    3. * @Date: 2022-6-2 14:23
    4. * @Description: xxl job rest api
    5. */
    6. public class XxlJobQuery {
    7. private int start;
    8. private int length;
    9. private int triggerStatus;
    10. private String jobDesc;
    11. private String executorHandler;
    12. private String author;
    13. private int jobGroup;
    14. public int getStart() {
    15. return start;
    16. }
    17. public void setStart(int start) {
    18. this.start = start;
    19. }
    20. public int getLength() {
    21. return length;
    22. }
    23. public void setLength(int length) {
    24. this.length = length;
    25. }
    26. public int getTriggerStatus() {
    27. return triggerStatus;
    28. }
    29. public void setTriggerStatus(int triggerStatus) {
    30. this.triggerStatus = triggerStatus;
    31. }
    32. public String getJobDesc() {
    33. return jobDesc;
    34. }
    35. public void setJobDesc(String jobDesc) {
    36. this.jobDesc = jobDesc;
    37. }
    38. public String getExecutorHandler() {
    39. return executorHandler;
    40. }
    41. public void setExecutorHandler(String executorHandler) {
    42. this.executorHandler = executorHandler;
    43. }
    44. public String getAuthor() {
    45. return author;
    46. }
    47. public void setAuthor(String author) {
    48. this.author = author;
    49. }
    50. public int getJobGroup() {
    51. return jobGroup;
    52. }
    53. public void setJobGroup(int jobGroup) {
    54. this.jobGroup = jobGroup;
    55. }
    56. }

    ② 在接入xxl job 的demo上 开始玩动态调度,其实就是调用admin里我们刚才写的提供的接口

    PS: 本篇里面有些不好的编码习惯,例如返回值用Map;打印用的输出,没用log; 接口返回没有统一返回数据等等, 因为我该篇是为了传递 怎么动态调度使用,具体细节大家自行调整就行,我们都是成年人,不要在意我这些点。

    先是pom.xml 引入使用的一些jar:


    (一个是fastjson,大家别学我,我是为了实战示例图方便,用的JsonObject来传参)

    (一个是httpClient ,用于调用admin服务的api接口)

    1. <dependency>
    2. <groupId>commons-httpclient</groupId>
    3. <artifactId>commons-httpclient</artifactId>
    4. <version>3.1</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.alibaba</groupId>
    8. <artifactId>fastjson</artifactId>
    9. <version>1.2.47</version>
    10. </dependency>

    XxlJobInfo.java 

    这个是原作者的其实,因为我们本质还是调用了作者xxl job提供的  XxlJobService 里面的方法。

    1. import java.util.Date;
    2. /**
    3. * xxl-job info
    4. *
    5. * @author xuxueli 2016-1-12 18:25:49
    6. */
    7. public class XxlJobInfo {
    8. private int id; // 主键ID
    9. private int jobGroup; // 执行器主键ID
    10. private String jobDesc; // 备注
    11. private String jobCron;
    12. private Date addTime;
    13. private Date updateTime;
    14. private String author; // 负责人
    15. private String alarmEmail; // 报警邮件
    16. private String scheduleType; // 调度类型
    17. private String scheduleConf; // 调度配置,值含义取决于调度类型
    18. private String misfireStrategy; // 调度过期策略
    19. private String executorRouteStrategy; // 执行器路由策略
    20. private String executorHandler; // 执行器,任务Handler名称
    21. private String executorParam; // 执行器,任务参数
    22. private String executorBlockStrategy; // 阻塞处理策略
    23. private int executorTimeout; // 任务执行超时时间,单位秒
    24. private int executorFailRetryCount; // 失败重试次数
    25. private String glueType; // GLUE类型 #com.xxl.job.core.glue.GlueTypeEnum
    26. private String glueSource; // GLUE源代码
    27. private String glueRemark; // GLUE备注
    28. private Date glueUpdatetime; // GLUE更新时间
    29. private String childJobId; // 子任务ID,多个逗号分隔
    30. private int triggerStatus; // 调度状态:0-停止,1-运行
    31. private long triggerLastTime; // 上次调度时间
    32. private long triggerNextTime; // 下次调度时间
    33. public String getJobCron() {
    34. return jobCron;
    35. }
    36. public void setJobCron(String jobCron) {
    37. this.jobCron = jobCron;
    38. }
    39. public int getId() {
    40. return id;
    41. }
    42. public void setId(int id) {
    43. this.id = id;
    44. }
    45. public int getJobGroup() {
    46. return jobGroup;
    47. }
    48. public void setJobGroup(int jobGroup) {
    49. this.jobGroup = jobGroup;
    50. }
    51. public String getJobDesc() {
    52. return jobDesc;
    53. }
    54. public void setJobDesc(String jobDesc) {
    55. this.jobDesc = jobDesc;
    56. }
    57. public Date getAddTime() {
    58. return addTime;
    59. }
    60. public void setAddTime(Date addTime) {
    61. this.addTime = addTime;
    62. }
    63. public Date getUpdateTime() {
    64. return updateTime;
    65. }
    66. public void setUpdateTime(Date updateTime) {
    67. this.updateTime = updateTime;
    68. }
    69. public String getAuthor() {
    70. return author;
    71. }
    72. public void setAuthor(String author) {
    73. this.author = author;
    74. }
    75. public String getAlarmEmail() {
    76. return alarmEmail;
    77. }
    78. public void setAlarmEmail(String alarmEmail) {
    79. this.alarmEmail = alarmEmail;
    80. }
    81. public String getScheduleType() {
    82. return scheduleType;
    83. }
    84. public void setScheduleType(String scheduleType) {
    85. this.scheduleType = scheduleType;
    86. }
    87. public String getScheduleConf() {
    88. return scheduleConf;
    89. }
    90. public void setScheduleConf(String scheduleConf) {
    91. this.scheduleConf = scheduleConf;
    92. }
    93. public String getMisfireStrategy() {
    94. return misfireStrategy;
    95. }
    96. public void setMisfireStrategy(String misfireStrategy) {
    97. this.misfireStrategy = misfireStrategy;
    98. }
    99. public String getExecutorRouteStrategy() {
    100. return executorRouteStrategy;
    101. }
    102. public void setExecutorRouteStrategy(String executorRouteStrategy) {
    103. this.executorRouteStrategy = executorRouteStrategy;
    104. }
    105. public String getExecutorHandler() {
    106. return executorHandler;
    107. }
    108. public void setExecutorHandler(String executorHandler) {
    109. this.executorHandler = executorHandler;
    110. }
    111. public String getExecutorParam() {
    112. return executorParam;
    113. }
    114. public void setExecutorParam(String executorParam) {
    115. this.executorParam = executorParam;
    116. }
    117. public String getExecutorBlockStrategy() {
    118. return executorBlockStrategy;
    119. }
    120. public void setExecutorBlockStrategy(String executorBlockStrategy) {
    121. this.executorBlockStrategy = executorBlockStrategy;
    122. }
    123. public int getExecutorTimeout() {
    124. return executorTimeout;
    125. }
    126. public void setExecutorTimeout(int executorTimeout) {
    127. this.executorTimeout = executorTimeout;
    128. }
    129. public int getExecutorFailRetryCount() {
    130. return executorFailRetryCount;
    131. }
    132. public void setExecutorFailRetryCount(int executorFailRetryCount) {
    133. this.executorFailRetryCount = executorFailRetryCount;
    134. }
    135. public String getGlueType() {
    136. return glueType;
    137. }
    138. public void setGlueType(String glueType) {
    139. this.glueType = glueType;
    140. }
    141. public String getGlueSource() {
    142. return glueSource;
    143. }
    144. public void setGlueSource(String glueSource) {
    145. this.glueSource = glueSource;
    146. }
    147. public String getGlueRemark() {
    148. return glueRemark;
    149. }
    150. public void setGlueRemark(String glueRemark) {
    151. this.glueRemark = glueRemark;
    152. }
    153. public Date getGlueUpdatetime() {
    154. return glueUpdatetime;
    155. }
    156. public void setGlueUpdatetime(Date glueUpdatetime) {
    157. this.glueUpdatetime = glueUpdatetime;
    158. }
    159. public String getChildJobId() {
    160. return childJobId;
    161. }
    162. public void setChildJobId(String childJobId) {
    163. this.childJobId = childJobId;
    164. }
    165. public int getTriggerStatus() {
    166. return triggerStatus;
    167. }
    168. public void setTriggerStatus(int triggerStatus) {
    169. this.triggerStatus = triggerStatus;
    170. }
    171. public long getTriggerLastTime() {
    172. return triggerLastTime;
    173. }
    174. public void setTriggerLastTime(long triggerLastTime) {
    175. this.triggerLastTime = triggerLastTime;
    176. }
    177. public long getTriggerNextTime() {
    178. return triggerNextTime;
    179. }
    180. public void setTriggerNextTime(long triggerNextTime) {
    181. this.triggerNextTime = triggerNextTime;
    182. }
    183. }

    XxlJobUtil.java 

    1. import com.alibaba.fastjson.JSONObject;
    2. import org.apache.commons.httpclient.*;
    3. import org.apache.commons.httpclient.methods.GetMethod;
    4. import org.apache.commons.httpclient.methods.PostMethod;
    5. import org.apache.commons.httpclient.methods.RequestEntity;
    6. import org.apache.commons.httpclient.methods.StringRequestEntity;
    7. import java.io.BufferedReader;
    8. import java.io.IOException;
    9. import java.io.InputStream;
    10. import java.io.InputStreamReader;
    11. /**
    12. * @Author: JCccc
    13. * @Date: 2022-6-22 9:51
    14. * @Description:
    15. */
    16. public class XxlJobUtil {
    17. private static String cookie="";
    18. /**
    19. * 查询现有的任务(可以关注这个整个调用链,可以自己模仿着写其他的拓展接口)
    20. * @param url
    21. * @param requestInfo
    22. * @return
    23. * @throws HttpException
    24. * @throws IOException
    25. */
    26. public static JSONObject pageList(String url,JSONObject requestInfo) throws HttpException, IOException {
    27. String path = "/api/jobinfo/pageList";
    28. String targetUrl = url + path;
    29. HttpClient httpClient = new HttpClient();
    30. PostMethod post = new PostMethod(targetUrl);
    31. post.setRequestHeader("cookie", cookie);
    32. RequestEntity requestEntity = new StringRequestEntity(requestInfo.toString(), "application/json", "utf-8");
    33. post.setRequestEntity(requestEntity);
    34. httpClient.executeMethod(post);
    35. JSONObject result = new JSONObject();
    36. result = getJsonObject(post, result);
    37. System.out.println(result.toJSONString());
    38. return result;
    39. }
    40. /**
    41. * 新增/编辑任务
    42. * @param url
    43. * @param requestInfo
    44. * @return
    45. * @throws HttpException
    46. * @throws IOException
    47. */
    48. public static JSONObject addJob(String url,JSONObject requestInfo) throws HttpException, IOException {
    49. String path = "/api/jobinfo/save";
    50. String targetUrl = url + path;
    51. HttpClient httpClient = new HttpClient();
    52. PostMethod post = new PostMethod(targetUrl);
    53. post.setRequestHeader("cookie", cookie);
    54. RequestEntity requestEntity = new StringRequestEntity(requestInfo.toString(), "application/json", "utf-8");
    55. post.setRequestEntity(requestEntity);
    56. httpClient.executeMethod(post);
    57. JSONObject result = new JSONObject();
    58. result = getJsonObject(post, result);
    59. System.out.println(result.toJSONString());
    60. return result;
    61. }
    62. /**
    63. * 删除任务
    64. * @param url
    65. * @param id
    66. * @return
    67. * @throws HttpException
    68. * @throws IOException
    69. */
    70. public static JSONObject deleteJob(String url,int id) throws HttpException, IOException {
    71. String path = "/api/jobinfo/delete?id="+id;
    72. return doGet(url,path);
    73. }
    74. /**
    75. * 开始任务
    76. * @param url
    77. * @param id
    78. * @return
    79. * @throws HttpException
    80. * @throws IOException
    81. */
    82. public static JSONObject startJob(String url,int id) throws HttpException, IOException {
    83. String path = "/api/jobinfo/start?id="+id;
    84. return doGet(url,path);
    85. }
    86. /**
    87. * 停止任务
    88. * @param url
    89. * @param id
    90. * @return
    91. * @throws HttpException
    92. * @throws IOException
    93. */
    94. public static JSONObject stopJob(String url,int id) throws HttpException, IOException {
    95. String path = "/api/jobinfo/stop?id="+id;
    96. return doGet(url,path);
    97. }
    98. public static JSONObject doGet(String url,String path) throws HttpException, IOException {
    99. String targetUrl = url + path;
    100. HttpClient httpClient = new HttpClient();
    101. HttpMethod get = new GetMethod(targetUrl);
    102. get.setRequestHeader("cookie", cookie);
    103. httpClient.executeMethod(get);
    104. JSONObject result = new JSONObject();
    105. result = getJsonObject(get, result);
    106. return result;
    107. }
    108. private static JSONObject getJsonObject(HttpMethod postMethod, JSONObject result) throws IOException {
    109. if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
    110. InputStream inputStream = postMethod.getResponseBodyAsStream();
    111. BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    112. StringBuffer stringBuffer = new StringBuffer();
    113. String str;
    114. while((str = br.readLine()) != null){
    115. stringBuffer.append(str);
    116. }
    117. String response = new String(stringBuffer);
    118. br.close();
    119. return (JSONObject) JSONObject.parse(response);
    120. } else {
    121. return null;
    122. }
    123. }
    124. /**
    125. * 登录
    126. * @param url
    127. * @param userName
    128. * @param password
    129. * @return
    130. * @throws HttpException
    131. * @throws IOException
    132. */
    133. public static String login(String url, String userName, String password) throws HttpException, IOException {
    134. String path = "/api/jobinfo/login?userName="+userName+"&password="+password;
    135. String targetUrl = url + path;
    136. HttpClient httpClient = new HttpClient();
    137. HttpMethod get = new GetMethod((targetUrl));
    138. httpClient.executeMethod(get);
    139. if (get.getStatusCode() == 200) {
    140. Cookie[] cookies = httpClient.getState().getCookies();
    141. StringBuffer tmpcookies = new StringBuffer();
    142. for (Cookie c : cookies) {
    143. tmpcookies.append(c.toString() + ";");
    144. }
    145. cookie = tmpcookies.toString();
    146. } else {
    147. try {
    148. cookie = "";
    149. } catch (Exception e) {
    150. cookie="";
    151. }
    152. }
    153. return cookie;
    154. }
    155. }

     XxlJobController.java

    (用于模拟触发我们的任务创建、编辑、删除、停止等等)

    1. import java.util.Date;
    2. import com.alibaba.fastjson.JSONObject;
    3. import org.springframework.web.bind.annotation.*;
    4. import java.io.IOException;
    5. /**
    6. * @Author: JCccc
    7. * @Date: 2022-6-22 9:26
    8. * @Description:
    9. */
    10. @RestController
    11. public class XxlJobController {
    12. @RequestMapping(value = "/pageList",method = RequestMethod.GET)
    13. public Object pageList() throws IOException {
    14. //int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author
    15. JSONObject test=new JSONObject();
    16. test.put("length",10);
    17. XxlJobUtil.login("http://127.0.0.1:8961/xxl-job-admin","admin","123456");
    18. JSONObject response = XxlJobUtil.pageList("http://127.0.0.1:8961/xxl-job-admin", test);
    19. return response.get("data");
    20. }
    21. @RequestMapping(value = "/add",method = RequestMethod.GET)
    22. public void add() throws IOException {
    23. XxlJobInfo xxlJobInfo=new XxlJobInfo();
    24. xxlJobInfo.setJobCron("0/5 * * * * ?");
    25. xxlJobInfo.setJobGroup(3);
    26. xxlJobInfo.setJobDesc("我来试试");
    27. xxlJobInfo.setAddTime(new Date());
    28. xxlJobInfo.setUpdateTime(new Date());
    29. xxlJobInfo.setAuthor("JCccc");
    30. xxlJobInfo.setAlarmEmail("864477182@com");
    31. xxlJobInfo.setScheduleType("CRON");
    32. xxlJobInfo.setScheduleConf("0/5 * * * * ?");
    33. xxlJobInfo.setMisfireStrategy("DO_NOTHING");
    34. xxlJobInfo.setExecutorRouteStrategy("FIRST");
    35. xxlJobInfo.setExecutorHandler("clockInJobHandler");
    36. xxlJobInfo.setExecutorParam("att");
    37. xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
    38. xxlJobInfo.setExecutorTimeout(0);
    39. xxlJobInfo.setExecutorFailRetryCount(1);
    40. xxlJobInfo.setGlueType("BEAN");
    41. xxlJobInfo.setGlueSource("");
    42. xxlJobInfo.setGlueRemark("GLUE代码初始化");
    43. xxlJobInfo.setGlueUpdatetime(new Date());
    44. JSONObject test = (JSONObject) JSONObject.toJSON(xxlJobInfo);
    45. XxlJobUtil.login("http://127.0.0.1:8961/xxl-job-admin","admin","123456");
    46. JSONObject response = XxlJobUtil.addJob("http://127.0.0.1:8961/xxl-job-admin", test);
    47. if (response.containsKey("code") && 200 == (Integer) response.get("code")) {
    48. System.out.println("新增成功");
    49. } else {
    50. System.out.println("新增失败");
    51. }
    52. }
    53. @RequestMapping(value = "/stop/{jobId}",method = RequestMethod.GET)
    54. public void stop(@PathVariable("jobId") Integer jobId) throws IOException {
    55. XxlJobUtil.login("http://127.0.0.1:8961/xxl-job-admin","admin","123456");
    56. JSONObject response = XxlJobUtil.stopJob("http://127.0.0.1:8961/xxl-job-admin", jobId);
    57. if (response.containsKey("code") && 200 == (Integer) response.get("code")) {
    58. System.out.println("任务停止成功");
    59. } else {
    60. System.out.println("任务停止失败");
    61. }
    62. }
    63. @RequestMapping(value = "/delete/{jobId}",method = RequestMethod.GET)
    64. public void delete(@PathVariable("jobId") Integer jobId) throws IOException {
    65. XxlJobUtil.login("http://127.0.0.1:8961/xxl-job-admin","admin","123456");
    66. JSONObject response = XxlJobUtil.deleteJob("http://127.0.0.1:8961/xxl-job-admin", jobId);
    67. if (response.containsKey("code") && 200 == (Integer) response.get("code")) {
    68. System.out.println("任务移除成功");
    69. } else {
    70. System.out.println("任务移除失败");
    71. }
    72. }
    73. @RequestMapping(value = "/start/{jobId}",method = RequestMethod.GET)
    74. public void start(@PathVariable("jobId") Integer jobId) throws IOException {
    75. XxlJobUtil.login("http://127.0.0.1:8961/xxl-job-admin","admin","123456");
    76. JSONObject response = XxlJobUtil.startJob("http://127.0.0.1:8961/xxl-job-admin", jobId);
    77. if (response.containsKey("code") && 200 == (Integer) response.get("code")) {
    78. System.out.println("任务启动成功");
    79. } else {
    80. System.out.println("任务启动失败");
    81. }
    82. }
    83. }



    开始验证测试



    创建任务:

    打开admin的HTML页面,这是原来上次我们手动创建的任务:

    现在我们通过api调用创建一个新的任务:

     调用接口后,可以看的任务创建出来了,按照我们的cron和其他调度规则开始跑了:

     

    然后我们调用停止任务接口(通过任务ID停止):

     

    停在成功:

    那么玩到这,可能大伙会有疑问,我怎么知道哪个我要停止的任务的任务ID是哪个?

    所以我抛砖引玉写了个查询列表接口,大家可以意会一下,数据都在的,数据还不好拓展么,什么业务场景,怎么使用怎么调度,都可以自由发挥啊:

    数据就在库里,自己查不就好了:

    剩下还有启动 start接口,删除delete接口,修改update接口,我就不一一展示测试了。

    这些增删改查,怎么组合使用,什么业务使用,大家自己玩起来就行。

    好吧,该篇就到这。

  • 相关阅读:
    xss盲打
    【SQL】新建库表时,报错attempt to write a readonly database
    【​毕业季·进击的技术er​】--毕业到工作小结
    Plink常见命令 --bfile --freq--recode --make-bed
    基于springboot实现民宿管理平台项目【项目源码+论文说明】计算机毕业设计
    RTOS 中 Task 之间资源共享示例
    transformer的理解
    Ubuntu扩容lvm空间
    【第6篇】AI语音测试简介
    如何找回回收站清空的文件?
  • 原文地址:https://blog.csdn.net/qq_35387940/article/details/125410895