42fd02e241c5d237ee47f0f2baee6a118fc8dafa.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package org.jeecg.modules.cloud.xxljob;;
  2. import com.xxl.job.core.biz.model.ReturnT;
  3. import com.xxl.job.core.handler.IJobHandler;
  4. import com.xxl.job.core.handler.annotation.XxlJob;
  5. import com.xxl.job.core.log.XxlJobLogger;
  6. import com.xxl.job.core.util.ShardingUtil;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.stereotype.Component;
  9. import java.io.BufferedInputStream;
  10. import java.io.BufferedReader;
  11. import java.io.DataOutputStream;
  12. import java.io.InputStreamReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.util.Arrays;
  16. /**
  17. * xxl-job定时任务测试
  18. */
  19. @Component
  20. @Slf4j
  21. public class DemoJobHandler {
  22. /**
  23. * 简单任务
  24. *
  25. * @param params
  26. * @return
  27. */
  28. @XxlJob(value = "demoJob")
  29. public ReturnT<String> demoJobHandler(String params) {
  30. log.info("我是定时任务,我执行了...............................");
  31. return ReturnT.SUCCESS;
  32. }
  33. /**
  34. * 2、分片广播任务
  35. */
  36. @XxlJob("shardingJobHandler")
  37. public ReturnT<String> shardingJobHandler(String param) throws Exception {
  38. // 分片参数
  39. ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
  40. XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal());
  41. // 业务逻辑
  42. for (int i = 0; i < shardingVO.getTotal(); i++) {
  43. if (i == shardingVO.getIndex()) {
  44. XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
  45. } else {
  46. XxlJobLogger.log("第 {} 片, 忽略", i);
  47. }
  48. }
  49. return ReturnT.SUCCESS;
  50. }
  51. /**
  52. * 3、命令行任务
  53. */
  54. @XxlJob("commandJobHandler")
  55. public ReturnT<String> commandJobHandler(String param) throws Exception {
  56. String command = param;
  57. int exitValue = -1;
  58. BufferedReader bufferedReader = null;
  59. try {
  60. // command process
  61. Process process = Runtime.getRuntime().exec(command);
  62. BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
  63. bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
  64. // command log
  65. String line;
  66. while ((line = bufferedReader.readLine()) != null) {
  67. XxlJobLogger.log(line);
  68. }
  69. // command exit
  70. process.waitFor();
  71. exitValue = process.exitValue();
  72. } catch (Exception e) {
  73. XxlJobLogger.log(e);
  74. } finally {
  75. if (bufferedReader != null) {
  76. bufferedReader.close();
  77. }
  78. }
  79. if (exitValue == 0) {
  80. return IJobHandler.SUCCESS;
  81. } else {
  82. return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value(" + exitValue + ") is failed");
  83. }
  84. }
  85. /**
  86. * 4、跨平台Http任务
  87. * 参数示例:
  88. * "url: http://www.baidu.com\n" +
  89. * "method: get\n" +
  90. * "data: content\n";
  91. */
  92. @XxlJob("httpJobHandler")
  93. public ReturnT<String> httpJobHandler(String param) throws Exception {
  94. // param parse
  95. if (param == null || param.trim().length() == 0) {
  96. XxlJobLogger.log("param[" + param + "] invalid.");
  97. return ReturnT.FAIL;
  98. }
  99. String[] httpParams = param.split("\n");
  100. String url = null;
  101. String method = null;
  102. String data = null;
  103. for (String httpParam : httpParams) {
  104. if (httpParam.startsWith("url:")) {
  105. url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
  106. }
  107. if (httpParam.startsWith("method:")) {
  108. method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
  109. }
  110. if (httpParam.startsWith("data:")) {
  111. data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
  112. }
  113. }
  114. // param valid
  115. if (url == null || url.trim().length() == 0) {
  116. XxlJobLogger.log("url[" + url + "] invalid.");
  117. return ReturnT.FAIL;
  118. }
  119. if (method == null || !Arrays.asList("GET", "POST").contains(method)) {
  120. XxlJobLogger.log("method[" + method + "] invalid.");
  121. return ReturnT.FAIL;
  122. }
  123. // request
  124. HttpURLConnection connection = null;
  125. BufferedReader bufferedReader = null;
  126. try {
  127. // connection
  128. URL realUrl = new URL(url);
  129. connection = (HttpURLConnection) realUrl.openConnection();
  130. // connection setting
  131. connection.setRequestMethod(method);
  132. connection.setDoOutput(true);
  133. connection.setDoInput(true);
  134. connection.setUseCaches(false);
  135. connection.setReadTimeout(5 * 1000);
  136. connection.setConnectTimeout(3 * 1000);
  137. connection.setRequestProperty("connection", "Keep-Alive");
  138. connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  139. connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
  140. // do connection
  141. connection.connect();
  142. // data
  143. if (data != null && data.trim().length() > 0) {
  144. DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
  145. dataOutputStream.write(data.getBytes("UTF-8"));
  146. dataOutputStream.flush();
  147. dataOutputStream.close();
  148. }
  149. // valid StatusCode
  150. int statusCode = connection.getResponseCode();
  151. if (statusCode != 200) {
  152. throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
  153. }
  154. // result
  155. bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  156. StringBuilder result = new StringBuilder();
  157. String line;
  158. while ((line = bufferedReader.readLine()) != null) {
  159. result.append(line);
  160. }
  161. String responseMsg = result.toString();
  162. XxlJobLogger.log(responseMsg);
  163. return ReturnT.SUCCESS;
  164. } catch (Exception e) {
  165. XxlJobLogger.log(e);
  166. return ReturnT.FAIL;
  167. } finally {
  168. try {
  169. if (bufferedReader != null) {
  170. bufferedReader.close();
  171. }
  172. if (connection != null) {
  173. connection.disconnect();
  174. }
  175. } catch (Exception e2) {
  176. XxlJobLogger.log(e2);
  177. }
  178. }
  179. }
  180. /**
  181. * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
  182. */
  183. @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
  184. public ReturnT<String> demoJobHandler2(String param) throws Exception {
  185. XxlJobLogger.log("XXL-JOB, Hello World.");
  186. return ReturnT.SUCCESS;
  187. }
  188. public void init() {
  189. log.info("init");
  190. }
  191. public void destroy() {
  192. log.info("destory");
  193. }
  194. }