java集合类深入分析之Queue篇(3)
2017-03-14 00:00
316 查看
摘要: 并发-换一种方式
针对篇(2),做了一些改进:
针对篇(2),做了一些改进:
public class XenPool { private static Logger logger = Logger.getLogger(XenPool.class); private AtomicInteger xenConnectionCount=new AtomicInteger(0); private static Map<String,AtomicInteger> countPool = new ConcurrentHashMap(100); public XenPool(){} public static Connection getConnect(XenServer xenServer) { URL url =null; try { url = new URL(xenServer.Hostname); } catch (MalformedURLException e) { e.printStackTrace(); return null; } String host=url.getHost(); Connection connection = null; try { connection = new Connection(new URL(xenServer.Hostname), XenServer.REPLY_WAIT, XenServer.CONNECT_WAIT); } catch (MalformedURLException e) { logger.error("xenserver[" + xenServer.Hostname + "]建立连接异常!",e); } try { Session.loginWithPassword(connection, xenServer.Username, SystemService.decryptPassword(xenServer.Password), APIVersion.latest().toString()); increase(host); logger.debug("TID["+Thread.currentThread().getId()+"],xenserver[" + host + "]连接建立,连接数["+getConnectionCount(host)+"]!"); } catch (Types.XenAPIException e) { logger.error("xenserver[" + xenServer.Hostname + "]XenAPIException异常!",e); } catch (XmlRpcException e) { logger.error("xenserver[" + xenServer.Hostname + "]XmlRpcException异常!",e); } catch (Exception ex){ logger.error(ex.getMessage(),ex); } return connection; } public static void disconnect (Connection connection) { String host= null; try { host = connection.getConfig().getServerURL().getHost(); } catch (Exception e) { e.printStackTrace(); } if (connection!=null){//当连接不用时放到队列里 try { Session.logout(connection); decrement(host); logger.debug("TID["+Thread.currentThread().getId()+"],xenserver[" + host + "]连接释放,连接数["+getConnectionCount(host)+"]!"); } catch (Types.XenAPIException e) { logger.error("xenserver[" + host + "]释放异常!",e); } catch (XmlRpcException e) { logger.error("xenserver[" + host + "]释放异常!",e); } } } /** * wzw add 上次被清除了 * @param xenServer */ public static void removeHost (XenServer xenServer){ if (xenServer!=null && StringUtils.isNotBlank(xenServer.Hostname)) { URL url =null; try { url = new URL(xenServer.Hostname); } catch (MalformedURLException e) { e.printStackTrace(); } String host=url.getHost(); countPool.remove(host); } } private static void increase(String host){ AtomicInteger count = countPool.get(host); if (count == null) { count = new AtomicInteger(0); countPool.put(host, count); } count.incrementAndGet(); } private static void decrement(String host){ AtomicInteger count = countPool.get(host); if (count == null) { return ; } count.decrementAndGet(); } private static int getConnectionCount(String host){ AtomicInteger count = countPool.get(host); if (count == null) { return 0; } return count.get(); } }
相关文章推荐
- java集合类深入分析之Queue篇
- java集合类深入分析之Queue篇(Q,DQ)
- java集合类深入分析之PriorityQueue(二)
- java集合类深入分析之HashSet, HashMap篇
- java集合类深入分析之Queue篇
- java集合类深入分析之Queue篇(1)
- java集合类深入分析之Queue篇(2)
- java集合类深入分析之List篇
- java集合类深入分析之PriorityQueue
- java集合类深入分析之TreeMap/TreeSet篇
- java集合类深入分析之TreeMap/TreeSet篇
- java集合类深入分析之Queue篇
- (转载)深入分析进程PID相同的奥秘
- 深入分析新浪上的一条新闻
- 深入剖析WTL—WTL框架窗口分析(5)
- 【分析】通用ShellCode深入剖析
- Java源码分析:深入探讨Iterator模式 (转载)
- SQL Server的隔离模式和锁深入分析(一)
- 深入分析 Linux 内核链表
- 深入剖析WTL—WTL框架窗口分析(6)