原文链接:https://www.jianshu.com/p/b5fd0f3285e7
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { HealthCheckController.status = ContainerStatus.YELLOW; logger.info("睡眠10s,等待tengine踢出当前web服务"); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("准备关闭容器,先关闭线程!"); this.connector.pause(); Executor executor = this.connector.getProtocolHandler().getExecutor(); if (executor instanceof ThreadPoolExecutor) { try { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor; threadPoolExecutor.shutdown(); if (!threadPoolExecutor.awaitTermination(waitTime, TimeUnit.SECONDS)) { logger.warn("Tomcat thread pool did not shut down gracefully within " + waitTime + " seconds. Proceeding with forceful shutdown"); } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } }}
public class Test { private static Logger logger = LoggerFactory.getLogger(Test.class); private static String url = "http://www.maple.td/check"; public static void main(String[] args) { System.setProperty("java.net.preferIPv4Stack", "true"); ExecutorService service = Executors.newFixedThreadPool(10); for (int i = 0; i < 2; i++) { service.execute(() -> { try { execute(); } catch (InterruptedException e) { e.printStackTrace(); } }); } } private static void execute() throws InterruptedException { int i = 0; while (true) { Thread.sleep(10); i++; try { List<NameValuePair> pairs = new ArrayList<>(4); ResponseResult postResult = null; postResult = poolPost(url, pairs); String response = postResult.getContent(); //利于展示,根据路由到的web服务ip对response以不同颜色区分打印 String sub = response.substring(response.indexOf("[") + 1, response.indexOf("]")); //api1 if (s.equals("172.22.0.2")) { System.err.println("response: " + response); } else { logger.info("response: " + response); } } catch (Exception e) { e.printStackTrace(); } } }private static ResponseResult poolPost(String uri, List<NameValuePair> arguments) throws Exception { // 通过 httpclient 或者其他方式请求指定url,这里省略代码}