这篇文章主要讲解了“java集合类深的实例用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“java集合类深的实例用法”吧!
在做云计算xenserverAPI 连接xenserver和关闭xenserver时候,由于有定时任务每分钟刷新一次,定时任务中存取数据时候会经常的连接和关闭xenserver,造成xenserver的连接阻塞,所以把连接放入队列中进行连接和断开…………
/** * xenserverAPI调用接口 * @author hanxuan * @version 2016-12-14 */public class XenPool {private static Logger logger = Logger.getLogger(XenPool.class);private static final int XENSERVER_POOL_CAPACITY=20;private static Map<String,Queue<Connection>> pool = new ConcurrentHashMap(100);public XenPool(){}public synchronized 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(); Queue queue = pool.get(host); Connection connection = null;if (queue != null) { Object conn = queue.poll();if (conn != null) { connection = (Connection) conn; } }if (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()); } catch (Types.XenAPIException e) {logger.error("xenserver[" + xenServer.Hostname + "]XenAPIException异常!",e); } catch (XmlRpcException e) {logger.error("xenserver[" + xenServer.Hostname + "]XmlRpcException异常!",e); } }return connection; }public static void disconnect (Connection connection) { String host= null;try { host = connection.getConfig().getServerURL().getHost(); } catch (Exception e) { e.printStackTrace();return; } Queue queue = pool.get(host);if (queue==null){ queue = new ArrayBlockingQueue(XENSERVER_POOL_CAPACITY);pool.put(host, queue); }boolean flag=queue.offer(connection);if (!flag){//当连接不用时放到队列里 try { Session.logout(connection); } catch (Types.XenAPIException e) {logger.error("xenserver[" + host + "]释放异常!",e); } catch (XmlRpcException e) {logger.error("xenserver[" + host + "]释放异常!",e); } } } }
感谢各位的阅读,以上就是“java集合类深的实例用法”的内容了,经过本文的学习后,相信大家对java集合类深的实例用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。