diff --git a/pom.xml b/pom.xml index ac599310..9f6af260 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ xxl-job-core xxl-job-admin xxl-job-executor-samples + xxl-job-solon-plugin @@ -30,6 +31,8 @@ 5.3.3 2.4.2 + 1.4.9 + 2.1.4 8.0.23 diff --git a/xxl-job-executor-samples/pom.xml b/xxl-job-executor-samples/pom.xml index ef1fba22..e98b13b8 100644 --- a/xxl-job-executor-samples/pom.xml +++ b/xxl-job-executor-samples/pom.xml @@ -13,6 +13,7 @@ xxl-job-executor-sample-frameless xxl-job-executor-sample-springboot + xxl-job-executor-sample-solon \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml new file mode 100644 index 00000000..f8cce7e5 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.xuxueli + xxl-job-executor-samples + 2.3.0 + + xxl-job-executor-sample-solon + jar + + ${project.artifactId} + Example executor project for spring boot. + https://www.xuxueli.com/ + + + + org.noear + solon-api + ${solon.version} + + + + + com.xuxueli + xxl-job-solon-plugin + ${project.parent.version} + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + repackage + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/XxlJobExecutorApp.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/XxlJobExecutorApp.java new file mode 100644 index 00000000..e026a4d1 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/XxlJobExecutorApp.java @@ -0,0 +1,12 @@ +package com.xxl.job.executor; + +import org.noear.solon.Solon; + +/** + * @author noear 2021/5/25 created + */ +public class XxlJobExecutorApp { + public static void main(String[] args){ + Solon.start(XxlJobExecutorApp.class, args); + } +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/controller/IndexController.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/controller/IndexController.java new file mode 100644 index 00000000..90ec17ae --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/controller/IndexController.java @@ -0,0 +1,13 @@ +package com.xxl.job.executor.controller;//package com.xxl.job.executor.mvc.controller; + + +//import org.noear.solon.annotation.Controller; +//import org.noear.solon.annotation.Mapping; +// +//@Controller +//public class IndexController { +// @Mapping("/") +// public String index() { +// return "xxl job executor running."; +// } +//} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob.java new file mode 100644 index 00000000..00da824d --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob.java @@ -0,0 +1,256 @@ +package com.xxl.job.executor.dso.jobs; + +import com.xxl.job.core.context.XxlJobHelper; +import com.xxl.job.core.handler.annotation.XxlJob; +import org.noear.solon.annotation.Component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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、任务开发:在Bean实例中,开发Job方法; + * 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。 + * 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志; + * 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果; + * + * @author xuxueli 2019-12-11 21:52:51 + * @author noear 2021-05025 + */ +@Component +public class SampleXxlJob { + private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class); + + + /** + * 1、简单任务示例(Bean模式) + */ + @XxlJob("demoJobHandler") + public void demoJobHandler() throws Exception { + System.out.println("demoJobHandler"); + + XxlJobHelper.log("XXL-JOB, Hello World."); + + for (int i = 0; i < 5; i++) { + XxlJobHelper.log("beat at:" + i); + TimeUnit.SECONDS.sleep(2); + } + // default success + } + + + /** + * 2、分片广播任务 + */ + @XxlJob("shardingJobHandler") + public void shardingJobHandler() throws Exception { + + // 分片参数 + int shardIndex = XxlJobHelper.getShardIndex(); + int shardTotal = XxlJobHelper.getShardTotal(); + + XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); + + // 业务逻辑 + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { + XxlJobHelper.log("第 {} 片, 命中分片开始处理", i); + } else { + XxlJobHelper.log("第 {} 片, 忽略", i); + } + } + + } + + + /** + * 3、命令行任务 + */ + @XxlJob("commandJobHandler") + public void commandJobHandler() throws Exception { + String command = XxlJobHelper.getJobParam(); + int exitValue = -1; + + BufferedReader bufferedReader = null; + try { + // command process + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(command); + processBuilder.redirectErrorStream(true); + + Process process = processBuilder.start(); + //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) { + XxlJobHelper.log(line); + } + + // command exit + process.waitFor(); + exitValue = process.exitValue(); + } catch (Exception e) { + XxlJobHelper.log(e); + } finally { + if (bufferedReader != null) { + bufferedReader.close(); + } + } + + if (exitValue == 0) { + // default success + } else { + XxlJobHelper.handleFail("command exit value("+exitValue+") is failed"); + } + + } + + + /** + * 4、跨平台Http任务 + * 参数示例: + * "url: http://www.baidu.com\n" + + * "method: get\n" + + * "data: content\n"; + */ + @XxlJob("httpJobHandler") + public void httpJobHandler() throws Exception { + + // param parse + String param = XxlJobHelper.getJobParam(); + if (param==null || param.trim().length()==0) { + XxlJobHelper.log("param["+ param +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + + 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) { + XxlJobHelper.log("url["+ url +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + if (method==null || !Arrays.asList("GET", "POST").contains(method)) { + XxlJobHelper.log("method["+ method +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + 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(); + + XxlJobHelper.log(responseMsg); + + return; + } catch (Exception e) { + XxlJobHelper.log(e); + + XxlJobHelper.handleFail(); + return; + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + if (connection != null) { + connection.disconnect(); + } + } catch (Exception e2) { + XxlJobHelper.log(e2); + } + } + + } + + /** + * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑; + */ + @XxlJob(value = "demoJobHandler2", init = "init", destroy = "destroy") + public void demoJobHandler2() throws Exception { + XxlJobHelper.log("XXL-JOB, Hello World."); + } + public void init(){ + logger.info("init"); + } + public void destroy(){ + logger.info("destory"); + } + + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob2.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob2.java new file mode 100644 index 00000000..3ff0f66d --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/dso/jobs/SampleXxlJob2.java @@ -0,0 +1,249 @@ +package com.xxl.job.executor.dso.jobs; + +import com.xxl.job.core.context.XxlJobHelper; +import org.noear.solon.annotation.Component; +import org.noear.solon.cloud.annotation.CloudJob; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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模式),使用Solon Cloud 的 @CloudJob注解 + * + * 开发步骤: + * 1、任务开发:在Bean实例中,开发Job方法; + * 2、注解配置:为Job方法添加注解 "@CloudJob(value="自定义jobhandler名称")",注解value值对应的是调度中心新建任务的JobHandler属性的值。 + * 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志; + * 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果; + * + * @author xuxueli 2019-12-11 21:52:51 + * @author noear 2021-05025 + */ +@Component +public class SampleXxlJob2 { + private static Logger logger = LoggerFactory.getLogger(SampleXxlJob2.class); + + + /** + * 1、简单任务示例(Bean模式) + */ + @CloudJob("demoJobHandler-cj") + public void demoJobHandler() throws Exception { + System.out.println("demoJobHandler-cj"); + + XxlJobHelper.log("XXL-JOB, Hello World."); + + for (int i = 0; i < 5; i++) { + XxlJobHelper.log("beat at:" + i); + TimeUnit.SECONDS.sleep(2); + } + // default success + } + + + /** + * 2、分片广播任务 + */ + @CloudJob("shardingJobHandler-cj") + public void shardingJobHandler() throws Exception { + + // 分片参数 + int shardIndex = XxlJobHelper.getShardIndex(); + int shardTotal = XxlJobHelper.getShardTotal(); + + XxlJobHelper.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardIndex, shardTotal); + + // 业务逻辑 + for (int i = 0; i < shardTotal; i++) { + if (i == shardIndex) { + XxlJobHelper.log("第 {} 片, 命中分片开始处理", i); + } else { + XxlJobHelper.log("第 {} 片, 忽略", i); + } + } + + } + + + /** + * 3、命令行任务 + */ + @CloudJob("commandJobHandler-cj") + public void commandJobHandler() throws Exception { + String command = XxlJobHelper.getJobParam(); + int exitValue = -1; + + BufferedReader bufferedReader = null; + try { + // command process + ProcessBuilder processBuilder = new ProcessBuilder(); + processBuilder.command(command); + processBuilder.redirectErrorStream(true); + + Process process = processBuilder.start(); + //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) { + XxlJobHelper.log(line); + } + + // command exit + process.waitFor(); + exitValue = process.exitValue(); + } catch (Exception e) { + XxlJobHelper.log(e); + } finally { + if (bufferedReader != null) { + bufferedReader.close(); + } + } + + if (exitValue == 0) { + // default success + } else { + XxlJobHelper.handleFail("command exit value("+exitValue+") is failed"); + } + + } + + + /** + * 4、跨平台Http任务 + * 参数示例: + * "url: http://www.baidu.com\n" + + * "method: get\n" + + * "data: content\n"; + */ + @CloudJob("httpJobHandler-cj") + public void httpJobHandler() throws Exception { + + // param parse + String param = XxlJobHelper.getJobParam(); + if (param==null || param.trim().length()==0) { + XxlJobHelper.log("param["+ param +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + + 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) { + XxlJobHelper.log("url["+ url +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + if (method==null || !Arrays.asList("GET", "POST").contains(method)) { + XxlJobHelper.log("method["+ method +"] invalid."); + + XxlJobHelper.handleFail(); + return; + } + 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(); + + XxlJobHelper.log(responseMsg); + + return; + } catch (Exception e) { + XxlJobHelper.log(e); + + XxlJobHelper.handleFail(); + return; + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + if (connection != null) { + connection.disconnect(); + } + } catch (Exception e2) { + XxlJobHelper.log(e2); + } + } + + } + + /** + * 5、生命周期任务示例:任务初始化与销毁时,支持自定义相关逻辑; + */ + @CloudJob(value = "demoJobHandler2-cj") + public void demoJobHandler2() throws Exception { + XxlJobHelper.log("XXL-JOB, Hello World."); + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.yml b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.yml new file mode 100644 index 00000000..a5efcae7 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server.port: 8082 + +solon.app: + name: xxl-job-executor-solon-demo + group: demo + + +solon.cloud.xxljob: + server: "http://localhost:8093/xxl-job-admin" + +solon.logging.logger: + "io.netty.*": + level: INFO + + +### xxl-job admin address list, (默认:${solon.cloud.xxljob.server}) +#xxl.job.admin.addresses: "http://172.168.0.163:30126/xxl-job-admin" +### xxl-job access token (默认:${solon.cloud.xxljob.password}) +#xxl.job.accessToken: +### xxl-job executor appname(默认:${solon.app.name}) +#xxl.job.executor.appname: +### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null +#xxl.job.executor.address: +### xxl-job executor server-info(默认:本机内网ip) +#xxl.job.executor.ip: +### xxl-job executor server-info(默认:9999) +#xxl.job.executor.port: 9999 +### xxl-job executor log-path(默认:/data/logs/xxl-job/jobhandler) +#xxl.job.executor.logpath: "/data/logs/xxl-job/jobhandler" +### xxl-job executor log-retention-days(默认:30) +#xxl.job.executor.logretentiondays: 30 diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/logback.xml b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/logback.xml new file mode 100644 index 00000000..c5be5634 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + logback + + + + + %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/xxl-job-solon-plugin/pom.xml b/xxl-job-solon-plugin/pom.xml new file mode 100644 index 00000000..ce44ff3b --- /dev/null +++ b/xxl-job-solon-plugin/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + + + com.xuxueli + xxl-job + 2.3.0 + + + xxl-job-solon-plugin + jar + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + org.noear + solon.cloud + ${solon.version} + + + org.noear + solon.logging.impl + + + + + \ No newline at end of file diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XPluginImp.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XPluginImp.java new file mode 100644 index 00000000..388b4000 --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XPluginImp.java @@ -0,0 +1,40 @@ +package com.xxl.job.solon; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.handler.annotation.XxlJob; +import org.noear.solon.SolonApp; +import org.noear.solon.cloud.CloudManager; +import com.xxl.job.solon.service.CloudJobServiceImpl; +import org.noear.solon.core.Aop; +import org.noear.solon.core.Plugin; + +/** + * @author noear + * @since 1.4 + */ +public class XPluginImp implements Plugin { + @Override + public void start(SolonApp app) { + if (XxljobProps.instance.getJobEnable() == false) { + return; + } + + //注册Job服务 + CloudManager.register(CloudJobServiceImpl.instance); + + //注册构建器和提取器 + Aop.context().beanExtractorAdd(XxlJob.class, new XxlJobExtractor()); + + //构建自动配置 + Aop.context().beanMake(XxlJobAutoConfig.class); + + Aop.beanOnloaded(() -> { + try { + XxlJobExecutor executor = Aop.get(XxlJobExecutor.class); + executor.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } +} diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobAutoConfig.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobAutoConfig.java new file mode 100644 index 00000000..d5ed4e95 --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobAutoConfig.java @@ -0,0 +1,93 @@ +package com.xxl.job.solon; + +import com.xxl.job.core.executor.XxlJobExecutor; +import org.noear.solon.Solon; +import org.noear.solon.Utils; +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Configuration; +import org.noear.solon.annotation.Inject; +import org.noear.solon.cloud.utils.LocalUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * auto config XxlJobExecutor + * + * @author noear + * @since 1.4 + */ +@Configuration +public class XxlJobAutoConfig { + private static final Logger logger = LoggerFactory.getLogger(XxlJobAutoConfig.class); + + @Inject("${xxl.job.admin.addresses}") + private String adminAddresses; + + @Inject("${xxl.job.accessToken}") + private String accessToken; + + @Inject("${xxl.job.executor.appname}") + private String appname; + + @Inject("${xxl.job.executor.address}") + private String address; + + @Inject("${xxl.job.executor.ip}") + private String ip; + + @Inject("${xxl.job.executor.port}") + private int port; + + @Inject("${xxl.job.executor.logpath}") + private String logPath; + + @Inject("${xxl.job.executor.logretentiondays}") + private int logRetentionDays; + + @Bean + public XxlJobExecutor xxlJobExecutor() { + logger.info(">>>>>>>>>>> xxl-job config init."); + + if(Utils.isEmpty(adminAddresses)){ + adminAddresses = XxljobProps.instance.getJobServer(); + } + + if(Utils.isEmpty(appname)){ + appname = Solon.cfg().appName(); + } + + if(Utils.isEmpty(ip)){ + ip = LocalUtils.getLocalAddress(); + } + + if(port < 1000){ + port = 9999; + } + + if(logRetentionDays < 1){ + logRetentionDays = 30; + } + + if(Utils.isEmpty(logPath)){ + logPath = "/data/applogs/xxl-job/jobhandler"; + } + + if(Utils.isEmpty(accessToken)) { + accessToken = XxljobProps.instance.getPassword(); + } + + + XxlJobExecutor executor = new XxlJobExecutor(); + + executor.setAdminAddresses(adminAddresses); + executor.setAppname(appname); + executor.setAddress(address); + executor.setIp(ip); + executor.setPort(port); + executor.setAccessToken(accessToken); + executor.setLogPath(logPath); + executor.setLogRetentionDays(logRetentionDays); + + return executor; + } +} diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobExtractor.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobExtractor.java new file mode 100644 index 00000000..daa8ac0d --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxlJobExtractor.java @@ -0,0 +1,54 @@ +package com.xxl.job.solon; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.handler.annotation.XxlJob; +import com.xxl.job.core.handler.impl.MethodJobHandler; +import org.noear.solon.core.BeanExtractor; +import org.noear.solon.core.BeanWrap; + +import java.lang.reflect.Method; + +/** + * @author noear + * @since 1.4 + */ +class XxlJobExtractor implements BeanExtractor { + @Override + public void doExtract(BeanWrap bw, Method method, XxlJob anno) { + String name = anno.value(); + + if (name.trim().length() == 0) { + throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + bw.clz() + "#" + method.getName() + "] ."); + } + if (XxlJobExecutor.loadJobHandler(name) != null) { + throw new RuntimeException("xxl-job jobhandler[" + name + "] naming conflicts."); + } + + + method.setAccessible(true); + + // init and destory + Method initMethod = null; + Method destroyMethod = null; + + if (anno.init().trim().length() > 0) { + try { + initMethod = bw.clz().getDeclaredMethod(anno.init()); + initMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + bw.clz() + "#" + method.getName() + "] ."); + } + } + if (anno.destroy().trim().length() > 0) { + try { + destroyMethod = bw.clz().getDeclaredMethod(anno.destroy()); + destroyMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + bw.clz() + "#" + method.getName() + "] ."); + } + } + + // registry jobhandler + XxlJobExecutor.registJobHandler(name, new MethodJobHandler(bw.raw(), method, initMethod, destroyMethod)); + } +} diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxljobProps.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxljobProps.java new file mode 100644 index 00000000..64915da0 --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/XxljobProps.java @@ -0,0 +1,10 @@ +package com.xxl.job.solon; + +import org.noear.solon.cloud.CloudProps; + +/** + * @author noear 2021/5/25 created + */ +public class XxljobProps { + public static final CloudProps instance = new CloudProps("xxljob"); +} diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobHanderProxy.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobHanderProxy.java new file mode 100644 index 00000000..f4e7867c --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobHanderProxy.java @@ -0,0 +1,44 @@ +package com.xxl.job.solon.service; + +import com.xxl.job.core.handler.IJobHandler; +import org.noear.solon.core.handle.Context; +import org.noear.solon.core.handle.ContextEmpty; +import org.noear.solon.core.handle.ContextUtil; +import org.noear.solon.core.handle.Handler; + +/** + * @author noear + * @since 1.4 + */ +class CloudJobHanderProxy extends IJobHandler { + Handler real; + + public CloudJobHanderProxy(Handler handler) { + real = handler; + } + + @Override + public void execute() throws Exception { + Context ctx = Context.current(); + + try { + if (ctx == null) { + ctx = new ContextEmpty(); + try { + ContextUtil.currentSet(ctx); + real.handle(ctx); + } finally { + ContextUtil.currentSet(ctx); + } + } else { + real.handle(ctx); + } + } catch (Throwable e) { + if (e instanceof Exception) { + throw (Exception) e; + } else { + throw new RuntimeException(e); + } + } + } +} \ No newline at end of file diff --git a/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobServiceImpl.java b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobServiceImpl.java new file mode 100644 index 00000000..b8fe33ce --- /dev/null +++ b/xxl-job-solon-plugin/src/main/java/com/xxl/job/solon/service/CloudJobServiceImpl.java @@ -0,0 +1,24 @@ +package com.xxl.job.solon.service; + +import com.xxl.job.core.executor.XxlJobExecutor; +import org.noear.solon.cloud.service.CloudJobService; +import org.noear.solon.core.handle.Handler; + +/** + * @author noear + * @since 1.4 + */ +public class CloudJobServiceImpl implements CloudJobService { + public static final CloudJobService instance = new CloudJobServiceImpl(); + + @Override + public boolean register(String name, String description, Handler handler) { + XxlJobExecutor.registJobHandler(name, new CloudJobHanderProxy(handler)); + return true; + } + + @Override + public boolean isRegistered(String name) { + return XxlJobExecutor.loadJobHandler(name) != null; + } +} diff --git a/xxl-job-solon-plugin/src/main/resources/META-INF/solon/com.xxl.job.solon.properties b/xxl-job-solon-plugin/src/main/resources/META-INF/solon/com.xxl.job.solon.properties new file mode 100644 index 00000000..eb1a1b67 --- /dev/null +++ b/xxl-job-solon-plugin/src/main/resources/META-INF/solon/com.xxl.job.solon.properties @@ -0,0 +1 @@ +solon.plugin=com.xxl.job.solon.XPluginImp \ No newline at end of file