您好,登錄后才能下訂單哦!
這篇文章主要講解了“java集合類深的實(shí)例用法”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“java集合類深的實(shí)例用法”吧!
在做云計(jì)算xenserverAPI 連接xenserver和關(guān)閉xenserver時(shí)候,由于有定時(shí)任務(wù)每分鐘刷新一次,定時(shí)任務(wù)中存取數(shù)據(jù)時(shí)候會(huì)經(jīng)常的連接和關(guān)閉xenserver,造成xenserver的連接阻塞,所以把連接放入隊(duì)列中進(jìn)行連接和斷開…………
/** * xenserverAPI調(diào)用接口 * @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){//當(dāng)連接不用時(shí)放到隊(duì)列里 try { Session.logout(connection); } catch (Types.XenAPIException e) {logger.error("xenserver[" + host + "]釋放異常!",e); } catch (XmlRpcException e) {logger.error("xenserver[" + host + "]釋放異常!",e); } } } }
感謝各位的閱讀,以上就是“java集合類深的實(shí)例用法”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對java集合類深的實(shí)例用法這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。