定时任务测试
This commit is contained in:
22
pom.xml
22
pom.xml
@ -27,8 +27,16 @@
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-attachment-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
||||
<!-- xxl-job-core -->
|
||||
<dependency>
|
||||
<groupId>cn.chinaunicom.sdsi</groupId>
|
||||
<artifactId>mall-xxljob-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.chinaunicom.ebtp</groupId>
|
||||
<artifactId>mall-ebtp-cloud-apollo-starter</artifactId>
|
||||
</dependency>
|
||||
@ -93,7 +101,11 @@
|
||||
<scope>system</scope>
|
||||
<systemPath>${basedir}/lib/aspose-words-16.8.0-jdk16.jar</systemPath>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.chinaunicom.sdsi</groupId>
|
||||
<artifactId>mall-xxljob-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-tools</artifactId>
|
||||
@ -130,6 +142,10 @@
|
||||
<id>com.e-iceblue</id>
|
||||
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>maven-virtual</id>
|
||||
<url>http://ccp.tianti.tg.unicom.local/artifactory/maven-virtual</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,58 @@
|
||||
package com.chinaunicom.mall.ebtp.extend.executor.config;
|
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* xxl-job config
|
||||
*
|
||||
* @author xuxueli 2017-04-28
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class XxlJobConfig {
|
||||
|
||||
@Value("${xxl.job.admin.addresses}")
|
||||
private String adminAddresses;
|
||||
|
||||
@Value("${xxl.job.accessToken}")
|
||||
private String accessToken;
|
||||
|
||||
@Value("${xxl.job.executor.appname}")
|
||||
private String appname;
|
||||
|
||||
@Value("${xxl.job.executor.address}")
|
||||
private String address;
|
||||
|
||||
@Value("${xxl.job.executor.ip}")
|
||||
private String ip;
|
||||
|
||||
@Value("${server.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${xxl.job.executor.logpath}")
|
||||
private String logPath;
|
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}")
|
||||
private int logRetentionDays;
|
||||
|
||||
|
||||
@Bean
|
||||
public XxlJobSpringExecutor xxlJobExecutor() {
|
||||
log.info(">>>>>>>>>>> xxl-job config init.");
|
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
|
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
|
||||
xxlJobSpringExecutor.setAppname(appname);
|
||||
xxlJobSpringExecutor.setAddress(address);
|
||||
xxlJobSpringExecutor.setIp(ip);
|
||||
xxlJobSpringExecutor.setPort(port);
|
||||
xxlJobSpringExecutor.setAccessToken(accessToken);
|
||||
xxlJobSpringExecutor.setLogPath(logPath);
|
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
|
||||
|
||||
return xxlJobSpringExecutor;
|
||||
}
|
||||
}
|
@ -0,0 +1,239 @@
|
||||
package com.chinaunicom.mall.ebtp.extend.executor.jobhandler;
|
||||
|
||||
import com.xxl.job.core.biz.model.ReturnT;
|
||||
import com.xxl.job.core.context.XxlJobContext;
|
||||
import com.xxl.job.core.handler.IJobHandler;
|
||||
import com.xxl.job.core.handler.annotation.XxlJob;
|
||||
import com.xxl.job.core.log.XxlJobLogger;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* XxlJob开发示例(Bean模式)
|
||||
*
|
||||
* 开发步骤:
|
||||
* 1、在Spring Bean实例中,开发Job方法,方式格式要求为 "public ReturnT<String> execute(String param)"
|
||||
* 2、为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。
|
||||
* 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
|
||||
*
|
||||
* @author xuxueli 2019-12-11 21:52:51
|
||||
*/
|
||||
@Component
|
||||
public class SampleXxlJob {
|
||||
private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);
|
||||
|
||||
|
||||
/**
|
||||
* 1、简单任务示例(Bean模式)
|
||||
*/
|
||||
@XxlJob("extendJobHandler")
|
||||
public ReturnT<String> extendJobHandler(String param) throws Exception {
|
||||
XxlJobLogger.log("XXL-JOB, Hello World.");
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
XxlJobLogger.log("beat at:" + i);
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
}
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * 2、分片广播任务
|
||||
// */
|
||||
// @XxlJob("shardingJobHandler")
|
||||
// public ReturnT<String> shardingJobHandler(String param) throws Exception {
|
||||
//
|
||||
// // 分片参数
|
||||
// int shardIndex = XxlJobContext.getXxlJobContext().getShardIndex();
|
||||
// int shardTotal = XxlJobContext.getXxlJobContext().getShardTotal();
|
||||
//
|
||||
// XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal);
|
||||
//
|
||||
// // 业务逻辑
|
||||
// for (int i = 0; i < shardTotal; i++) {
|
||||
// if (i == shardIndex) {
|
||||
// XxlJobLogger.log("第 {} 片, 命中分片开始处理", i);
|
||||
// } else {
|
||||
// XxlJobLogger.log("第 {} 片, 忽略", i);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return ReturnT.SUCCESS;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 3、命令行任务
|
||||
// */
|
||||
// @XxlJob("commandJobHandler")
|
||||
// public ReturnT<String> commandJobHandler(String param) throws Exception {
|
||||
// String command = param;
|
||||
// int exitValue = -1;
|
||||
//
|
||||
// BufferedReader bufferedReader = null;
|
||||
// try {
|
||||
// // command process
|
||||
// Process process = Runtime.getRuntime().exec(command);
|
||||
// BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
|
||||
// bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
|
||||
//
|
||||
// // command log
|
||||
// String line;
|
||||
// while ((line = bufferedReader.readLine()) != null) {
|
||||
// XxlJobLogger.log(line);
|
||||
// }
|
||||
//
|
||||
// // command exit
|
||||
// process.waitFor();
|
||||
// exitValue = process.exitValue();
|
||||
// } catch (Exception e) {
|
||||
// XxlJobLogger.log(e);
|
||||
// } finally {
|
||||
// if (bufferedReader != null) {
|
||||
// bufferedReader.close();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (exitValue == 0) {
|
||||
// return IJobHandler.SUCCESS;
|
||||
// } else {
|
||||
// return new ReturnT<String>(IJobHandler.FAIL.getCode(), "command exit value("+exitValue+") is failed");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 4、跨平台Http任务
|
||||
// * 参数示例:
|
||||
// * "url: http://www.baidu.com\n" +
|
||||
// * "method: get\n" +
|
||||
// * "data: content\n";
|
||||
// */
|
||||
// @XxlJob("httpJobHandler")
|
||||
// public ReturnT<String> httpJobHandler(String param) throws Exception {
|
||||
//
|
||||
// // param parse
|
||||
// if (param==null || param.trim().length()==0) {
|
||||
// XxlJobLogger.log("param["+ param +"] invalid.");
|
||||
// return ReturnT.FAIL;
|
||||
// }
|
||||
// String[] httpParams = param.split("\n");
|
||||
// String url = null;
|
||||
// String method = null;
|
||||
// String data = null;
|
||||
// for (String httpParam: httpParams) {
|
||||
// if (httpParam.startsWith("url:")) {
|
||||
// url = httpParam.substring(httpParam.indexOf("url:") + 4).trim();
|
||||
// }
|
||||
// if (httpParam.startsWith("method:")) {
|
||||
// method = httpParam.substring(httpParam.indexOf("method:") + 7).trim().toUpperCase();
|
||||
// }
|
||||
// if (httpParam.startsWith("data:")) {
|
||||
// data = httpParam.substring(httpParam.indexOf("data:") + 5).trim();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // param valid
|
||||
// if (url==null || url.trim().length()==0) {
|
||||
// XxlJobLogger.log("url["+ url +"] invalid.");
|
||||
// return ReturnT.FAIL;
|
||||
// }
|
||||
// if (method==null || !Arrays.asList("GET", "POST").contains(method)) {
|
||||
// XxlJobLogger.log("method["+ method +"] invalid.");
|
||||
// return ReturnT.FAIL;
|
||||
// }
|
||||
// boolean isPostMethod = method.equals("POST");
|
||||
//
|
||||
// // request
|
||||
// HttpURLConnection connection = null;
|
||||
// BufferedReader bufferedReader = null;
|
||||
// try {
|
||||
// // connection
|
||||
// URL realUrl = new URL(url);
|
||||
// connection = (HttpURLConnection) realUrl.openConnection();
|
||||
//
|
||||
// // connection setting
|
||||
// connection.setRequestMethod(method);
|
||||
// connection.setDoOutput(isPostMethod);
|
||||
// connection.setDoInput(true);
|
||||
// connection.setUseCaches(false);
|
||||
// connection.setReadTimeout(5 * 1000);
|
||||
// connection.setConnectTimeout(3 * 1000);
|
||||
// connection.setRequestProperty("connection", "Keep-Alive");
|
||||
// connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
||||
// connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
|
||||
//
|
||||
// // do connection
|
||||
// connection.connect();
|
||||
//
|
||||
// // data
|
||||
// if (isPostMethod && data!=null && data.trim().length()>0) {
|
||||
// DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
|
||||
// dataOutputStream.write(data.getBytes("UTF-8"));
|
||||
// dataOutputStream.flush();
|
||||
// dataOutputStream.close();
|
||||
// }
|
||||
//
|
||||
// // valid StatusCode
|
||||
// int statusCode = connection.getResponseCode();
|
||||
// if (statusCode != 200) {
|
||||
// throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
||||
// }
|
||||
//
|
||||
// // result
|
||||
// bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
||||
// StringBuilder result = new StringBuilder();
|
||||
// String line;
|
||||
// while ((line = bufferedReader.readLine()) != null) {
|
||||
// result.append(line);
|
||||
// }
|
||||
// String responseMsg = result.toString();
|
||||
//
|
||||
// XxlJobLogger.log(responseMsg);
|
||||
// return ReturnT.SUCCESS;
|
||||
// } catch (Exception e) {
|
||||
// XxlJobLogger.log(e);
|
||||
// return ReturnT.FAIL;
|
||||
// } finally {
|
||||
// try {
|
||||
// if (bufferedReader != null) {
|
||||
// bufferedReader.close();
|
||||
// }
|
||||
// if (connection != null) {
|
||||
// connection.disconnect();
|
||||
// }
|
||||
// } catch (Exception e2) {
|
||||
// XxlJobLogger.log(e2);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑;
|
||||
// */
|
||||
// @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy")
|
||||
// public ReturnT<String> demoJobHandler2(String param) throws Exception {
|
||||
// XxlJobLogger.log("XXL-JOB, Hello World.");
|
||||
// return ReturnT.SUCCESS;
|
||||
// }
|
||||
// public void init(){
|
||||
// logger.info("init");
|
||||
// }
|
||||
// public void destroy(){
|
||||
// logger.info("destory");
|
||||
// }
|
||||
|
||||
|
||||
}
|
@ -180,3 +180,14 @@ management:
|
||||
cors:
|
||||
allowed-origins: "*"
|
||||
allowed-methods: "*"
|
||||
# 自定义的定时任务
|
||||
xxl:
|
||||
job:
|
||||
admin:
|
||||
addresses: http://10.242.31.158:8735/xxl-job-admin
|
||||
accessToken: Eshop@2021
|
||||
executor: appname:biz-service-ebtp-extend
|
||||
address: ''
|
||||
ip: ''
|
||||
logpath: /var/lib/docker/log/349553515466-uat/default-group/all
|
||||
logretentiondays: 30
|
Reference in New Issue
Block a user