FileDocCategorySizeDatePackage
JMSConnectorManager.javaAPI DocApache Axis 1.415519Sat Apr 22 18:57:28 BST 2006org.apache.axis.transport.jms

JMSConnectorManager

public class JMSConnectorManager extends Object
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)

Fields Summary
protected static Log
log
private static JMSConnectorManager
s_instance
private static HashMap
vendorConnectorPools
private int
DEFAULT_WAIT_FOR_SHUTDOWN
Constructors Summary
private JMSConnectorManager()

 // 1.5 minutes

     
    
    
Methods Summary
public voidaddConnectorToPool(JMSConnector conn)
Adds a JMSConnector to the appropriate vendor pool

        if (log.isDebugEnabled()) {
            log.debug("Enter: JMSConnectorManager::addConnectorToPool");
        }

        ShareableObjectPool vendorConnectors = null;
        synchronized (vendorConnectorPools)
        {
            String vendorId = conn.getVendorAdapter().getVendorId();
            vendorConnectors = getVendorPool(vendorId);
            // it's possible the pool does not yet exist (if, for example, the connector
            // is created before invoking the call/JMSTransport, as is the case with
            // SimpleJMSListener)
            if (vendorConnectors == null)
            {
                vendorConnectors = new ShareableObjectPool();
                vendorConnectorPools.put(vendorId, vendorConnectors);
            }
        }

        synchronized (vendorConnectors)
        {
            vendorConnectors.addObject(conn);
        }

        if (log.isDebugEnabled()) {
            log.debug("Exit: JMSConnectorManager::addConnectorToPool");
        }
    
voidcloseAllConnectors()
Closes JMSConnectors in all pools

        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");
        }
    
private voidcloseConnector(JMSConnector conn)

        conn.stop();
        conn.shutdown();
    
voidcloseMatchingJMSConnectors(java.util.HashMap connectorProps, java.util.HashMap cfProps, java.lang.String username, java.lang.String password, org.apache.axis.components.jms.JMSVendorAdapter vendorAdapter)
Closes JMS connectors that match the specified endpoint address

        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");
        }
    
public JMSConnectorgetConnector(java.util.HashMap connectorProperties, java.util.HashMap connectionFactoryProperties, java.lang.String username, java.lang.String password, org.apache.axis.components.jms.JMSVendorAdapter vendorAdapter)
Retrieves a JMSConnector that satisfies the provided connector criteria

        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;
    
public static org.apache.axis.transport.jms.JMSConnectorManagergetInstance()

        return s_instance;
    
public org.apache.axis.transport.jms.JMSConnectorManager$ShareableObjectPoolgetVendorPool(java.lang.String vendorId)
Returns the pool of JMSConnectors for a particular vendor

        return (ShareableObjectPool)vendorConnectorPools.get(vendorId);
    
public voidrelease(JMSConnector connector)
Performs a non-exclusive checkin of the JMSConnector

        ShareableObjectPool pool = null;
        synchronized (vendorConnectorPools)
        {
            pool = getVendorPool(connector.getVendorAdapter().getVendorId());
        }
        if (pool != null)
            pool.release(connector);
    
public voidremoveConnectorFromPool(JMSConnector conn)
Removes a JMSConnector from the appropriate vendor pool

        if (log.isDebugEnabled()) {
            log.debug("Enter: JMSConnectorManager::removeConnectorFromPool");
        }

        ShareableObjectPool vendorConnectors = null;
        synchronized (vendorConnectorPools)
        {
            vendorConnectors = getVendorPool(conn.getVendorAdapter().getVendorId());
        }
        if (vendorConnectors == null)
            return;

        synchronized (vendorConnectors)
        {
            // first release, to decrement the ref count (it is automatically incremented when
            // the connector is matched)
            vendorConnectors.release(conn);
            vendorConnectors.removeObject(conn);
        }

        if (log.isDebugEnabled()) {
            log.debug("Exit: JMSConnectorManager::removeConnectorFromPool");
        }
    
public voidreserve(JMSConnector connector)
Performs a non-exclusive checkout of the JMSConnector

        ShareableObjectPool pool = null;
        synchronized (vendorConnectorPools)
        {
            pool = getVendorPool(connector.getVendorAdapter().getVendorId());
        }
        if (pool != null)
            pool.reserve(connector);