Bladeren bron

底层优化

xueli.xue 8 jaren geleden
bovenliggende
commit
650682d2fe

+ 5 - 1
xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java Bestand weergeven

@@ -8,6 +8,7 @@ import com.xxl.job.core.rpc.netcom.NetComServerFactory;
8 8
 import com.xxl.job.core.thread.ExecutorRegistryThread;
9 9
 import com.xxl.job.core.thread.JobThread;
10 10
 import com.xxl.job.core.thread.TriggerCallbackThread;
11
+import com.xxl.job.core.util.AdminApiUtil;
11 12
 import org.slf4j.Logger;
12 13
 import org.slf4j.LoggerFactory;
13 14
 import org.springframework.beans.BeansException;
@@ -29,7 +30,7 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
29 30
     private String ip;
30 31
     private int port = 9999;
31 32
     private String appName;
32
-    public static String adminAddresses;
33
+    private String adminAddresses;
33 34
     public static String logPath;
34 35
 
35 36
     public void setIp(String ip) {
@@ -51,6 +52,9 @@ public class XxlJobExecutor implements ApplicationContextAware, ApplicationListe
51 52
     // ---------------------------------- job server ------------------------------------
52 53
     private NetComServerFactory serverFactory = new NetComServerFactory();
53 54
     public void start() throws Exception {
55
+        // admin api util init
56
+        AdminApiUtil.init(adminAddresses);
57
+
54 58
         // executor start
55 59
         NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());
56 60
         serverFactory.start(port, ip, appName);

+ 3 - 1
xxl-job-core/src/main/java/com/xxl/job/core/thread/ExecutorRegistryThread.java Bestand weergeven

@@ -24,7 +24,9 @@ public class ExecutorRegistryThread extends Thread {
24 24
     private Thread registryThread;
25 25
     private boolean toStop = false;
26 26
     public void start(final int port, final String ip, final String appName){
27
-        if (appName==null || appName.trim().length()==0) {
27
+
28
+        // valid
29
+        if (AdminApiUtil.allowCallApi && (appName!=null && appName.trim().length()>0) ) {
28 30
             logger.warn(">>>>>>>>>>>> xxl-job, executor registry config fail");
29 31
             return;
30 32
         }

+ 22 - 9
xxl-job-core/src/main/java/com/xxl/job/core/util/AdminApiUtil.java Bestand weergeven

@@ -1,7 +1,6 @@
1 1
 package com.xxl.job.core.util;
2 2
 
3 3
 import com.xxl.job.core.biz.model.ReturnT;
4
-import com.xxl.job.core.executor.XxlJobExecutor;
5 4
 import org.apache.http.HttpEntity;
6 5
 import org.apache.http.HttpResponse;
7 6
 import org.apache.http.client.config.RequestConfig;
@@ -15,7 +14,9 @@ import org.slf4j.LoggerFactory;
15 14
 
16 15
 import java.io.IOException;
17 16
 import java.util.ArrayList;
17
+import java.util.HashSet;
18 18
 import java.util.List;
19
+import java.util.Set;
19 20
 
20 21
 /**
21 22
  * @author xuxueli 2017-05-10 21:28:15
@@ -26,19 +27,31 @@ public class AdminApiUtil {
26 27
 	public static final String CALLBACK = "/api/callback";
27 28
 	public static final String REGISTRY = "/api/registry";
28 29
 
29
-	public static ReturnT<String> callApiFailover(String subUrl, Object requestObj) throws Exception {
30
+	private static List<String> adminAddressList = null;
31
+	public static boolean allowCallApi = true;
30 32
 
33
+	public static void init(String adminAddresses){
31 34
 		// admin assress list
32
-		List<String> adminAddressList = new ArrayList<String>();
33
-		if (XxlJobExecutor.adminAddresses != null) {
34
-			for (String adminAddressItem: XxlJobExecutor.adminAddresses.split(",")) {
35
-				if (adminAddressItem.trim().length()>0 && !adminAddressList.contains(adminAddressItem)) {
36
-					adminAddressList.add(adminAddressItem);
35
+		if (adminAddresses != null) {
36
+			Set<String> adminAddressSet = new HashSet<String>();
37
+			for (String adminAddressItem: adminAddresses.split(",")) {
38
+				if (adminAddressItem.trim().length()>0 && !adminAddressSet.contains(adminAddressItem)) {
39
+					adminAddressSet.add(adminAddressItem);
37 40
 				}
38 41
 			}
42
+			if (adminAddressSet==null || adminAddressSet.size()==0) {
43
+				adminAddressList = new ArrayList<String>(adminAddressSet);
44
+			}
39 45
 		}
40
-		if (adminAddressList==null || adminAddressList.size()==0) {
41
-			return ReturnT.FAIL;
46
+
47
+		// parse
48
+		allowCallApi = (adminAddressList!=null && adminAddressList.size()>0);
49
+	}
50
+
51
+	public static ReturnT<String> callApiFailover(String subUrl, Object requestObj) throws Exception {
52
+
53
+		if (!allowCallApi) {
54
+			return new ReturnT<String>(ReturnT.FAIL_CODE, "allowCallback fail.");
42 55
 		}
43 56
 
44 57
 		for (String adminAddress: adminAddressList) {