Browse Source

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

xuxueli 8 years ago
parent
commit
f83346de2d

+ 28 - 1
xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java View File

1
 package com.xxl.job.core.executor;
1
 package com.xxl.job.core.executor;
2
 
2
 
3
+import com.xxl.job.core.biz.AdminBiz;
3
 import com.xxl.job.core.biz.ExecutorBiz;
4
 import com.xxl.job.core.biz.ExecutorBiz;
4
 import com.xxl.job.core.biz.impl.ExecutorBizImpl;
5
 import com.xxl.job.core.biz.impl.ExecutorBizImpl;
5
 import com.xxl.job.core.handler.IJobHandler;
6
 import com.xxl.job.core.handler.IJobHandler;
6
 import com.xxl.job.core.handler.annotation.JobHander;
7
 import com.xxl.job.core.handler.annotation.JobHander;
8
+import com.xxl.job.core.rpc.netcom.NetComClientProxy;
7
 import com.xxl.job.core.rpc.netcom.NetComServerFactory;
9
 import com.xxl.job.core.rpc.netcom.NetComServerFactory;
8
 import com.xxl.job.core.thread.ExecutorRegistryThread;
10
 import com.xxl.job.core.thread.ExecutorRegistryThread;
9
 import com.xxl.job.core.thread.JobThread;
11
 import com.xxl.job.core.thread.JobThread;
17
 import org.springframework.context.ApplicationListener;
19
 import org.springframework.context.ApplicationListener;
18
 import org.springframework.context.event.ContextClosedEvent;
20
 import org.springframework.context.event.ContextClosedEvent;
19
 
21
 
22
+import java.util.ArrayList;
23
+import java.util.List;
20
 import java.util.Map;
24
 import java.util.Map;
21
 import java.util.concurrent.ConcurrentHashMap;
25
 import java.util.concurrent.ConcurrentHashMap;
22
 
26
 
29
     private String ip;
33
     private String ip;
30
     private int port = 9999;
34
     private int port = 9999;
31
     private String appName;
35
     private String appName;
32
-    public static String adminAddresses;
36
+    private String adminAddresses;
33
     public static String logPath;
37
     public static String logPath;
34
 
38
 
35
     public void setIp(String ip) {
39
     public void setIp(String ip) {
48
         this.logPath = logPath;
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
     // ---------------------------------- job server ------------------------------------
75
     // ---------------------------------- job server ------------------------------------
52
     private NetComServerFactory serverFactory = new NetComServerFactory();
76
     private NetComServerFactory serverFactory = new NetComServerFactory();
53
     public void start() throws Exception {
77
     public void start() throws Exception {
78
+        // init admin-client
79
+        initAdminBizList(adminAddresses);
80
+
54
         // executor start
81
         // executor start
55
         NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());   // rpc-service, base on jetty
82
         NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());   // rpc-service, base on jetty
56
         serverFactory.start(port, ip, appName);
83
         serverFactory.start(port, ip, appName);

+ 2 - 9
xxl-job-core/src/main/java/com/xxl/job/core/thread/ExecutorRegistryThread.java View File

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

+ 2 - 6
xxl-job-core/src/main/java/com/xxl/job/core/thread/TriggerCallbackThread.java View File

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