📄 jmsconnectormanager.java
字号:
/* * Copyright 2001, 2002,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.transport.jms;import org.apache.axis.AxisFault;import org.apache.axis.components.jms.JMSVendorAdapter;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.utils.Messages;import org.apache.commons.logging.Log;import java.util.HashMap;import java.util.Iterator;/** * JMSConnectorManager manages a pool of connectors and works with the * vendor adapters to support the reuse of JMS connections. * * @author Ray Chun (rchun@sonicsoftware.com) */public class JMSConnectorManager{ protected static Log log = LogFactory.getLog(JMSConnectorManager.class.getName()); private static JMSConnectorManager s_instance = new JMSConnectorManager(); private static HashMap vendorConnectorPools = new HashMap(); private int DEFAULT_WAIT_FOR_SHUTDOWN = 90000; // 1.5 minutes private JMSConnectorManager() { } public static JMSConnectorManager getInstance() { return s_instance; } /** * Returns the pool of JMSConnectors for a particular vendor */ public ShareableObjectPool getVendorPool(String vendorId) { return (ShareableObjectPool)vendorConnectorPools.get(vendorId); } /** * Retrieves a JMSConnector that satisfies the provided connector criteria */ public JMSConnector getConnector(HashMap connectorProperties, HashMap connectionFactoryProperties, String username, String password, JMSVendorAdapter vendorAdapter) throws AxisFault { JMSConnector connector = null; try { // check for a vendor-specific pool, and create if necessary ShareableObjectPool vendorConnectors = getVendorPool(vendorAdapter.getVendorId()); if (vendorConnectors == null) { synchronized (vendorConnectorPools) { vendorConnectors = getVendorPool(vendorAdapter.getVendorId()); if (vendorConnectors == null) { vendorConnectors = new ShareableObjectPool(); vendorConnectorPools.put(vendorAdapter.getVendorId(), vendorConnectors); } } } // look for a matching JMSConnector among existing connectors synchronized (vendorConnectors) { try { connector = JMSConnectorFactory.matchConnector(vendorConnectors.getElements(), connectorProperties, connectionFactoryProperties, username, password, vendorAdapter); } catch (Exception e) {} // ignore. a new connector will be created if no match is found if (connector == null) { connector = JMSConnectorFactory.createClientConnector(connectorProperties, connectionFactoryProperties, username, password, vendorAdapter); connector.start(); } } } catch (Exception e) { log.error(Messages.getMessage("cannotConnectError"), e); if(e instanceof AxisFault) throw (AxisFault)e; throw new AxisFault("cannotConnect", e); } return connector; } /** * Closes JMSConnectors in all pools */ void closeAllConnectors() { if (log.isDebugEnabled()) { log.debug("Enter: JMSConnectorManager::closeAllConnectors"); } synchronized (vendorConnectorPools) { Iterator iter = vendorConnectorPools.values().iterator(); while (iter.hasNext()) { // close all connectors in the vendor pool ShareableObjectPool pool = (ShareableObjectPool)iter.next(); synchronized (pool) { java.util.Iterator connectors = pool.getElements().iterator(); while (connectors.hasNext()) { JMSConnector conn = (JMSConnector)connectors.next(); try { // shutdown automatically decrements the ref count of a connector before closing it // call reserve() to simulate the checkout reserve(conn); closeConnector(conn); } catch (Exception e) {} // ignore. the connector is already being deactivated } } } } if (log.isDebugEnabled()) { log.debug("Exit: JMSConnectorManager::closeAllConnectors"); } } /** * Closes JMS connectors that match the specified endpoint address */ void closeMatchingJMSConnectors(HashMap connectorProps, HashMap cfProps, String username, String password, JMSVendorAdapter vendorAdapter) { if (log.isDebugEnabled()) { log.debug("Enter: JMSConnectorManager::closeMatchingJMSConnectors"); } try { String vendorId = vendorAdapter.getVendorId(); // get the vendor-specific pool of connectors ShareableObjectPool vendorConnectors = null; synchronized (vendorConnectorPools) { vendorConnectors = getVendorPool(vendorId); } // it's possible that there is no pool for that vendor if (vendorConnectors == null) return; synchronized (vendorConnectors) { // close any matched connectors JMSConnector connector = null; while ((vendorConnectors.size() > 0) && (connector = JMSConnectorFactory.matchConnector(vendorConnectors.getElements(), connectorProps, cfProps, username, password, vendorAdapter)) != null) { closeConnector(connector); } } } catch (Exception e) { log.warn(Messages.getMessage("failedJMSConnectorShutdown"), e); } if (log.isDebugEnabled()) { log.debug("Exit: JMSConnectorManager::closeMatchingJMSConnectors"); } } private void closeConnector(JMSConnector conn) { conn.stop(); conn.shutdown(); } /** * Adds a JMSConnector to the appropriate vendor pool */ public void addConnectorToPool(JMSConnector conn) { if (log.isDebugEnabled()) { log.debug("Enter: JMSConnectorManager::addConnectorToPool"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -