瀏覽代碼

调度中心API服务,Client端调用逻辑优化

xuxueli 8 年之前
父節點
當前提交
f83346de2d

+ 28 - 1
xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java 查看文件

@@ -1,9 +1,11 @@
1 1
 package com.xxl.job.core.executor;
2 2
 
3
+import com.xxl.job.core.biz.AdminBiz;
3 4
 import com.xxl.job.core.biz.ExecutorBiz;
4 5
 import com.xxl.job.core.biz.impl.ExecutorBizImpl;
5 6
 import com.xxl.job.core.handler.IJobHandler;
6 7
 import com.xxl.job.core.handler.annotation.JobHander;
8
+import com.xxl.job.core.rpc.netcom.NetComClientProxy;
7 9
 import com.xxl.job.core.rpc.netcom.NetComServerFactory;
8 10
 import com.xxl.job.core.thread.ExecutorRegistryThread;
9 11
 import com.xxl.job.core.thread.JobThread;
@@ -17,6 +19,8 @@ import org.springframework.context.ApplicationEvent;
17 19
 import org.springframework.context.ApplicationListener;
18 20
 import org.springframework.context.event.ContextClosedEvent;
19 21
 
22
+import java.util.ArrayList;
23
+import java.util.List;
20 24
 import java.util.Map;
21 25
 import java.util.concurrent.ConcurrentHashMap;
22 26
 
@@ -29,7 +33,7 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
29 33
     private String ip;
30 34
     private int port = 9999;
31 35
     private String appName;
32
-    public static String adminAddresses;
36
+    private String adminAddresses;
33 37
     public static String logPath;
34 38
 
35 39
     public void setIp(String ip) {
@@ -48,9 +52,32 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
48 52
         this.logPath = logPath;
49 53
     }
50 54
 
55
+    // ---------------------------------- admin-client ------------------------------------
56
+    private static List<AdminBiz> adminBizList;
57
+    private static void initAdminBizList(String adminAddresses) throws Exception {
58
+        if (adminAddresses!=null && adminAddresses.trim().length()>0) {
59
+            for (String address: adminAddresses.trim().split(",")) {
60
+                if (address!=null && address.trim().length()>0) {
61
+                    String addressUrl = address.concat("/api");
62
+                    AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, addressUrl).getObject();
63
+                    if (adminBizList == null) {
64
+                        adminBizList = new ArrayList<AdminBiz>();
65
+                    }
66
+                    adminBizList.add(adminBiz);
67
+                }
68
+            }
69
+        }
70
+    }
71
+    public static List<AdminBiz> getAdminBizList(){
72
+        return adminBizList;
73
+    }
74
+
51 75
     // ---------------------------------- job server ------------------------------------
52 76
     private NetComServerFactory serverFactory = new NetComServerFactory();
53 77
     public void start() throws Exception {
78
+        // init admin-client
79
+        initAdminBizList(adminAddresses);
80
+
54 81
         // executor start
55 82
         NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());   // rpc-service, base on jetty
56 83
         serverFactory.start(port, ip, appName);

+ 2 - 9
xxl-job-core/src/main/java/com/xxl/job/core/thread/ExecutorRegistryThread.java 查看文件

@@ -5,7 +5,6 @@ import com.xxl.job.core.biz.model.RegistryParam;
5 5
 import com.xxl.job.core.biz.model.ReturnT;
6 6
 import com.xxl.job.core.enums.RegistryConfig;
7 7
 import com.xxl.job.core.executor.XxlJobExecutor;
8
-import com.xxl.job.core.rpc.netcom.NetComClientProxy;
9 8
 import com.xxl.job.core.util.IpUtil;
10 9
 import org.slf4j.Logger;
11 10
 import org.slf4j.LoggerFactory;
@@ -32,7 +31,7 @@ public class ExecutorRegistryThread extends Thread {
32 31
             logger.warn(">>>>>>>>>>>> xxl-job, executor registry config fail, appName is null.");
33 32
             return;
34 33
         }
35
-        if (XxlJobExecutor.adminAddresses==null || XxlJobExecutor.adminAddresses.trim().length()==0) {
34
+        if (XxlJobExecutor.getAdminBizList() == null) {
36 35
             logger.warn(">>>>>>>>>>>> xxl-job, executor registry config fail, adminAddresses is null.");
37 36
             return;
38 37
         }
@@ -49,15 +48,10 @@ public class ExecutorRegistryThread extends Thread {
49 48
             @Override
50 49
             public void run() {
51 50
                 while (!toStop) {
52
-
53 51
                     try {
54 52
                         RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.name(), appName, executorAddress);
55
-
56
-                        for (String addressUrl: XxlJobExecutor.adminAddresses.split(",")) {
57
-                            String apiUrl = addressUrl.concat("/api");
58
-
53
+                        for (AdminBiz adminBiz: XxlJobExecutor.getAdminBizList()) {
59 54
                             try {
60
-                                AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, apiUrl).getObject();
61 55
                                 ReturnT<String> registryResult = adminBiz.registry(registryParam);
62 56
                                 if (registryResult!=null && ReturnT.SUCCESS_CODE == registryResult.getCode()) {
63 57
                                     registryResult = ReturnT.SUCCESS;
@@ -71,7 +65,6 @@ public class ExecutorRegistryThread extends Thread {
71 65
                             }
72 66
 
73 67
                         }
74
-
75 68
                     } catch (Exception e) {
76 69
                         logger.error(e.getMessage(), e);
77 70
                     }

+ 2 - 6
xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java 查看文件

@@ -4,7 +4,6 @@ import com.xxl.job.core.biz.AdminBiz;
4 4
 import com.xxl.job.core.biz.model.HandleCallbackParam;
5 5
 import com.xxl.job.core.biz.model.ReturnT;
6 6
 import com.xxl.job.core.executor.XxlJobExecutor;
7
-import com.xxl.job.core.rpc.netcom.NetComClientProxy;
8 7
 import org.slf4j.Logger;
9 8
 import org.slf4j.LoggerFactory;
10 9
 
@@ -43,17 +42,14 @@ public class TriggerCallbackThread {
43 42
                             callbackParamList.add(callback);
44 43
 
45 44
                             // valid
46
-                            if (XxlJobExecutor.adminAddresses==null || XxlJobExecutor.adminAddresses.trim().length()==0) {
45
+                            if (XxlJobExecutor.getAdminBizList()==null) {
47 46
                                 logger.warn(">>>>>>>>>>>> xxl-job callback fail, adminAddresses is null, callbackParamList:{}", callbackParamList);
48 47
                                 continue;
49 48
                             }
50 49
 
51 50
                             // callback, will retry if error
52
-                            for (String addressUrl: XxlJobExecutor.adminAddresses.split(",")) {
53
-                                String apiUrl = addressUrl.concat("/api");
54
-
51
+                            for (AdminBiz adminBiz: XxlJobExecutor.getAdminBizList()) {
55 52
                                 try {
56
-                                    AdminBiz adminBiz = (AdminBiz) new NetComClientProxy(AdminBiz.class, apiUrl).getObject();
57 53
                                     ReturnT<String> callbackResult = adminBiz.callback(callbackParamList);
58 54
                                     if (callbackResult!=null && ReturnT.SUCCESS_CODE == callbackResult.getCode()) {
59 55
                                         callbackResult = ReturnT.SUCCESS;