From ca2ab5cfe08106c5c2ae79b8b1f26494449858c4 Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 22 May 2021 09:46:00 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20solon=20=E7=9A=84?= =?UTF-8?q?=E9=9B=86=E6=88=90=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 + xxl-job-core/pom.xml | 8 + .../executor/impl/XxlJobSolonExecutor.java | 87 ++++++ .../com/xxl/job/core/glue/GlueFactory.java | 4 +- .../job/core/glue/impl/SolonGlueFactory.java | 32 +++ xxl-job-executor-samples/pom.xml | 1 + .../xxl-job-executor-sample-solon/pom.xml | 103 +++++++ .../xxl/job/executor/XxlJobExecutorApp.java | 13 + .../xxl/job/executor/config/XxlJobConfig.java | 63 +++++ .../executor/controller/IndexController.java | 14 + .../job/executor/jobhandler/SampleXxlJob.java | 254 ++++++++++++++++++ .../src/main/resources/application.properties | 21 ++ .../src/main/resources/logback.xml | 29 ++ 13 files changed, 630 insertions(+), 1 deletion(-) create mode 100644 xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java create mode 100644 xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/XxlJobExecutorApp.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/controller/IndexController.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.properties create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/logback.xml diff --git a/pom.xml b/pom.xml index ac599310..9efc5498 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,8 @@ 5.3.3 2.4.2 + 1.4.3 + 2.1.4 8.0.23 diff --git a/xxl-job-core/pom.xml b/xxl-job-core/pom.xml index 70769714..f5a782f5 100644 --- a/xxl-job-core/pom.xml +++ b/xxl-job-core/pom.xml @@ -43,6 +43,14 @@ provided + + + org.noear + solon + ${solon.version} + provided + + diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java new file mode 100644 index 00000000..81aa39e7 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java @@ -0,0 +1,87 @@ +package com.xxl.job.core.executor.impl; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.glue.GlueFactory; +import com.xxl.job.core.handler.annotation.XxlJob; +import com.xxl.job.core.handler.impl.MethodJobHandler; +import org.noear.solon.SolonApp; +import org.noear.solon.core.Aop; +import org.noear.solon.core.BeanExtractor; +import org.noear.solon.core.BeanWrap; +import org.noear.solon.core.Plugin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; + +/** + * xxl-job executor (for solon) + * + * @author noear 2021/5/22 created + */ +public class XxlJobSolonExecutor extends XxlJobExecutor implements Plugin, BeanExtractor { + private static final Logger logger = LoggerFactory.getLogger(XxlJobSolonExecutor.class); + + + @Override + public void start(SolonApp app) { + // init JobHandler Repository (for method) + Aop.context().beanExtractorAdd(XxlJob.class, this); + + // refresh GlueFactory + GlueFactory.refreshInstance(2); + + // super start + try { + super.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + // destroy + @Override + public void destroy() { + super.destroy(); + } + + + @Override + public void doExtract(BeanWrap wrap, Method method, XxlJob anno) { + String name = anno.value(); + + if (name.trim().length() == 0) { + throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); + } + if (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 = wrap.clz().getDeclaredMethod(anno.init()); + initMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); + } + } + if (anno.destroy().trim().length() > 0) { + try { + destroyMethod = wrap.clz().getDeclaredMethod(anno.destroy()); + destroyMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); + } + } + + // registry jobhandler + registJobHandler(name, new MethodJobHandler(wrap.raw(), method, initMethod, destroyMethod)); + } +} \ No newline at end of file diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java index b727a851..5cab9381 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/GlueFactory.java @@ -1,5 +1,6 @@ package com.xxl.job.core.glue; +import com.xxl.job.core.glue.impl.SolonGlueFactory; import com.xxl.job.core.glue.impl.SpringGlueFactory; import com.xxl.job.core.handler.IJobHandler; import groovy.lang.GroovyClassLoader; @@ -16,7 +17,6 @@ import java.util.concurrent.ConcurrentMap; */ public class GlueFactory { - private static GlueFactory glueFactory = new GlueFactory(); public static GlueFactory getInstance(){ return glueFactory; @@ -26,6 +26,8 @@ public class GlueFactory { glueFactory = new GlueFactory(); } else if (type == 1) { glueFactory = new SpringGlueFactory(); + } else if (type == 2) { + glueFactory = new SolonGlueFactory(); } } diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java new file mode 100644 index 00000000..82395eb8 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java @@ -0,0 +1,32 @@ +package com.xxl.job.core.glue.impl; + +import com.xxl.job.core.glue.GlueFactory; +import org.noear.solon.Solon; +import org.noear.solon.core.Aop; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author noear 2021/5/22 created + */ +public class SolonGlueFactory extends GlueFactory { + private static Logger logger = LoggerFactory.getLogger(SolonGlueFactory.class); + + + /** + * inject action of solon + * @param instance + */ + @Override + public void injectService(Object instance){ + if (instance==null) { + return; + } + + if (Solon.global() == null) { + return; + } + + Aop.inject(instance); + } +} 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..9b3d81c9 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml @@ -0,0 +1,103 @@ + + + 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-parent + ${solon.version} + pom + import + + + + + + + + org.noear + solon-api + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + + + ${project.artifactId} + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + -parameters + 1.8 + 1.8 + UTF-8 + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.3.0 + + ${project.artifactId} + false + + jar-with-dependencies + + + + com.xxl.job.executor.XxlJobExecutorApp + + + + + + make-assembly + package + + single + + + + + + + + + + \ 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..85006ee7 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/XxlJobExecutorApp.java @@ -0,0 +1,13 @@ +package com.xxl.job.executor; + +import org.noear.solon.Solon; + +/** + * @author xuxueli 2018-10-28 00:38:13 + * @author noear 2021/5/22 created + */ +public class XxlJobExecutorApp { + public static void main(String[] args) { + Solon.start(XxlJobExecutorApp.class, args); + } +} \ 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/config/XxlJobConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java new file mode 100644 index 00000000..f5886108 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java @@ -0,0 +1,63 @@ +package com.xxl.job.executor.config; + +import com.xxl.job.core.executor.impl.XxlJobSolonExecutor; +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Configuration; +import org.noear.solon.annotation.Inject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * xxl-job config + * + * @author xuxueli 2017-04-28 + * @author noear 2021/5/22 created + */ +@Configuration +public class XxlJobConfig { + private Logger logger = LoggerFactory.getLogger(XxlJobConfig.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 XxlJobSolonExecutor xxlJobExecutor() { + logger.info(">>>>>>>>>>> xxl-job config init."); + + XxlJobSolonExecutor executor = new XxlJobSolonExecutor(); + + 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; + } + +} \ 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/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..19ce6ff9 --- /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,14 @@ +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/jobhandler/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java new file mode 100644 index 00000000..094e5de8 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java @@ -0,0 +1,254 @@ +package com.xxl.job.executor.jobhandler; + +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、任务开发:在Spring 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/5/22 created + */ +@Component +public class SampleXxlJob { + private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class); + + + /** + * 1、简单任务示例(Bean模式) + */ + @XxlJob("demoJobHandler") + public void demoJobHandler() throws Exception { + 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/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.properties new file mode 100644 index 00000000..c5e8ca49 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/application.properties @@ -0,0 +1,21 @@ +# web port +server.port=8081 + + +### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" +xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin + +### xxl-job, access token +xxl.job.accessToken= + +### xxl-job executor appname +xxl.job.executor.appname=xxl-job-executor-sample +### 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 +xxl.job.executor.ip= +xxl.job.executor.port=9999 +### xxl-job executor log-path +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler +### xxl-job executor log-retention-days +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..75167115 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/resources/logback.xml @@ -0,0 +1,29 @@ + + + + logback + + + + + %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 525cf9239a392013f1d8665a98dba0a4ad09faad Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 22 May 2021 14:35:42 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20solon=20=E7=9A=84?= =?UTF-8?q?=E9=9B=86=E6=88=90=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/impl/XxlJobSolonAutoConfig.java | 57 +++++++++++++++ .../executor/impl/XxlJobSolonExecutor.java | 63 +--------------- .../core/executor/impl/XxlJobSolonPlugin.java | 72 +++++++++++++++++++ .../job/core/glue/impl/SolonGlueFactory.java | 11 +-- .../solon/com.xxl.job.core.properties | 1 + 5 files changed, 134 insertions(+), 70 deletions(-) create mode 100644 xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java create mode 100644 xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java create mode 100644 xxl-job-core/src/main/resources/META-INF/solon/com.xxl.job.core.properties diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java new file mode 100644 index 00000000..21e7ac9e --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java @@ -0,0 +1,57 @@ +package com.xxl.job.core.executor.impl; + +import org.noear.solon.annotation.Bean; +import org.noear.solon.annotation.Configuration; +import org.noear.solon.annotation.Inject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author noear 2021/5/22 created + */ +@Configuration +public class XxlJobSolonAutoConfig { + private static final Logger logger = LoggerFactory.getLogger(XxlJobSolonAutoConfig.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 XxlJobSolonExecutor xxlJobExecutor() { + logger.info(">>>>>>>>>>> xxl-job config init."); + + XxlJobSolonExecutor executor = new XxlJobSolonExecutor(); + + 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-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java index 81aa39e7..24db21ac 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java @@ -2,41 +2,24 @@ package com.xxl.job.core.executor.impl; import com.xxl.job.core.executor.XxlJobExecutor; import com.xxl.job.core.glue.GlueFactory; -import com.xxl.job.core.handler.annotation.XxlJob; -import com.xxl.job.core.handler.impl.MethodJobHandler; -import org.noear.solon.SolonApp; -import org.noear.solon.core.Aop; -import org.noear.solon.core.BeanExtractor; -import org.noear.solon.core.BeanWrap; -import org.noear.solon.core.Plugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.lang.reflect.Method; - /** * xxl-job executor (for solon) * * @author noear 2021/5/22 created */ -public class XxlJobSolonExecutor extends XxlJobExecutor implements Plugin, BeanExtractor { +public class XxlJobSolonExecutor extends XxlJobExecutor { private static final Logger logger = LoggerFactory.getLogger(XxlJobSolonExecutor.class); - @Override - public void start(SolonApp app) { - // init JobHandler Repository (for method) - Aop.context().beanExtractorAdd(XxlJob.class, this); - + public void start() throws Exception { // refresh GlueFactory GlueFactory.refreshInstance(2); // super start - try { - super.start(); - } catch (Exception e) { - throw new RuntimeException(e); - } + super.start(); } // destroy @@ -44,44 +27,4 @@ public class XxlJobSolonExecutor extends XxlJobExecutor implements Plugin, BeanE public void destroy() { super.destroy(); } - - - @Override - public void doExtract(BeanWrap wrap, Method method, XxlJob anno) { - String name = anno.value(); - - if (name.trim().length() == 0) { - throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); - } - if (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 = wrap.clz().getDeclaredMethod(anno.init()); - initMethod.setAccessible(true); - } catch (NoSuchMethodException e) { - throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); - } - } - if (anno.destroy().trim().length() > 0) { - try { - destroyMethod = wrap.clz().getDeclaredMethod(anno.destroy()); - destroyMethod.setAccessible(true); - } catch (NoSuchMethodException e) { - throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); - } - } - - // registry jobhandler - registJobHandler(name, new MethodJobHandler(wrap.raw(), method, initMethod, destroyMethod)); - } } \ No newline at end of file diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java new file mode 100644 index 00000000..b13910c2 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java @@ -0,0 +1,72 @@ +package com.xxl.job.core.executor.impl; + +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.SolonApp; +import org.noear.solon.core.Aop; +import org.noear.solon.core.BeanExtractor; +import org.noear.solon.core.BeanWrap; +import org.noear.solon.core.Plugin; + +import java.lang.reflect.Method; + +/** + * @author noear 2021/5/22 created + */ +public class XxlJobSolonPlugin implements Plugin, BeanExtractor { + @Override + public void start(SolonApp app) { + Aop.context().beanExtractorAdd(XxlJob.class, this); + Aop.context().beanMake(XxlJobSolonAutoConfig.class); + + XxlJobSolonExecutor executor = Aop.get(XxlJobSolonExecutor.class); + + Aop.beanOnloaded(() -> { + try { + executor.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Override + public void doExtract(BeanWrap wrap, Method method, XxlJob anno) { + String name = anno.value(); + + if (name.trim().length() == 0) { + throw new RuntimeException("xxl-job method-jobhandler name invalid, for[" + wrap.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 = wrap.clz().getDeclaredMethod(anno.init()); + initMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler initMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); + } + } + if (anno.destroy().trim().length() > 0) { + try { + destroyMethod = wrap.clz().getDeclaredMethod(anno.destroy()); + destroyMethod.setAccessible(true); + } catch (NoSuchMethodException e) { + throw new RuntimeException("xxl-job method-jobhandler destroyMethod invalid, for[" + wrap.clz() + "#" + method.getName() + "] ."); + } + } + + // registry jobhandler + XxlJobExecutor.registJobHandler(name, new MethodJobHandler(wrap.raw(), method, initMethod, destroyMethod)); + } +} diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java b/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java index 82395eb8..3a9c40aa 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java @@ -1,20 +1,15 @@ package com.xxl.job.core.glue.impl; import com.xxl.job.core.glue.GlueFactory; -import org.noear.solon.Solon; import org.noear.solon.core.Aop; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * @author noear 2021/5/22 created */ public class SolonGlueFactory extends GlueFactory { - private static Logger logger = LoggerFactory.getLogger(SolonGlueFactory.class); - - /** * inject action of solon + * * @param instance */ @Override @@ -23,10 +18,6 @@ public class SolonGlueFactory extends GlueFactory { return; } - if (Solon.global() == null) { - return; - } - Aop.inject(instance); } } diff --git a/xxl-job-core/src/main/resources/META-INF/solon/com.xxl.job.core.properties b/xxl-job-core/src/main/resources/META-INF/solon/com.xxl.job.core.properties new file mode 100644 index 00000000..f3ecb6a6 --- /dev/null +++ b/xxl-job-core/src/main/resources/META-INF/solon/com.xxl.job.core.properties @@ -0,0 +1 @@ +solon.plugin=com.xxl.job.core.executor.impl.XxlJobSolonPlugin \ No newline at end of file From 11adcba3a72d8f203f7b935e9e061579c0b6f74e Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 22 May 2021 14:41:09 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20solon=20=E7=9A=84?= =?UTF-8?q?=E9=9B=86=E6=88=90=E9=80=82=E9=85=8D=E5=8F=8A=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../executor/impl/XxlJobSolonAutoConfig.java | 5 +- .../executor/impl/XxlJobSolonExecutor.java | 2 +- .../core/executor/impl/XxlJobSolonPlugin.java | 7 ++- .../xxl-job-executor-sample-solon/pom.xml | 2 +- .../xxl/job/executor/config/XxlJobConfig.java | 63 ------------------- .../job/executor/jobhandler/SampleXxlJob.java | 3 +- 6 files changed, 12 insertions(+), 70 deletions(-) delete mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java index 21e7ac9e..45248947 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java @@ -1,5 +1,6 @@ package com.xxl.job.core.executor.impl; +import com.xxl.job.core.executor.XxlJobExecutor; import org.noear.solon.annotation.Bean; import org.noear.solon.annotation.Configuration; import org.noear.solon.annotation.Inject; @@ -7,6 +8,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** + * auto config XxlJobExecutor + * * @author noear 2021/5/22 created */ @Configuration @@ -38,7 +41,7 @@ public class XxlJobSolonAutoConfig { private int logRetentionDays; @Bean - public XxlJobSolonExecutor xxlJobExecutor() { + public XxlJobExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobSolonExecutor executor = new XxlJobSolonExecutor(); diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java index 24db21ac..2fb71574 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java @@ -15,7 +15,7 @@ public class XxlJobSolonExecutor extends XxlJobExecutor { @Override public void start() throws Exception { - // refresh GlueFactory + // refresh GlueFactory (add solon inject) GlueFactory.refreshInstance(2); // super start diff --git a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java index b13910c2..c9bd3eaf 100644 --- a/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java @@ -17,13 +17,16 @@ import java.lang.reflect.Method; public class XxlJobSolonPlugin implements Plugin, BeanExtractor { @Override public void start(SolonApp app) { + //add extractor for bean method Aop.context().beanExtractorAdd(XxlJob.class, this); - Aop.context().beanMake(XxlJobSolonAutoConfig.class); - XxlJobSolonExecutor executor = Aop.get(XxlJobSolonExecutor.class); + Aop.context().beanMake(XxlJobSolonAutoConfig.class); Aop.beanOnloaded(() -> { try { + // + XxlJobExecutor executor = Aop.get(XxlJobExecutor.class); + executor.start(); } catch (Exception e) { throw new RuntimeException(e); 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 index 9b3d81c9..adbc6ad6 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml @@ -35,7 +35,7 @@ org.noear - solon-api + solon diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java deleted file mode 100644 index f5886108..00000000 --- a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/config/XxlJobConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.xxl.job.executor.config; - -import com.xxl.job.core.executor.impl.XxlJobSolonExecutor; -import org.noear.solon.annotation.Bean; -import org.noear.solon.annotation.Configuration; -import org.noear.solon.annotation.Inject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * xxl-job config - * - * @author xuxueli 2017-04-28 - * @author noear 2021/5/22 created - */ -@Configuration -public class XxlJobConfig { - private Logger logger = LoggerFactory.getLogger(XxlJobConfig.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 XxlJobSolonExecutor xxlJobExecutor() { - logger.info(">>>>>>>>>>> xxl-job config init."); - - XxlJobSolonExecutor executor = new XxlJobSolonExecutor(); - - 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; - } - -} \ 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/jobhandler/SampleXxlJob.java b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java index 094e5de8..d9bedeb3 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/src/main/java/com/xxl/job/executor/jobhandler/SampleXxlJob.java @@ -19,7 +19,7 @@ import java.util.concurrent.TimeUnit; * XxlJob开发示例(Bean模式) * * 开发步骤: - * 1、任务开发:在Spring Bean实例中,开发Job方法; + * 1、任务开发:在Solon Bean实例中,开发Job方法; * 2、注解配置:为Job方法添加注解 "@XxlJob(value="自定义jobhandler名称", init = "JobHandler初始化方法", destroy = "JobHandler销毁方法")",注解value值对应的是调度中心新建任务的JobHandler属性的值。 * 3、执行日志:需要通过 "XxlJobHelper.log" 打印执行日志; * 4、任务结果:默认任务结果为 "成功" 状态,不需要主动设置;如有诉求,比如设置任务结果为失败,可以通过 "XxlJobHelper.handleFail/handleSuccess" 自主设置任务结果; @@ -250,5 +250,4 @@ public class SampleXxlJob { logger.info("destory"); } - } From fd82106d76ce7a5df4adcda1f9f97b245f8e59a8 Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 22 May 2021 15:11:19 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20solon=20=E7=9A=84?= =?UTF-8?q?=E9=9B=86=E6=88=90=E9=80=82=E9=85=8D=E5=8F=8A=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index adbc6ad6..9b3d81c9 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml @@ -35,7 +35,7 @@ org.noear - solon + solon-api From 5afa0682b763f3e232e94841ab498c233130efa4 Mon Sep 17 00:00:00 2001 From: noear Date: Sat, 22 May 2021 15:12:20 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20solon=20=E7=9A=84?= =?UTF-8?q?=E9=9B=86=E6=88=90=E9=80=82=E9=85=8D=E5=8F=8A=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9b3d81c9..cb199dc8 100644 --- a/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml +++ b/xxl-job-executor-samples/xxl-job-executor-sample-solon/pom.xml @@ -32,7 +32,7 @@ - + org.noear solon-api