Authored by BuddyJack

Fix

@@ -7,10 +7,13 @@ import com.monitor.compare.service.GatewayCompare; @@ -7,10 +7,13 @@ import com.monitor.compare.service.GatewayCompare;
7 import com.monitor.compare.service.NignxCompare; 7 import com.monitor.compare.service.NignxCompare;
8 import com.monitor.compare.service.ServiceCompare; 8 import com.monitor.compare.service.ServiceCompare;
9 import com.monitor.model.response.BaseResponse; 9 import com.monitor.model.response.BaseResponse;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.web.bind.annotation.*; 13 import org.springframework.web.bind.annotation.*;
12 14
13 import java.util.*; 15 import java.util.*;
  16 +import java.util.concurrent.*;
14 17
15 18
16 /** 19 /**
@@ -20,6 +23,8 @@ import java.util.*; @@ -20,6 +23,8 @@ import java.util.*;
20 @RequestMapping(value = "/compareIps") 23 @RequestMapping(value = "/compareIps")
21 public class CompareCtrl { 24 public class CompareCtrl {
22 25
  26 + public static final Logger LOGGER = LoggerFactory.getLogger(CompareCtrl.class);
  27 +
23 @Autowired 28 @Autowired
24 private NignxCompare nignxCompare; 29 private NignxCompare nignxCompare;
25 30
@@ -32,7 +37,7 @@ public class CompareCtrl { @@ -32,7 +37,7 @@ public class CompareCtrl {
32 @RequestMapping(value = "/compare") 37 @RequestMapping(value = "/compare")
33 public BaseResponse compare(@RequestBody CompareRequest request) { 38 public BaseResponse compare(@RequestBody CompareRequest request) {
34 39
35 - CompareResponse compareResponse=new CompareResponse(); 40 + CompareResponse compareResponse = new CompareResponse();
36 41
37 Map<String, List<String>> configMap = new HashMap<>(); 42 Map<String, List<String>> configMap = new HashMap<>();
38 43
@@ -72,8 +77,7 @@ public class CompareCtrl { @@ -72,8 +77,7 @@ public class CompareCtrl {
72 } 77 }
73 compareResponse.setIps(resultList); 78 compareResponse.setIps(resultList);
74 79
75 - if(!checkConfig(configMap))  
76 - { 80 + if (!checkConfig(configMap)) {
77 compareResponse.setStatus(1); 81 compareResponse.setStatus(1);
78 } 82 }
79 83
@@ -113,6 +117,7 @@ public class CompareCtrl { @@ -113,6 +117,7 @@ public class CompareCtrl {
113 117
114 /** 118 /**
115 * 在线ip对比--对比所有的服务 119 * 在线ip对比--对比所有的服务
  120 + *
116 * @param cloud 121 * @param cloud
117 * @return 122 * @return
118 */ 123 */
@@ -120,20 +125,34 @@ public class CompareCtrl { @@ -120,20 +125,34 @@ public class CompareCtrl {
120 public BaseResponse compareAll(@RequestBody String cloud) { 125 public BaseResponse compareAll(@RequestBody String cloud) {
121 BaseResponse resp = new BaseResponse(); 126 BaseResponse resp = new BaseResponse();
122 List<String> serviceList = IpCompareSeviceType.getAllServices(); 127 List<String> serviceList = IpCompareSeviceType.getAllServices();
123 - List<CompareResponse> compareResults = new ArrayList<>();  
124 - for(String s : serviceList){  
125 - CompareRequest compareRequest = new CompareRequest(s,cloud);  
126 - CompareResponse result = compareService(compareRequest);  
127 - result.setServiceName(s);  
128 - compareResults.add(result);  
129 - 128 + ExecutorService serversExecutor = Executors.newFixedThreadPool(10);
  129 + List<CompareResponse> compareResults = new CopyOnWriteArrayList<>();
  130 + for (String s : serviceList) {
  131 + CompareRequest compareRequest = new CompareRequest(s, cloud);
  132 + serversExecutor.submit(new Runnable() {
  133 + @Override
  134 + public void run() {
  135 + CompareResponse result = compareService(compareRequest);
  136 + result.setServiceName(s);
  137 + compareResults.add(result);
  138 + }
  139 + });
  140 + }
  141 + serversExecutor.shutdown();
  142 + try {
  143 + serversExecutor.awaitTermination(50, TimeUnit.SECONDS);
  144 + } catch (InterruptedException e) {
  145 + LOGGER.error("check tasks time out ....");
  146 + if (!serversExecutor.isShutdown()) {
  147 + serversExecutor.shutdownNow();
  148 + }
130 } 149 }
131 resp.setData(compareResults); 150 resp.setData(compareResults);
132 return resp; 151 return resp;
133 } 152 }
134 153
  154 +
135 /** 155 /**
136 - *  
137 * @param request 156 * @param request
138 * @return 157 * @return
139 */ 158 */
@@ -162,8 +181,7 @@ public class CompareCtrl { @@ -162,8 +181,7 @@ public class CompareCtrl {
162 181
163 compareResponse.setIpMaps(configMap); 182 compareResponse.setIpMaps(configMap);
164 183
165 - if(!checkConfig(configMap))  
166 - { 184 + if (!checkConfig(configMap)) {
167 compareResponse.setStatus(1); 185 compareResponse.setStatus(1);
168 } 186 }
169 187