XxlJobUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.cyksj.xxljob;
  2. /**
  3. * @ClassName XxlJobUtil
  4. * @Description TODO
  5. * @Author Huang yuanzhi
  6. * @Date 2023/6/12 16:11
  7. * @Version 1.0.0
  8. **/
  9. import cn.hutool.http.HttpException;
  10. import cn.hutool.http.HttpResponse;
  11. import cn.hutool.http.HttpUtil;
  12. import cn.hutool.json.JSONObject;
  13. import com.cyksj.common.util.Jsons;
  14. import com.cyksj.dto.XxlJobResult;
  15. import java.io.IOException;
  16. import java.net.HttpCookie;
  17. import java.util.List;
  18. /**
  19. * @Author: JCccc
  20. * @Date: 2022-6-22 9:51
  21. * @Description:
  22. */
  23. public class XxlJobUtil {
  24. private final static String BASE_URL = "http://app7.liuliangbang.vip:9099/xxl-job-admin";
  25. private static String cookie = "";
  26. // /**
  27. // * 查询现有的任务(可以关注这个整个调用链,可以自己模仿着写其他的拓展接口)
  28. // * @return
  29. // * @throws HttpException
  30. // * @throws IOException
  31. // */
  32. // public static JSONObject pageList(Map<String,Object> param) throws Exception {
  33. // String path = "/jobinfo/pageList" + "?jobGroup=5&triggerStatus=-1";
  34. // String targetUrl = BASE_URL + path;
  35. // System.out.println(cookie);
  36. // HttpResponse execute = HttpUtil.createPost(targetUrl)
  37. // .header("Content-Type","application/json")
  38. // .header("Cookie",getCookie())
  39. // .execute();
  40. // return Jsons.parseObject(execute.body(),JSONObject.class);
  41. // }
  42. /**
  43. * 新增任务
  44. *
  45. * @param jobParam
  46. * @return
  47. * @throws HttpException
  48. * @throws IOException
  49. */
  50. public static XxlJobResult addJob(String jobParam) throws Exception {
  51. String path = "/jobinfo/add";
  52. String targetUrl = BASE_URL + path + jobParam;
  53. HttpResponse execute = HttpUtil.createPost(targetUrl)
  54. .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  55. .header("Cookie", getCookie())
  56. .execute();
  57. return Jsons.parseObject(execute.body(), XxlJobResult.class);
  58. }
  59. public static void main(String[] args) throws Exception {
  60. // updateJob(XxlJobInfo.getUpdateInstance(58,XxlJobInfo.AUTHOR.HUANG_YUAN_ZHI,"修改job","0/4 * * * * ?",5,"LikeJob","").toString());
  61. JSONObject jsonObject = deleteJob("67");
  62. System.out.println(jsonObject);
  63. }
  64. /**
  65. * 编辑任务
  66. *
  67. * @param query
  68. * @return
  69. * @throws Exception
  70. */
  71. public static JSONObject updateJob(String query) throws Exception {
  72. String path = "/jobinfo/update";
  73. String targetUrl = BASE_URL + path + query;
  74. HttpResponse execute = HttpUtil.createPost(targetUrl)
  75. .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  76. .header("Cookie", getCookie())
  77. .execute();
  78. return Jsons.parseObject(execute.body(), JSONObject.class);
  79. }
  80. /**
  81. * 删除任务
  82. *
  83. * @param id
  84. * @return
  85. * @throws HttpException
  86. * @throws IOException
  87. */
  88. public static JSONObject deleteJob(String id) throws Exception {
  89. String path = "/jobinfo/remove?id=" + id;
  90. String url = BASE_URL + path;
  91. HttpResponse execute = HttpUtil.createGet(url).header("Cookie", getCookie()).execute();
  92. return Jsons.parseObject(execute.body(), JSONObject.class);
  93. }
  94. /**
  95. * 立即执行某一定时任务
  96. * @param jobParam
  97. */
  98. public static XxlJobResult executeMarkDynamicRuleTagJob(String jobParam) throws Exception {
  99. String path = "/jobinfo/trigger";
  100. String targetUrl = BASE_URL + path + jobParam;
  101. HttpResponse execute = HttpUtil.createPost(targetUrl)
  102. .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  103. .header("Cookie", getCookie())
  104. .execute();
  105. return Jsons.parseObject(execute.body(), XxlJobResult.class);
  106. }
  107. public static XxlJobResult stopJob(String jobId) throws Exception {
  108. String path = "/jobinfo/stop?id=" + jobId;
  109. String targetUrl = BASE_URL + path;
  110. HttpResponse execute = HttpUtil.createPost(targetUrl)
  111. .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
  112. .header("Cookie", getCookie())
  113. .execute();
  114. return Jsons.parseObject(execute.body(), XxlJobResult.class);
  115. }
  116. /**
  117. * 获取登录cookie
  118. *
  119. * @return
  120. */
  121. public static String getCookie() {
  122. String path = "/login";
  123. String targetUrl = BASE_URL + path;
  124. HttpResponse execute = HttpUtil.createPost(targetUrl)
  125. .form("userName", "admin")
  126. .form("password", "123456")
  127. .execute();
  128. if (execute.getStatus() == 200) {
  129. List<HttpCookie> cookies = execute.getCookies();
  130. StringBuffer tmpcookies = new StringBuffer();
  131. for (HttpCookie c : cookies) {
  132. tmpcookies.append(c.getName()).append("=").append(c.getValue() + ";");
  133. }
  134. cookie = tmpcookies.toString();
  135. } else {
  136. try {
  137. cookie = "";
  138. } catch (Exception e) {
  139. cookie = "";
  140. }
  141. }
  142. return cookie;
  143. }
  144. }