From 8e49c708384c285fa6cecac652d4d9a08c3d1c73 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 27 Dec 2017 10:15:46 +0800 Subject: [PATCH] add: xxl-job-executor-sample-nutzboot --- xxl-job-executor-samples/pom.xml | 1 + .../README.md | 17 +++ .../xxl-job-executor-sample-nutzboot/pom.xml | 104 ++++++++++++++++++ .../com/xxl/job/executor/MainLauncher.java | 56 ++++++++++ .../service/jobhandler/DemoJobHandler.java | 39 +++++++ .../jobhandler/ShardingJobHandler.java | 40 +++++++ .../service/jobhandler/XxlJobConfig.java | 38 +++++++ .../src/main/resources/application.properties | 17 +++ .../src/main/resources/log4j.properties | 8 ++ 9 files changed, 320 insertions(+) create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/README.md create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/pom.xml create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/MainLauncher.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/XxlJobConfig.java create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/application.properties create mode 100644 xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/log4j.properties diff --git a/xxl-job-executor-samples/pom.xml b/xxl-job-executor-samples/pom.xml index bcbd0f13..14a34322 100644 --- a/xxl-job-executor-samples/pom.xml +++ b/xxl-job-executor-samples/pom.xml @@ -14,6 +14,7 @@ xxl-job-executor-sample-spring xxl-job-executor-sample-springboot xxl-job-executor-sample-jfinal + xxl-job-executor-sample-nutzboot \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/README.md b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/README.md new file mode 100644 index 00000000..74ff2540 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/README.md @@ -0,0 +1,17 @@ +# xxl-job-executor-sample-nutzboot + +用NutzBoot作为xxl-job-executor的示例 + +## 文件介绍 + +* MainLauncher.java NutzBoot启动类, 其中的init方法,扫描/加载/注册ioc容器内的IJobHandler +* XxlJobConfig.java 读取配置信息,声明XxlJobExecutor对象 +* ShardingJobHandler.java和DemoJobHandler.java 2个示例IJobHandler实现类 + +## 环境要求 + +* JDK8u112 以上 + +## 我有疑问? + +请访问 https://nutz.cn 获取帮助 \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/pom.xml b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/pom.xml new file mode 100644 index 00000000..55787ae1 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/pom.xml @@ -0,0 +1,104 @@ + + + 4.0.0 + + com.xuxueli + xxl-job-executor-samples + 1.9.0-SNAPSHOT + + xxl-job-executor-sample-nutzboot + jar + + ${project.artifactId} + Example executor project for nutzboot. https://nutz.io + http://www.xuxueli.com/ + + + UTF-8 + UTF-8 + 1.8 + 2.0 + + + + + org.nutz + nutzboot-starter-jetty + ${nutzboot.version} + + + org.nutz + nutzboot-starter-nutz-mvc + ${nutzboot.version} + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + org.eclipse.jetty + jetty-server + + + javax.servlet + javax.servlet-api + + + javax.servlet.jsp + jsp-api + + + + + + org.slf4j + slf4j-log4j12 + 1.7.25 + + + org.nutz + nutzboot-starter + ${nutzboot.version} + + + javax.servlet + javax.servlet-api + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.0.0 + + + package + + shade + + + + + + META-INF/nutz/org.nutz.boot.starter.NbStarter + + + com.xxl.job.executor.MainLauncher + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/MainLauncher.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/MainLauncher.java new file mode 100644 index 00000000..d0fd0643 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/MainLauncher.java @@ -0,0 +1,56 @@ +package com.xxl.job.executor; + +import org.nutz.boot.NbApp; +import org.nutz.ioc.Ioc; +import org.nutz.ioc.loader.annotation.Inject; +import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.mvc.annotation.At; +import org.nutz.mvc.annotation.Ok; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; + +/** + * 使用NutzBoot作为xxl job executor的demo,简单修改后也可用于NutzMvc项目. + *

+ * 如有任何疑问,请访问 https://nutz.cn + * + * @author wendal(wendal1985@gmail.com) + * + */ +@IocBean(create = "init") +public class MainLauncher { + + @Inject("refer:$ioc") + protected Ioc ioc; + + @At("/") + @Ok("raw") + public String index() { + // demo嘛, 简单做个入口方法,显示一句话就够了 + return "xxl job executor running."; + } + + // 如果是普通Nutz MVC项目,这段代码放到MainSetup.init方法内就可以了 + public void init() { + // 从ioc容器中找出所有实现了IJobHandler接口的对象,注册到XxlJobExecutor + for (String jobHandlerBeanName : ioc.getNamesByType(IJobHandler.class)) { + // 获取JobHandler实例 + IJobHandler jobHandler = ioc.get(IJobHandler.class, jobHandlerBeanName); + // 看看有没有@JobHandler注解 + JobHandler annoJobHandler = jobHandler.getClass().getAnnotation(JobHandler.class); + // 得到jobHandlerName + String jobHandlerName = annoJobHandler == null ? jobHandlerBeanName : annoJobHandler.value(); + // 注册到XxlJobExecutor上下文 + XxlJobExecutor.registJobHandler(jobHandlerName, jobHandler); + } + // 获取XxlJobExecutor,从而触发XxlJobExecutor的初始化 + ioc.getByType(XxlJobExecutor.class); + } + + public static void main(String[] args) { + new NbApp().run(); // 启动一切 + } + +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java new file mode 100644 index 00000000..c1f6383e --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java @@ -0,0 +1,39 @@ +package com.xxl.job.executor.service.jobhandler; + +import java.util.concurrent.TimeUnit; + +import org.nutz.ioc.loader.annotation.IocBean; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; + + +/** + * 任务Handler的一个Demo(Bean模式) + * + * 开发步骤: + * 1、继承 “IJobHandler” ; + * 2、装配到Spring,例如加 “@Service” 注解; + * 3、加 “@JobHandler” 注解,注解value值为新增任务生成的JobKey的值;多个JobKey用逗号分割; + * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; + * + * @author xuxueli 2015-12-19 19:43:36 + */ +@JobHandler(value="demoJobHandler") +@IocBean +public class DemoJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String... params) throws Exception { + XxlJobLogger.log("XXL-JOB, Hello World."); + + for (int i = 0; i < 5; i++) { + XxlJobLogger.log("beat at:" + i); + TimeUnit.SECONDS.sleep(2); + } + return ReturnT.SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java new file mode 100644 index 00000000..77f15aff --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/ShardingJobHandler.java @@ -0,0 +1,40 @@ +package com.xxl.job.executor.service.jobhandler; + +import org.nutz.ioc.loader.annotation.IocBean; + +import com.xxl.job.core.biz.model.ReturnT; +import com.xxl.job.core.handler.IJobHandler; +import com.xxl.job.core.handler.annotation.JobHandler; +import com.xxl.job.core.log.XxlJobLogger; +import com.xxl.job.core.util.ShardingUtil; + + +/** + * 分片广播任务 + * + * @author xuxueli 2017-07-25 20:56:50 + */ +@JobHandler(value="shardingJobHandler") +@IocBean +public class ShardingJobHandler extends IJobHandler { + + @Override + public ReturnT execute(String... params) throws Exception { + + // 分片参数 + ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo(); + XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal()); + + // 业务逻辑 + for (int i = 0; i < shardingVO.getTotal(); i++) { + if (i == shardingVO.getIndex()) { + XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i); + } else { + XxlJobLogger.log("第 {0} 片, 忽略", i); + } + } + + return ReturnT.SUCCESS; + } + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/XxlJobConfig.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/XxlJobConfig.java new file mode 100644 index 00000000..56478ca0 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/java/com/xxl/job/executor/service/jobhandler/XxlJobConfig.java @@ -0,0 +1,38 @@ +package com.xxl.job.executor.service.jobhandler; + +import org.nutz.ioc.impl.PropertiesProxy; +import org.nutz.ioc.loader.annotation.Inject; +import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.log.Log; +import org.nutz.log.Logs; + +import com.xxl.job.core.executor.XxlJobExecutor; + +/** + * xxl-job config + * + * @author wendal 2017-12-27 + */ +@IocBean +public class XxlJobConfig { + + private static Log log = Logs.get(); + + @Inject + protected PropertiesProxy conf; + + @IocBean(create = "start", depose = "destroy") + public XxlJobExecutor xxlJobExecutor() { + log.info(">>>>>>>>>>> xxl-job config init."); + XxlJobExecutor xxlJobExecutor = new XxlJobExecutor(); + // 下列参数均为必填项,声明在application.properties + xxlJobExecutor.setIp(conf.check("xxl.job.executor.ip")); + xxlJobExecutor.setPort(conf.getInt("xxl.job.executor.port")); + xxlJobExecutor.setAppName(conf.check("xxl.job.executor.appname")); + xxlJobExecutor.setAdminAddresses(conf.check("xxl.job.admin.addresses")); + xxlJobExecutor.setLogPath(conf.check("xxl.job.executor.logpath")); + xxlJobExecutor.setAccessToken(conf.check("xxl.job.accessToken")); + return xxlJobExecutor; + } + +} \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/application.properties b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/application.properties new file mode 100644 index 00000000..a18ceed6 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/application.properties @@ -0,0 +1,17 @@ +# web port +jetty.port=8081 + +# xxl-job +### 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=-1 + +### xxl-job log path +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/ + +### xxl-job, access token +xxl.job.accessToken= diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/log4j.properties b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/log4j.properties new file mode 100644 index 00000000..b691d196 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutzboot/src/main/resources/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootLogger=info,Console + +log4j.logger.org.nutz=info +log4j.logger.org.eclipse.jetty=info + +log4j.appender.Console=org.apache.log4j.ConsoleAppender +log4j.appender.Console.layout=org.apache.log4j.PatternLayout +log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] %5p [%t] --- %c{1}: %m%n