Browse Source

Merge pull request #153 from denghuafeng/master

许雪里 7 years ago
parent
commit
25ed0cfb23
No account linked to committer's email

+ 1 - 0
pom.xml View File

@@ -31,6 +31,7 @@
31 31
 		<hessian.version>4.0.51</hessian.version>
32 32
 		<httpclient.version>4.5.4</httpclient.version>
33 33
 
34
+
34 35
 		<commons-exec.version>1.3</commons-exec.version>
35 36
 		<commons-collections4.version>4.1</commons-collections4.version>
36 37
 		<commons-lang3.version>3.7</commons-lang3.version>

+ 1 - 0
xxl-job-executor-samples/pom.xml View File

@@ -14,6 +14,7 @@
14 14
         <module>xxl-job-executor-sample-spring</module>
15 15
         <module>xxl-job-executor-sample-springboot</module>
16 16
         <module>xxl-job-executor-sample-jfinal</module>
17
+        <module>xxl-job-executor-sample-nutz</module>
17 18
     </modules>
18 19
 
19 20
 </project>

+ 35 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/pom.xml View File

@@ -0,0 +1,35 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <parent>
6
+        <artifactId>xxl-job-executor-samples</artifactId>
7
+        <groupId>com.xuxueli</groupId>
8
+        <version>1.8.2-SNAPSHOT</version>
9
+    </parent>
10
+    <modelVersion>4.0.0</modelVersion>
11
+    <artifactId>xxl-job-executor-sample-nutz</artifactId>
12
+    <packaging>war</packaging>
13
+
14
+    <dependencies>
15
+       <dependency>
16
+			<groupId>org.nutz</groupId>
17
+			<artifactId>nutz</artifactId>
18
+			<version>1.r.62</version>
19
+		</dependency>
20
+
21
+        <!-- xxl-job -->
22
+        <dependency>
23
+            <groupId>com.xuxueli</groupId>
24
+            <artifactId>xxl-job-core</artifactId>
25
+            <version>${project.parent.version}</version>
26
+        </dependency>
27
+
28
+        <dependency>
29
+            <groupId>org.slf4j</groupId>
30
+            <artifactId>slf4j-log4j12</artifactId>
31
+            <version>${slf4j-api.version}</version>
32
+        </dependency>
33
+    </dependencies>
34
+
35
+</project>

+ 26 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainModule.java View File

@@ -0,0 +1,26 @@
1
+package com.xuxueli.executor.sample.nutz;
2
+
3
+import org.nutz.mvc.annotation.Encoding;
4
+import org.nutz.mvc.annotation.Fail;
5
+import org.nutz.mvc.annotation.IocBy;
6
+import org.nutz.mvc.annotation.Localization;
7
+import org.nutz.mvc.annotation.Modules;
8
+import org.nutz.mvc.annotation.Ok;
9
+import org.nutz.mvc.annotation.SetupBy;
10
+import org.nutz.mvc.ioc.provider.ComboIocProvider;
11
+/**
12
+ * 
13
+ * @author 邓华锋
14
+ *
15
+ */
16
+@IocBy(type=ComboIocProvider.class,args={"*org.nutz.ioc.loader.json.JsonLoader","ioc/",
17
+	  "*org.nutz.ioc.loader.annotation.AnnotationIocLoader","com.xuxueli"})
18
+@Encoding(input="utf-8",output="utf-8")
19
+@Modules(scanPackage=true)
20
+@Localization("msg")
21
+@Ok("json")
22
+@Fail("json")
23
+@SetupBy(MainSetup.class)
24
+public class MainModule {
25
+	
26
+}

+ 55 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/MainSetup.java View File

@@ -0,0 +1,55 @@
1
+package com.xuxueli.executor.sample.nutz;
2
+
3
+import org.nutz.ioc.IocException;
4
+import org.nutz.ioc.impl.PropertiesProxy;
5
+import org.nutz.log.Log;
6
+import org.nutz.log.Logs;
7
+import org.nutz.mvc.NutConfig;
8
+import org.nutz.mvc.Setup;
9
+
10
+import com.xxl.job.core.executor.XxlJobExecutor;
11
+import com.xxl.job.core.handler.IJobHandler;
12
+
13
+/**
14
+ * 
15
+ * @author 邓华锋
16
+ *
17
+ */
18
+public class MainSetup implements Setup {
19
+	public static final Log log = Logs.get();
20
+	XxlJobExecutor xxlJobExecutor = null;
21
+
22
+	@Override
23
+	public void init(NutConfig cfg) {
24
+		// 通用注册IJobHandler
25
+		String[] names = cfg.getIoc().getNamesByType(IJobHandler.class);
26
+		for (String name : names) {
27
+			XxlJobExecutor.registJobHandler(name, cfg.getIoc().get(IJobHandler.class, name));
28
+		}
29
+		// load executor prop
30
+		PropertiesProxy xxlJobProp = cfg.getIoc().get(PropertiesProxy.class, "conf");
31
+
32
+		// init executor
33
+		xxlJobExecutor = new XxlJobExecutor();
34
+		xxlJobExecutor.setIp(xxlJobProp.get("xxl.job.executor.ip"));
35
+		xxlJobExecutor.setPort(xxlJobProp.getInt("xxl.job.executor.port"));
36
+		xxlJobExecutor.setAppName(xxlJobProp.get("xxl.job.executor.appname"));
37
+		xxlJobExecutor.setAdminAddresses(xxlJobProp.get("xxl.job.admin.addresses"));
38
+		xxlJobExecutor.setLogPath(xxlJobProp.get("xxl.job.executor.logpath"));
39
+		xxlJobExecutor.setAccessToken(xxlJobProp.get("xxl.job.accessToken"));
40
+
41
+		// start executor
42
+		try {
43
+			xxlJobExecutor.start();
44
+		} catch (Exception e) {
45
+			log.error(e.getMessage(), e);
46
+		}
47
+	}
48
+
49
+	@Override
50
+	public void destroy(NutConfig cfg) {
51
+		if (xxlJobExecutor != null) {
52
+			xxlJobExecutor.destroy();
53
+		}
54
+	}
55
+}

+ 36 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/DemoJobHandler.java View File

@@ -0,0 +1,36 @@
1
+package com.xuxueli.executor.sample.nutz.jobhandler;
2
+
3
+import java.util.concurrent.TimeUnit;
4
+
5
+import org.nutz.ioc.loader.annotation.IocBean;
6
+
7
+import com.xxl.job.core.biz.model.ReturnT;
8
+import com.xxl.job.core.handler.IJobHandler;
9
+import com.xxl.job.core.log.XxlJobLogger;
10
+
11
+
12
+/**
13
+ * 任务Handler的一个Demo(Bean模式)
14
+ *
15
+ * 开发步骤:
16
+ * 1、继承 “IJobHandler” ;
17
+ * 2、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;
18
+ * 3、在 "JFinalCoreConfig" 中注册,执行Jobhandler名称;
19
+ *
20
+ * @author xuxueli 2015-12-19 19:43:36
21
+ */
22
+@IocBean
23
+public class DemoJobHandler extends IJobHandler {
24
+
25
+	@Override
26
+	public ReturnT<String> execute(String... params) throws Exception {
27
+		XxlJobLogger.log("XXL-JOB, Hello World.");
28
+
29
+		for (int i = 0; i < 5; i++) {
30
+			XxlJobLogger.log("beat at:" + i);
31
+			TimeUnit.SECONDS.sleep(2);
32
+		}
33
+		return ReturnT.SUCCESS;
34
+	}
35
+
36
+}

+ 38 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/jobhandler/ShardingJobHandler.java View File

@@ -0,0 +1,38 @@
1
+package com.xuxueli.executor.sample.nutz.jobhandler;
2
+
3
+import org.nutz.ioc.loader.annotation.IocBean;
4
+
5
+import com.xxl.job.core.biz.model.ReturnT;
6
+import com.xxl.job.core.handler.IJobHandler;
7
+import com.xxl.job.core.log.XxlJobLogger;
8
+import com.xxl.job.core.util.ShardingUtil;
9
+
10
+
11
+/**
12
+ * 分片广播任务
13
+ *
14
+ * @author xuxueli 2017-07-25 20:56:50
15
+ */
16
+@IocBean
17
+public class ShardingJobHandler extends IJobHandler {
18
+
19
+	@Override
20
+	public ReturnT<String> execute(String... params) throws Exception {
21
+
22
+		// 分片参数
23
+		ShardingUtil.ShardingVO shardingVO = ShardingUtil.getShardingVo();
24
+		XxlJobLogger.log("分片参数:当前分片序号 = {0}, 总分片数 = {1}", shardingVO.getIndex(), shardingVO.getTotal());
25
+
26
+		// 业务逻辑
27
+		for (int i = 0; i < shardingVO.getTotal(); i++) {
28
+			if (i == shardingVO.getIndex()) {
29
+				XxlJobLogger.log("第 {0} 片, 命中分片开始处理", i);
30
+			} else {
31
+				XxlJobLogger.log("第 {0} 片, 忽略", i);
32
+			}
33
+		}
34
+
35
+		return ReturnT.SUCCESS;
36
+	}
37
+	
38
+}

+ 18 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/java/com/xuxueli/executor/sample/nutz/module/IndexModule.java View File

@@ -0,0 +1,18 @@
1
+package com.xuxueli.executor.sample.nutz.module;
2
+
3
+import org.nutz.ioc.loader.annotation.IocBean;
4
+import org.nutz.mvc.annotation.At;
5
+import org.nutz.mvc.annotation.Ok;
6
+/**
7
+ * 
8
+ * @author 邓华锋
9
+ *
10
+ */
11
+@IocBean
12
+public class IndexModule {
13
+	
14
+	@At
15
+	@Ok("jsp:index")
16
+	public void index() {
17
+	}
18
+}

+ 13 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/custom/xxl-job-executor.properties View File

@@ -0,0 +1,13 @@
1
+### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
2
+xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
3
+
4
+### xxl-job executor address
5
+xxl.job.executor.appname=xxl-job-executor-sample
6
+xxl.job.executor.ip=
7
+xxl.job.executor.port=1024
8
+
9
+### xxl-job log path
10
+xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler/
11
+
12
+### xxl-job, access token
13
+xxl.job.accessToken=

+ 38 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/ioc/dao.js View File

@@ -0,0 +1,38 @@
1
+var ioc = {
2
+	conf : {
3
+		type : "org.nutz.ioc.impl.PropertiesProxy",
4
+		fields : {
5
+			paths : [ "custom/" ]
6
+		}
7
+	},
8
+	/**
9
+	 * 配置单例job执行
10
+	 */
11
+	/*xxlJobExecutor : {
12
+		type : "com.xxl.job.core.executor.XxlJobExecutor",
13
+		events : {
14
+			create : "start",
15
+			depose : "destroy"
16
+		},
17
+		fields : {
18
+			ip : {
19
+				java : "$conf.get('xxl.job.executor.ip')"
20
+			},
21
+			port : {
22
+				java : "$conf.get('xxl.job.executor.port')"
23
+			},
24
+			appName : {
25
+				java : "$conf.get('xxl.job.executor.appname')"
26
+			},
27
+			adminAddresses : {
28
+				java : "$conf.get('xxl.job.admin.addresses')"
29
+			},
30
+			logPath : {
31
+				java : "$conf.get('xxl.job.executor.logpath')"
32
+			},
33
+			accessToken : {
34
+				java : "$conf.get('xxl.job.accessToken')"
35
+			}
36
+		}
37
+	}*/
38
+};

+ 27 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/resources/log4j.xml View File

@@ -0,0 +1,27 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
3
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" threshold="null" debug="null">
4
+
5
+	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
6
+		<param name="Target" value="System.out" />
7
+		<layout class="org.apache.log4j.PatternLayout">
8
+            <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} spider-executor [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
9
+		</layout>
10
+	</appender>
11
+	
12
+    <appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
13
+        <param name="file" value="/data/applogs/xxl-job/spider-executor.log"/>
14
+        <param name="append" value="true"/>
15
+        <param name="encoding" value="UTF-8"/>
16
+        <layout class="org.apache.log4j.PatternLayout">
17
+            <param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} spider-executor [%c]-[%t]-[%M]-[%L]-[%p] %m%n"/>
18
+        </layout>
19
+    </appender>
20
+
21
+    <root>
22
+        <level value="INFO" />
23
+        <appender-ref ref="CONSOLE" />
24
+        <appender-ref ref="FILE" />
25
+    </root>
26
+
27
+</log4j:configuration>

+ 12 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/index.jsp View File

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

+ 22 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/WEB-INF/web.xml View File

@@ -0,0 +1,22 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4
+	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5
+	id="WebApp_ID" version="2.5">
6
+	<display-name>xxl-job-executor-sample-nutz</display-name>
7
+	<filter>
8
+		<filter-name>xxl-job-executor-sample-nutz</filter-name>
9
+		<filter-class>org.nutz.mvc.NutFilter</filter-class>
10
+		<init-param>
11
+			<param-name>modules</param-name>
12
+			<param-value>com.xuxueli.executor.sample.nutz.MainModule</param-value>
13
+		</init-param>
14
+	</filter>
15
+	<filter-mapping>
16
+		<filter-name>xxl-job-executor-sample-nutz</filter-name>
17
+		<url-pattern>/*</url-pattern>
18
+	</filter-mapping>
19
+	<welcome-file-list>
20
+		<welcome-file>index</welcome-file>
21
+	</welcome-file-list>
22
+</web-app>

+ 0 - 0
xxl-job-executor-samples/xxl-job-executor-sample-nutz/src/main/webapp/index View File