Java tutorial
/** * Project: doris.admin.service.failover-0.1.0-SNAPSHOT * * File Created at 2011-5-27 * $Id$ * * Copyright 1999-2100 Alibaba.com Corporation Limited. * All rights reserved. * * This software is the confidential and proprietary information of * Alibaba Company. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Alibaba.com. */ package com.alibaba.doris.admin.service.failover.node.check; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * @deprecated * @author mian.hem */ public class NodeCheckService { private static final Log log = LogFactory.getLog(NodeCheckService.class); /** * ? */ public static final int DEFAULT_POOL_SIZE = 5; /** * ?? */ public static final long DEFAULT_TASK_TIMEOUT = 5000; private int poolSize = DEFAULT_POOL_SIZE; private ExecutorService executorService; public NodeCheckService() { setPoolSize(DEFAULT_POOL_SIZE); } public NodeCheckService(int poolSize) { setPoolSize(poolSize); } /** * ?? * * @see #invokeAll(List, long) */ public List<NodeCheckResult> invokeAll(List<NodeCheckTask> tasks) { return invokeAll(tasks, DEFAULT_TASK_TIMEOUT * tasks.size()); } /** * ?? * * @param timeout ???0? * @see java.util.concurrent.ExecutorService#invokeAll(java.util.Collection) */ public List<NodeCheckResult> invokeAll(List<? extends NodeCheckTask> tasks, long timeout) { List<NodeCheckResult> nodes = new ArrayList<NodeCheckResult>(tasks.size()); try { List<Future<NodeCheckResult>> futures = null; if (timeout < 0) { futures = executorService.invokeAll(tasks, DEFAULT_TASK_TIMEOUT * tasks.size(), TimeUnit.MILLISECONDS); } else { futures = executorService.invokeAll(tasks, timeout, TimeUnit.MILLISECONDS); } for (Future<NodeCheckResult> future : futures) { try { nodes.add(future.get()); } catch (ExecutionException e) { log.error("failed to check node, skip and continue.", e); } } } catch (InterruptedException e) { log.error("failed to check node, skip and continue.", e); } return nodes; } /** * ?ExecutorService * * @param timeout ?? */ private void destoryExecutorService(long timeout) { if (executorService != null && !executorService.isShutdown()) { try { executorService.awaitTermination(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } executorService.shutdown(); } } /** * ?ExecutorService???poolSizeExecutorService */ private void createExecutorService() { destoryExecutorService(1000); executorService = Executors.newFixedThreadPool(poolSize); } /** * ? * * @see #createExecutorService() */ private void setPoolSize(int poolSize) { this.poolSize = poolSize; createExecutorService(); } }