Pārlūkot izejas kodu

任务注册功能:

xueli.xue 9 gadus atpakaļ
vecāks
revīzija
1fd922f95b

+ 8 - 1
db/tables_xxl_job.sql Parādīt failu

@@ -193,7 +193,14 @@ CREATE TABLE XXL_JOB_QRTZ_TRIGGER_LOGGLUE (
193 193
   PRIMARY KEY (`id`)
194 194
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
195 195
 
196
-
196
+CREATE TABLE XXL_JOB_QRTZ_TRIGGER_REGISTRY (
197
+  `id` int(11) NOT NULL AUTO_INCREMENT,
198
+  `registry_group` varchar(255) NOT NULL,
199
+  `registry_key` varchar(255) NOT NULL,
200
+  `registry_value` varchar(255) NOT NULL,
201
+  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
202
+  PRIMARY KEY (`id`)
203
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
197 204
 
198 205
 commit;
199 206
 

+ 40 - 5
xxl-job-core/src/main/java/com/xxl/job/core/executor/jetty/XxlJobExecutor.java Parādīt failu

@@ -1,8 +1,10 @@
1 1
 package com.xxl.job.core.executor.jetty;
2 2
 
3
-import com.xxl.job.core.router.HandlerRouter;
4 3
 import com.xxl.job.core.handler.IJobHandler;
5 4
 import com.xxl.job.core.handler.annotation.JobHander;
5
+import com.xxl.job.core.registry.RegistHelper;
6
+import com.xxl.job.core.router.HandlerRouter;
7
+import com.xxl.job.core.util.IpUtil;
6 8
 import org.eclipse.jetty.server.Connector;
7 9
 import org.eclipse.jetty.server.Handler;
8 10
 import org.eclipse.jetty.server.Server;
@@ -16,6 +18,7 @@ import org.springframework.context.ApplicationContext;
16 18
 import org.springframework.context.ApplicationContextAware;
17 19
 
18 20
 import java.util.Map;
21
+import java.util.concurrent.TimeUnit;
19 22
 
20 23
 /**
21 24
  * Created by xuxueli on 2016/3/2 21:14.
@@ -24,15 +27,23 @@ public class XxlJobExecutor implements ApplicationContextAware {
24 27
     private static final Logger logger = LoggerFactory.getLogger(XxlJobExecutor.class);
25 28
 
26 29
     private int port = 9999;
30
+    private String appName;
31
+    private RegistHelper registHelper;
27 32
     public void setPort(int port) {
28 33
         this.port = port;
29 34
     }
35
+    public void setAppName(String appName) {
36
+        this.appName = appName;
37
+    }
38
+    public void setRegistHelper(RegistHelper registHelper) {
39
+        this.registHelper = registHelper;
40
+    }
30 41
 
31 42
     // ---------------------------------- job server ------------------------------------
32 43
     Server server = null;
33 44
     public void start() throws Exception {
34 45
 
35
-        new Thread(new Runnable() {
46
+        Thread executorTnread = new Thread(new Runnable() {
36 47
             @Override
37 48
             public void run() {
38 49
                 server = new Server();
@@ -52,14 +63,16 @@ public class XxlJobExecutor implements ApplicationContextAware {
52 63
                 try {
53 64
                     server.start();
54 65
                     logger.info(">>>>>>>>>>>> xxl-job jetty server start success at port:{}.", port);
55
-                    server.join();  // block until server ready
66
+                    registryBeat();
67
+                    server.join();  // block until thread stopped
56 68
                     logger.info(">>>>>>>>>>>> xxl-job jetty server join success at port:{}.", port);
57 69
                 } catch (Exception e) {
58 70
                     e.printStackTrace();
59 71
                 }
60 72
             }
61
-        }).start();
62
-
73
+        });
74
+        executorTnread.setDaemon(true); // daemon, service jvm, user thread leave >>> daemon leave >>> jvm leave
75
+        executorTnread.start();
63 76
     }
64 77
     
65 78
     public void destroy(){
@@ -72,6 +85,28 @@ public class XxlJobExecutor implements ApplicationContextAware {
72 85
 		}
73 86
     }
74 87
 
88
+    private void registryBeat(){
89
+        if (registHelper==null && appName==null || appName.trim().length()==0) {
90
+            return;
91
+        }
92
+        Thread registryThread = new Thread(new Runnable() {
93
+            @Override
94
+            public void run() {
95
+                while (true) {
96
+                    try {
97
+                        String address = IpUtil.getIp().concat(":").concat(String.valueOf(port));
98
+                        registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
99
+                        TimeUnit.SECONDS.sleep(15);
100
+                    } catch (Exception e) {
101
+                        e.printStackTrace();
102
+                    }
103
+                }
104
+            }
105
+        });
106
+        registryThread.setDaemon(true);
107
+        registryThread.start();
108
+    }
109
+
75 110
     // ---------------------------------- init job handler ------------------------------------
76 111
     public static ApplicationContext applicationContext;
77 112
 	@Override

+ 12 - 0
xxl-job-core/src/main/java/com/xxl/job/core/registry/RegistHelper.java Parādīt failu

@@ -0,0 +1,12 @@
1
+package com.xxl.job.core.registry;
2
+
3
+/**
4
+ * Created by xuxueli on 16/9/30.
5
+ */
6
+public interface RegistHelper {
7
+
8
+    public enum RegistType{ EXECUTOR, ADMIN }
9
+
10
+    public int registry(String registGroup, String registryKey, String registryValue);
11
+
12
+}

+ 28 - 0
xxl-job-core/src/main/java/com/xxl/job/core/registry/impl/DbRegistHelper.java Parādīt failu

@@ -0,0 +1,28 @@
1
+package com.xxl.job.core.registry.impl;
2
+
3
+import com.xxl.job.core.registry.RegistHelper;
4
+import com.xxl.job.core.util.DBUtil;
5
+
6
+import javax.sql.DataSource;
7
+
8
+/**
9
+ * Created by xuxueli on 16/9/30.
10
+ */
11
+public class DbRegistHelper implements RegistHelper {
12
+
13
+    private DataSource dataSource;
14
+    public void setDataSource(DataSource dataSource) {
15
+        this.dataSource = dataSource;
16
+    }
17
+
18
+    @Override
19
+    public int registry(String registGroup, String registryKey, String registryValue) {
20
+        String updateSql = "UPDATE XXL_JOB_QRTZ_TRIGGER_REGISTRY SET `update_time` = NOW() WHERE `registry_group` = ? AND `registry_key` = ? AND `registry_value` = ?";
21
+        String insertSql = "INSERT INTO XXL_JOB_QRTZ_TRIGGER_REGISTRY( `registry_group` , `registry_key` , `registry_value`, `update_time`) VALUES(? , ? , ?, NOW())";
22
+        int ret = DBUtil.update(dataSource, updateSql, new Object[]{registGroup, registryKey, registryValue});
23
+        if (ret<1) {
24
+            ret = DBUtil.update(dataSource, insertSql, new Object[]{registGroup, registryKey, registryValue});
25
+        }
26
+        return ret;
27
+    }
28
+}

+ 10 - 1
xxl-job-executor-example/src/main/resources/applicationcontext-xxl-job.xml Parādīt failu

@@ -16,6 +16,15 @@
16 16
 	<bean id="xxlJobExecutor" class="com.xxl.job.core.executor.jetty.XxlJobExecutor" init-method="start" destroy-method="destroy" >
17 17
 		<!-- 执行器端口号 -->
18 18
 		<property name="port" value="9999" />
19
+        <property name="appName" value="xxl-job-executor-example" />
20
+        <!-- 执行器注册器,默认使用系统提供的 "DbRegistHelper", 推荐将其改为公共的RPC服务 -->
21
+        <property name="registHelper" >
22
+            <!-- DbRegistHelper, 依赖 "XXL-JOB公共数据源" -->
23
+            <bean class="com.xxl.job.core.registry.impl.DbRegistHelper" >
24
+                <!-- XXL-JOB公共数据源 -->
25
+                <property name="dataSource" ref="dataSource" />
26
+            </bean>
27
+        </property>
19 28
 	</bean>
20 29
 
21 30
     <!-- ********************************* "GlueFactory" 配置, 仅在启动 "GLUE模式任务" 时才需要, 否则可删除 ********************************* -->
@@ -34,7 +43,7 @@
34 43
 		</property>
35 44
 	</bean>
36 45
 
37
-    <!-- ********************************* "XXL-JOB公共数据源" 配置, 仅在启动 "GLUE模式任务" 的 "DbGlueLoader" 时才需要, 否则可删除 ********************************* -->
46
+    <!-- ********************************* "XXL-JOB公共数据源" 配置, 仅在启动 "DbRegistHelper" 或 "DbGlueLoader" 时才需要, 否则可删除 ********************************* -->
38 47
 
39 48
 	<!-- 配置04、XXL-JOB公共数据源 -->
40 49
 	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">