增加nutz执行器

pull/6/head
邓华锋 7 years ago
parent aef9ef6c66
commit 20fb8ce7a6

@ -14,6 +14,7 @@
<module>xxl-job-executor-sample-spring</module>
<module>xxl-job-executor-sample-springboot</module>
<module>xxl-job-executor-sample-jfinal</module>
<module>xxl-job-executor-sample-nutz</module>
</modules>
</project>

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>xxl-job-executor-samples</artifactId>
<groupId>com.xuxueli</groupId>
<version>1.8.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>xxl-job-executor-sample-nutz</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.62</version>
</dependency>
<!-- xxl-job -->
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
</dependencies>
</project>

@ -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 {
}

@ -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();
}
}
}

@ -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;
/**
* HandlerDemoBean
*
*
* 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<String> 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;
}
}

@ -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<String> 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;
}
}

@ -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() {
}
}

@ -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=

@ -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')"
}
}
}*/
};

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="null" debug="null">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} spider-executor [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="/data/applogs/xxl-job/spider-executor.log"/>
<param name="append" value="true"/>
<param name="encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} spider-executor [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>

@ -0,0 +1,12 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>nutz 执行器启动成功</title>
</head>
<body>
nutz 执行器启动成功!
</body>
</html>

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>xxl-job-executor-sample-nutz</display-name>
<filter>
<filter-name>xxl-job-executor-sample-nutz</filter-name>
<filter-class>org.nutz.mvc.NutFilter</filter-class>
<init-param>
<param-name>modules</param-name>
<param-value>com.xuxueli.executor.sample.nutz.MainModule</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>xxl-job-executor-sample-nutz</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
</web-app>
Loading…
Cancel
Save