diff --git a/xxl-job-executor-samples/pom.xml b/xxl-job-executor-samples/pom.xml index 96b7d4ca..06627683 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-nutz \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/pom.xml b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/pom.xml new file mode 100644 index 00000000..7ccd6ba8 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/pom.xml @@ -0,0 +1,35 @@ + + + + xxl-job-executor-samples + com.xuxueli + 1.8.2-SNAPSHOT + + 4.0.0 + xxl-job-executor-sample-nutz + war + + + + org.nutz + nutz + 1.r.62 + + + + + com.xuxueli + xxl-job-core + ${project.parent.version} + + + + org.slf4j + slf4j-log4j12 + ${slf4j-api.version} + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainModule.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainModule.java new file mode 100644 index 00000000..01e1b34f --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainModule.java @@ -0,0 +1,26 @@ +package com.xuxueli.executor.sample.nutz; + +import org.nutz.mvc.annotation.Encoding; +import org.nutz.mvc.annotation.Fail; +import org.nutz.mvc.annotation.IocBy; +import org.nutz.mvc.annotation.Localization; +import org.nutz.mvc.annotation.Modules; +import org.nutz.mvc.annotation.Ok; +import org.nutz.mvc.annotation.SetupBy; +import org.nutz.mvc.ioc.provider.ComboIocProvider; +/** + * + * @author 邓华锋 + * + */ +@IocBy(type=ComboIocProvider.class,args={"*org.nutz.ioc.loader.json.JsonLoader","ioc/", + "*org.nutz.ioc.loader.annotation.AnnotationIocLoader","com.xuxueli"}) +@Encoding(input="utf-8",output="utf-8") +@Modules(scanPackage=true) +@Localization("msg") +@Ok("json") +@Fail("json") +@SetupBy(MainSetup.class) +public class MainModule { + +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainSetup.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainSetup.java new file mode 100644 index 00000000..b2192392 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainSetup.java @@ -0,0 +1,55 @@ +package com.xuxueli.executor.sample.nutz; + +import org.nutz.ioc.IocException; +import org.nutz.ioc.impl.PropertiesProxy; +import org.nutz.log.Log; +import org.nutz.log.Logs; +import org.nutz.mvc.NutConfig; +import org.nutz.mvc.Setup; + +import com.xxl.job.core.executor.XxlJobExecutor; +import com.xxl.job.core.handler.IJobHandler; + +/** + * + * @author 邓华锋 + * + */ +public class MainSetup implements Setup { + public static final Log log = Logs.get(); + XxlJobExecutor xxlJobExecutor = null; + + @Override + public void init(NutConfig cfg) { + // 通用注册IJobHandler + String[] names = cfg.getIoc().getNamesByType(IJobHandler.class); + for (String name : names) { + XxlJobExecutor.registJobHandler(name, cfg.getIoc().get(IJobHandler.class, name)); + } + // load executor prop + PropertiesProxy xxlJobProp = cfg.getIoc().get(PropertiesProxy.class, "conf"); + + // init executor + xxlJobExecutor = new XxlJobExecutor(); + xxlJobExecutor.setIp(xxlJobProp.get("xxl.job.executor.ip")); + xxlJobExecutor.setPort(xxlJobProp.getInt("xxl.job.executor.port")); + xxlJobExecutor.setAppName(xxlJobProp.get("xxl.job.executor.appname")); + xxlJobExecutor.setAdminAddresses(xxlJobProp.get("xxl.job.admin.addresses")); + xxlJobExecutor.setLogPath(xxlJobProp.get("xxl.job.executor.logpath")); + xxlJobExecutor.setAccessToken(xxlJobProp.get("xxl.job.accessToken")); + + // start executor + try { + xxlJobExecutor.start(); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + + @Override + public void destroy(NutConfig cfg) { + if (xxlJobExecutor != null) { + xxlJobExecutor.destroy(); + } + } +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/DemoJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/DemoJobHandler.java new file mode 100644 index 00000000..b37eb256 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/DemoJobHandler.java @@ -0,0 +1,36 @@ +package com.xuxueli.executor.sample.nutz.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.log.XxlJobLogger; + + +/** + * 任务Handler的一个Demo(Bean模式) + * + * 开发步骤: + * 1、继承 “IJobHandler” ; + * 2、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志; + * 3、在 "JFinalCoreConfig" 中注册,执行Jobhandler名称; + * + * @author xuxueli 2015-12-19 19:43:36 + */ +@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-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java new file mode 100644 index 00000000..db31758c --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java @@ -0,0 +1,38 @@ +package com.xuxueli.executor.sample.nutz.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.log.XxlJobLogger; +import com.xxl.job.core.util.ShardingUtil; + + +/** + * 分片广播任务 + * + * @author xuxueli 2017-07-25 20:56:50 + */ +@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-nutz/src/main/java/com/xuxueli/executor/sample/nutz/module/IndexModule.java b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/module/IndexModule.java new file mode 100644 index 00000000..0a103e6c --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/module/IndexModule.java @@ -0,0 +1,18 @@ +package com.xuxueli.executor.sample.nutz.module; + +import org.nutz.ioc.loader.annotation.IocBean; +import org.nutz.mvc.annotation.At; +import org.nutz.mvc.annotation.Ok; +/** + * + * @author 邓华锋 + * + */ +@IocBean +public class IndexModule { + + @At + @Ok("jsp:index") + public void index() { + } +} diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/custom/xxl-job-executor.properties b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/custom/xxl-job-executor.properties new file mode 100644 index 00000000..e955535d --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/custom/xxl-job-executor.properties @@ -0,0 +1,13 @@ +### xxl-job admin address list, such as "http://address" or "http://address01,http://address02" +xxl.job.admin.addresses=http://127.0.0.1:8282/xxl-job-admin + +### xxl-job executor address +xxl.job.executor.appname=xxl-job-executor-sample-nutz +xxl.job.executor.ip= +xxl.job.executor.port=1024 + +### xxl-job log path +xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/ + +### xxl-job, access token +xxl.job.accessToken= \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/ioc/dao.js b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/ioc/dao.js new file mode 100644 index 00000000..cc80b0a7 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/ioc/dao.js @@ -0,0 +1,38 @@ +var ioc = { + conf : { + type : "org.nutz.ioc.impl.PropertiesProxy", + fields : { + paths : [ "custom/" ] + } + }, + /** + * 配置单例job执行 + */ + /*xxlJobExecutor : { + type : "com.xxl.job.core.executor.XxlJobExecutor", + events : { + create : "start", + depose : "destroy" + }, + fields : { + ip : { + java : "$conf.get('xxl.job.executor.ip')" + }, + port : { + java : "$conf.get('xxl.job.executor.port')" + }, + appName : { + java : "$conf.get('xxl.job.executor.appname')" + }, + adminAddresses : { + java : "$conf.get('xxl.job.admin.addresses')" + }, + logPath : { + java : "$conf.get('xxl.job.executor.logpath')" + }, + accessToken : { + java : "$conf.get('xxl.job.accessToken')" + } + } + }*/ +}; \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/log4j.xml b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/log4j.xml new file mode 100644 index 00000000..f6196a83 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/log4j.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/index.jsp b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/index.jsp new file mode 100644 index 00000000..03f281fd --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/index.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + + + +nutz 执行器启动成功 + + +nutz 执行器启动成功! + + \ No newline at end of file diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/web.xml b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..97bbf405 --- /dev/null +++ b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,22 @@ + + + xxl-job-executor-sample-nutz + + xxl-job-executor-sample-nutz + org.nutz.mvc.NutFilter + + modules + com.xuxueli.executor.sample.nutz.MainModule + + + + xxl-job-executor-sample-nutz + /* + + + index + + diff --git a/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/index b/xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/index new file mode 100644 index 00000000..e69de29b