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/XxlJobSolonAutoConfig.java b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java new file mode 100644 index 00000000..45248947 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonAutoConfig.java @@ -0,0 +1,60 @@ +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * auto config XxlJobExecutor + * + * @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 XxlJobExecutor 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 new file mode 100644 index 00000000..2fb71574 --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonExecutor.java @@ -0,0 +1,30 @@ +package com.xxl.job.core.executor.impl; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.glue.GlueFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * xxl-job executor (for solon) + * + * @author noear 2021/5/22 created + */ +public class XxlJobSolonExecutor extends XxlJobExecutor { + private static final Logger logger = LoggerFactory.getLogger(XxlJobSolonExecutor.class); + + @Override + public void start() throws Exception { + // refresh GlueFactory (add solon inject) + GlueFactory.refreshInstance(2); + + // super start + super.start(); + } + + // destroy + @Override + public void destroy() { + super.destroy(); + } +} \ 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..c9bd3eaf --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/executor/impl/XxlJobSolonPlugin.java @@ -0,0 +1,75 @@ +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) { + //add extractor for bean method + Aop.context().beanExtractorAdd(XxlJob.class, this); + + Aop.context().beanMake(XxlJobSolonAutoConfig.class); + + Aop.beanOnloaded(() -> { + try { + // + XxlJobExecutor executor = Aop.get(XxlJobExecutor.class); + + 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/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..3a9c40aa --- /dev/null +++ b/xxl-job-core/src/main/java/com/xxl/job/core/glue/impl/SolonGlueFactory.java @@ -0,0 +1,23 @@ +package com.xxl.job.core.glue.impl; + +import com.xxl.job.core.glue.GlueFactory; +import org.noear.solon.core.Aop; + +/** + * @author noear 2021/5/22 created + */ +public class SolonGlueFactory extends GlueFactory { + /** + * inject action of solon + * + * @param instance + */ + @Override + public void injectService(Object instance){ + if (instance==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 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..cb199dc8 --- /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/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..d9bedeb3 --- /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,253 @@ +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、任务开发:在Solon 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