From c722ac1ae8908c07a8245f0f13fec22406a77f79 Mon Sep 17 00:00:00 2001 From: liuhongfeng Date: Sun, 12 May 2019 08:47:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Jboot=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E7=9A=84=E5=BA=94=E7=94=A8=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/pom.xml | 1 + .../xxl-job-executor-sample-jboot/pom.xml | 129 +++++++++++++++ .../src/main/java/com/xuxueli/JbootApp.java | 14 ++ .../sample/jboot/config/JbootConfig.java | 68 ++++++++ .../jboot/controller/IndexController.java | 15 ++ .../jboot/jobhandler/CommandJobHandler.java | 54 +++++++ .../jboot/jobhandler/DemoJobHandler.java | 32 ++++ .../jboot/jobhandler/HttpJobHandler.java | 81 ++++++++++ .../jboot/jobhandler/ShardingJobHandler.java | 34 ++++ .../src/main/resources/jboot.properties | 19 +++ .../src/main/resources/logback.xml | 149 ++++++++++++++++++ .../src/test/java/com/xuxueli/AppTest.java | 20 +++ 12 files changed, 616 insertions(+) create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/pom.xml create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/JbootApp.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/config/JbootConfig.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/controller/IndexController.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/CommandJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/DemoJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/jboot.properties create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/logback.xml create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/test/java/com/xuxueli/AppTest.java diff --git a/xxl-job-executor-samples/pom.xml b/xxl-job-executor-samples/pom.xml index e2e6a2d3..dc270c56 100644 --- a/xxl-job-executor-samples/pom.xml +++ b/xxl-job-executor-samples/pom.xml @@ -16,6 +16,7 @@ xxl-job-executor-sample-jfinal xxl-job-executor-sample-nutz xxl-job-executor-sample-frameless + xxl-job-executor-sample-jboot \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/pom.xml b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/pom.xml new file mode 100644 index 00000000..f75734f5 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/pom.xml @@ -0,0 +1,129 @@ + + + + + xxl-job-executor-samples + com.xuxueli + 2.1.0-SNAPSHOT + + 4.0.0 + + xxl-job-executor-sample-jboot + + ${project.artifactId} + Example executor project for spring boot. + http://www.xuxueli.com/ + + + UTF-8 + 1.7 + 1.7 + + + + + + junit + junit + 4.11 + test + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + io.jboot + jboot + 2.0.9 + + + + + ch.qos.logback + logback-core + 1.1.11 + + + + ch.qos.logback + logback-classic + 1.1.11 + + + + + + + + + src/main/java + + **/*.xml + + true + + + src/main/resources + + **/* + + true + + + src/main/webapp + + **/ + + true + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + org.codehaus.mojo + appassembler-maven-plugin + 1.10 + + ${project.build.directory}/app + lib + bin + webRoot + true + src/main/resources + flat + UTF-8 + logs + tmp + + + + io.jboot.Jboot + jboot + + windows + unix + + + + + + + + + + + diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/JbootApp.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/JbootApp.java new file mode 100644 index 00000000..d4aea99f --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/JbootApp.java @@ -0,0 +1,14 @@ +package com.xuxueli; + +import io.jboot.app.JbootApplication; + +/** + * Jboot app + * + * @author https://github.com/souvc + */ +public class JbootApp { + public static void main(String[] args) { + JbootApplication.run(args); + } +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/config/JbootConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/config/JbootConfig.java new file mode 100644 index 00000000..61fa967c --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/config/JbootConfig.java @@ -0,0 +1,68 @@ +package com.xuxueli.executor.sample.jboot.config; + +import com.xuxueli.executor.sample.jboot.jobhandler.CommandJobHandler; +import com.xuxueli.executor.sample.jboot.jobhandler.DemoJobHandler; +import com.xuxueli.executor.sample.jboot.jobhandler.HttpJobHandler; +import com.xuxueli.executor.sample.jboot.jobhandler.ShardingJobHandler; +import com.xxl.job.core.executor.XxlJobExecutor; +import io.jboot.Jboot; +import io.jboot.core.listener.JbootAppListenerBase; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author https://github.com/souvc + */ +public class JbootConfig extends JbootAppListenerBase { + + private Logger logger = LoggerFactory.getLogger(JbootConfig.class); + + // ---------------------- xxl-job executor ---------------------- + private XxlJobExecutor xxlJobExecutor = null; + + private void initXxlJobExecutor() { + + // registry jobhandler + XxlJobExecutor.registJobHandler("demoJobHandler", new DemoJobHandler()); + XxlJobExecutor.registJobHandler("shardingJobHandler", new ShardingJobHandler()); + XxlJobExecutor.registJobHandler("httpJobHandler", new HttpJobHandler()); + XxlJobExecutor.registJobHandler("commandJobHandler", new CommandJobHandler()); + + // init executor + xxlJobExecutor = new XxlJobExecutor(); + xxlJobExecutor.setAdminAddresses(Jboot.configValue("xxl.job.admin.addresses")); + xxlJobExecutor.setAppName(Jboot.configValue("xxl.job.executor.appname")); + xxlJobExecutor.setIp(Jboot.configValue("xxl.job.executor.ip")); + xxlJobExecutor.setPort(Integer.valueOf(Jboot.configValue("xxl.job.executor.port"))); + xxlJobExecutor.setAccessToken(Jboot.configValue("xxl.job.accessToken")); + xxlJobExecutor.setLogPath(Jboot.configValue("xxl.job.executor.logpath")); + xxlJobExecutor.setLogRetentionDays(Integer.valueOf(Jboot.configValue("xxl.job.executor.logretentiondays"))); + + // start executor + try { + xxlJobExecutor.start(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + // ---------------------- jboot ---------------------- + + private void destoryXxlJobExecutor() { + if (xxlJobExecutor != null) { + xxlJobExecutor.destroy(); + } + } + + @Override + public void onStart() { + initXxlJobExecutor(); + super.onStart(); + } + + @Override + public void onStop() { + destoryXxlJobExecutor(); + super.onStop(); + } +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/controller/IndexController.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/controller/IndexController.java new file mode 100644 index 00000000..10f219fa --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/controller/IndexController.java @@ -0,0 +1,15 @@ +package com.xuxueli.executor.sample.jboot.controller; + +import io.jboot.web.controller.JbootController; +import io.jboot.web.controller.annotation.RequestMapping; + +/** + * @author https://github.com/souvc + */ +@RequestMapping("/") +public class IndexController extends JbootController { + + public void index() { + renderText("xxl job executor running."); + } +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/CommandJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/CommandJobHandler.java new file mode 100644 index 00000000..a73e19ab --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/CommandJobHandler.java @@ -0,0 +1,54 @@ +package com.xuxueli.executor.sample.jboot.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.log.XxlJobLogger; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.InputStreamReader; + +/** + * 命令行任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +public class CommandJobHandler extends IJobHandler { + + @Override + public ReturnT execute(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(IJobHandler.FAIL.getCode(), "command exit value("+exitValue+") is failed"); + } + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/DemoJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/DemoJobHandler.java new file mode 100644 index 00000000..baf7e885 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/DemoJobHandler.java @@ -0,0 +1,32 @@ +package com.xuxueli.executor.sample.jboot.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.log.XxlJobLogger; + +import java.util.concurrent.TimeUnit; + +/** + * 任务Handler示例(Bean模式) + * + * 开发步骤: + * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”; + * 2、注册到执行器工厂:在 "JFinalCoreConfig.initXxlJobExecutor" 中手动注册,注解key值对应的是调度中心新建任务的JobHandler属性的值。 + * 3、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; + * + * @author xuxueli 2015-12-19 19:43:36 + */ +public class DemoJobHandler extends IJobHandler { + + @Override + public ReturnT execute(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 SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java new file mode 100644 index 00000000..e11d6683 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/HttpJobHandler.java @@ -0,0 +1,81 @@ +package com.xuxueli.executor.sample.jboot.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.log.XxlJobLogger; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * 跨平台Http任务 + * + * @author xuxueli 2018-09-16 03:48:34 + */ +public class HttpJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // request + HttpURLConnection connection = null; + BufferedReader bufferedReader = null; + try { + // connection + URL realUrl = new URL(param); + connection = (HttpURLConnection) realUrl.openConnection(); + + // connection setting + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + 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(); + + //Map> map = connection.getHeaderFields(); + + // 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())); + StringBuilder result = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + result.append(line); + } + String responseMsg = result.toString(); + + XxlJobLogger.log(responseMsg); + return SUCCESS; + } catch (Exception e) { + XxlJobLogger.log(e); + return FAIL; + } finally { + try { + if (bufferedReader != null) { + bufferedReader.close(); + } + if (connection != null) { + connection.disconnect(); + } + } catch (Exception e2) { + XxlJobLogger.log(e2); + } + } + + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java new file mode 100644 index 00000000..8eb70aa7 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/java/com/xuxueli/executor/sample/jboot/jobhandler/ShardingJobHandler.java @@ -0,0 +1,34 @@ +package com.xuxueli.executor.sample.jboot.jobhandler; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import com.xxl.job.core.util.ShardingUtil; + +/** + * 分片广播任务 + * + * @author xuxueli 2017-07-25 20:56:50 + */ +public class ShardingJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String param) throws Exception { + + // 分片参数 + ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); + XxlJobLogger.log("分片参数:当前分片序号 = {}, 总分片数 = {}", shardingVO.getIndex(), shardingVO.getTotal()); + + // 业务逻辑 + for (int i = 0; i < shardingVO.getTotal(); i++) { + if (i == shardingVO.getIndex()) { + XxlJobLogger.log("第 {} 片, 命中分片开始处理", i); + } else { + XxlJobLogger.log("第 {} 片, 忽略", i); + } + } + + return SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/jboot.properties b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/jboot.properties new file mode 100644 index 00000000..3cdf1d53 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/jboot.properties @@ -0,0 +1,19 @@ +#jboot ˿ +undertow.port=8082 + + +### 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 executor address +xxl.job.executor.appname=xxl-job-executor-sample +xxl.job.executor.ip= +xxl.job.executor.port=9994 + +### xxl-job, access token +xxl.job.accessToken= + +### xxl-job log path +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler +### xxl-job log retention days +xxl.job.executor.logretentiondays=-1 \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/logback.xml b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/logback.xml new file mode 100644 index 00000000..f3740648 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/main/resources/logback.xml @@ -0,0 +1,149 @@ + + + + + + + + + + + + + + + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + ERROR + + ACCEPT + + DENY + + + + + + ${log_dir}/error/%d{yyyy-MM-dd}/logs.log + + + ${maxHistory} + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + WARN + + ACCEPT + + DENY + + + + ${log_dir}/warn/%d{yyyy-MM-dd}/logs.log + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + INFO + ACCEPT + DENY + + + ${log_dir}/info/%d{yyyy-MM-dd}/logs.log + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + DEBUG + ACCEPT + DENY + + + ${log_dir}/debug/%d{yyyy-MM-dd}/logs.log + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + TRACE + ACCEPT + DENY + + + ${log_dir}/trace/%d{yyyy-MM-dd}/logs.log + ${maxHistory} + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/test/java/com/xuxueli/AppTest.java b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/test/java/com/xuxueli/AppTest.java new file mode 100644 index 00000000..beff7f60 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-jboot/src/test/java/com/xuxueli/AppTest.java @@ -0,0 +1,20 @@ +package com.xuxueli; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +}