Search in sources :

Example 16 with TransportListener

use of org.apache.activemq.transport.TransportListener in project opencast by opencast.

the class MessageBaseFacility method reconnect.

/**
 * Opens new sessions and connections to the message broker
 */
public synchronized boolean reconnect() {
    disconnectMessageBroker(false);
    try {
        /* Create a ConnectionFactory for establishing connections to the Active MQ broker */
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
            connectionFactory.setUserName(username);
            connectionFactory.setPassword(password);
        }
        connectionFactory.setTransportListener(new TransportListener() {

            @Override
            public void transportResumed() {
                enable(true);
                logger.info("Connection to ActiveMQ is working");
            }

            @Override
            public void transportInterupted() {
                enable(false);
                logger.error("Connection to ActiveMQ message broker interrupted ({}, username: {})", url, username);
            }

            @Override
            public void onException(IOException ex) {
                enable(false);
                logger.error("ActiveMQ transport exception: {}", ex.getMessage());
            }

            @Override
            public void onCommand(Object obj) {
                logger.trace("ActiveMQ command: {}", obj);
            }
        });
        logger.info("Starting connection to ActiveMQ message broker, waiting until connection is established...");
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    } catch (JMSException e) {
        logger.error("Failed connecting to ActiveMQ message broker using url '{}'", url);
        /* Make sure to set session, etc. to null if connecting failed */
        disconnectMessageBroker(false);
        return false;
    }
    logger.info("Connection to ActiveMQ message broker successfully started");
    return true;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TransportListener(org.apache.activemq.transport.TransportListener) JMSException(javax.jms.JMSException) IOException(java.io.IOException)

Aggregations

TransportListener (org.apache.activemq.transport.TransportListener)16 IOException (java.io.IOException)15 URI (java.net.URI)8 Transport (org.apache.activemq.transport.Transport)6 Test (org.junit.Test)5 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4 WireFormatInfo (org.apache.activemq.command.WireFormatInfo)3 URISyntaxException (java.net.URISyntaxException)2 Connection (javax.jms.Connection)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 Queue (javax.jms.Queue)2 Session (javax.jms.Session)2 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)2 OpenwireArtemisBaseTest (org.apache.activemq.broker.artemiswrapper.OpenwireArtemisBaseTest)2 BrokerInfo (org.apache.activemq.command.BrokerInfo)2 TransportAcceptListener (org.apache.activemq.transport.TransportAcceptListener)2 ArrayDeque (java.util.ArrayDeque)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1