Example usage for javax.jms QueueConnectionFactory createQueueConnection

List of usage examples for javax.jms QueueConnectionFactory createQueueConnection

Introduction

In this page you can find the example usage for javax.jms QueueConnectionFactory createQueueConnection.

Prototype


QueueConnection createQueueConnection(String userName, String password) throws JMSException;

Source Link

Document

Creates a queue connection with the specified user identity.

Usage

From source file:org.apache.synapse.transport.jms.JMSConnectionFactory.java

/**
 * Begin [or restart] listening for messages on the list of destinations associated
 * with this connection factory. (Called during Axis2 initialization of
 * the Transport receivers, or after a disconnection has been detected)
 *
 * When called from the JMS transport sender, this call simply acquires the actual
 * JMS connection factory from the JNDI, creates a new connection and starts it.
 *
 * @throws JMSException on exceptions/*from  ww w . j a  v a  2  s  . c om*/
 * @throws NamingException on exceptions
 */
public synchronized void connectAndListen() throws JMSException, NamingException {

    // if this is a reconnection/re-initialization effort after the detection of a
    // disconnection, close all sessions and the CF connection and re-initialize
    if (connection != null) {
        log.info("Re-initializing the JMS connection factory : " + name);

        Iterator sessionIter = jmsSessions.values().iterator();
        while (sessionIter.hasNext()) {
            try {
                ((Session) sessionIter.next()).close();
            } catch (JMSException ignore) {
            }
        }
        try {
            connection.stop();
        } catch (JMSException ignore) {
        }

    } else {
        if (log.isDebugEnabled()) {
            log.debug("Initializing the JMS connection factory : " + name);
        }
    }

    // get the CF reference freshly [again] from JNDI
    context = new InitialContext(jndiProperties);
    conFactory = (ConnectionFactory) context.lookup(connFactoryJNDIName);
    log.info("Connected to the JMS connection factory : " + connFactoryJNDIName);

    try {
        ConnectionFactory conFac = null;
        QueueConnectionFactory qConFac = null;
        TopicConnectionFactory tConFac = null;
        if (JMSConstants.DESTINATION_TYPE_QUEUE.equals(getConnectionFactoryType())) {
            qConFac = (QueueConnectionFactory) conFactory;
        } else if (JMSConstants.DESTINATION_TYPE_TOPIC.equals(getConnectionFactoryType())) {
            tConFac = (TopicConnectionFactory) conFactory;
        } else {
            handleException("Unable to determine type of Connection Factory - i.e. Queue/Topic", null);
        }

        String user = (String) jndiProperties.get(Context.SECURITY_PRINCIPAL);
        String pass = (String) jndiProperties.get(Context.SECURITY_CREDENTIALS);

        if (user != null && pass != null) {
            if (qConFac != null) {
                connection = qConFac.createQueueConnection(user, pass);
            } else if (tConFac != null) {
                connection = tConFac.createTopicConnection(user, pass);
            }
        } else {
            if (qConFac != null) {
                connection = qConFac.createQueueConnection();
            } else if (tConFac != null) {
                connection = tConFac.createTopicConnection();
            }
        }

        connection.setExceptionListener(this);

    } catch (JMSException e) {
        handleException("Error connecting to Connection Factory : " + connFactoryJNDIName, e);
    }

    Iterator destJNDINameIter = serviceJNDINameMapping.keySet().iterator();
    while (destJNDINameIter.hasNext()) {
        String destJNDIName = (String) destJNDINameIter.next();
        String destinationType = (String) destinationTypeMapping.get(destJNDIName);
        startListeningOnDestination(destJNDIName, destinationType);
    }

    connection.start(); // indicate readyness to start receiving messages
    log.info("Connection factory : " + name + " initialized...");
}

From source file:org.apache.synapse.transport.jms.JMSSender.java

/**
 * Performs the actual sending of the JMS message
 *//*from   w ww .java2s  .c om*/
public void sendMessage(MessageContext msgCtx, String targetAddress, OutTransportInfo outTransportInfo)
        throws AxisFault {

    JMSConnectionFactory jmsConnectionFactory = null;
    Connection connection = null; // holds a one time connection if used
    JMSOutTransportInfo jmsOut = null;
    Session session = null;
    Destination destination = null;
    Destination replyDestination = null;

    try {
        if (targetAddress != null) {

            jmsOut = new JMSOutTransportInfo(targetAddress);
            // do we have a definition for a connection factory to use for this address?
            jmsConnectionFactory = getJMSConnectionFactory(jmsOut);

            if (jmsConnectionFactory != null) {
                // create new or get existing session to send to the destination from the CF
                session = jmsConnectionFactory.getSessionForDestination(JMSUtils.getDestination(targetAddress));

            } else {
                // digest the targetAddress and locate CF from the EPR
                jmsOut.loadConnectionFactoryFromProperies();
                try {
                    // create a one time connection and session to be used
                    Hashtable jndiProps = jmsOut.getProperties();
                    String user = (String) jndiProps.get(Context.SECURITY_PRINCIPAL);
                    String pass = (String) jndiProps.get(Context.SECURITY_CREDENTIALS);

                    QueueConnectionFactory qConFac = null;
                    TopicConnectionFactory tConFac = null;
                    ConnectionFactory conFac = null;

                    if (JMSConstants.DESTINATION_TYPE_QUEUE.equals(jmsOut.getDestinationType())) {
                        qConFac = (QueueConnectionFactory) jmsOut.getConnectionFactory();
                    } else if (JMSConstants.DESTINATION_TYPE_TOPIC.equals(jmsOut.getDestinationType())) {
                        tConFac = (TopicConnectionFactory) jmsOut.getConnectionFactory();
                    } else {
                        handleException(
                                "Unable to determine type of JMS " + "Connection Factory - i.e Queue/Topic");
                    }

                    if (user != null && pass != null) {
                        if (qConFac != null) {
                            connection = qConFac.createQueueConnection(user, pass);
                        } else if (tConFac != null) {
                            connection = tConFac.createTopicConnection(user, pass);
                        }
                    } else {
                        if (qConFac != null) {
                            connection = qConFac.createQueueConnection();
                        } else if (tConFac != null) {
                            connection = tConFac.createTopicConnection();
                        }
                    }

                    if (JMSConstants.DESTINATION_TYPE_QUEUE.equals(jmsOut.getDestinationType())) {
                        session = ((QueueConnection) connection).createQueueSession(false,
                                Session.AUTO_ACKNOWLEDGE);
                    } else if (JMSConstants.DESTINATION_TYPE_TOPIC.equals(jmsOut.getDestinationType())) {
                        session = ((TopicConnection) connection).createTopicSession(false,
                                Session.AUTO_ACKNOWLEDGE);
                    }

                } catch (JMSException e) {
                    handleException("Error creating a connection/session for : " + targetAddress);
                }
            }
            destination = jmsOut.getDestination();

        } else if (outTransportInfo != null && outTransportInfo instanceof JMSOutTransportInfo) {

            jmsOut = (JMSOutTransportInfo) outTransportInfo;
            jmsConnectionFactory = jmsOut.getJmsConnectionFactory();

            session = jmsConnectionFactory.getSessionForDestination(jmsOut.getDestination().toString());
            destination = jmsOut.getDestination();
        }

        String replyDestName = (String) msgCtx.getProperty(JMSConstants.JMS_REPLY_TO);
        if (replyDestName != null) {
            if (jmsConnectionFactory != null) {
                replyDestination = jmsConnectionFactory.getDestination(replyDestName);
            } else {
                replyDestination = jmsOut.getReplyDestination(replyDestName);
            }
        }

        // now we are going to use the JMS session, but if this was a session from a
        // defined JMS connection factory, we need to synchronize as sessions are not
        // thread safe
        synchronized (session) {

            // convert the axis message context into a JMS Message that we can send over JMS
            Message message = null;
            String correlationId = null;
            try {
                message = createJMSMessage(msgCtx, session);
            } catch (JMSException e) {
                handleException("Error creating a JMS message from the axis message context", e);
            }

            String destinationType = jmsOut.getDestinationType();

            // if the destination does not exist, see if we can create it
            destination = JMSUtils.createDestinationIfRequired(destination, destinationType, targetAddress,
                    session);

            // should we wait for a synchronous response on this same thread?
            boolean waitForResponse = waitForSynchronousResponse(msgCtx);

            // if this is a synchronous out-in, prepare to listen on the response destination
            if (waitForResponse) {
                replyDestination = JMSUtils.setReplyDestination(replyDestination, session, message);
            }

            // send the outgoing message over JMS to the destination selected
            JMSUtils.sendMessageToJMSDestination(session, destination, message);

            // if we are expecting a synchronous response back for the message sent out
            if (waitForResponse) {
                try {
                    connection.start();
                } catch (JMSException ignore) {
                }
                try {
                    correlationId = message.getJMSMessageID();
                } catch (JMSException ignore) {
                }
                waitForResponseAndProcess(session, replyDestination, msgCtx, correlationId);
            }
        }

    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ignore) {
            }
        }
    }
}