Example usage for javax.jms Connection setExceptionListener

List of usage examples for javax.jms Connection setExceptionListener

Introduction

In this page you can find the example usage for javax.jms Connection setExceptionListener.

Prototype


void setExceptionListener(ExceptionListener listener) throws JMSException;

Source Link

Document

Sets an exception listener for this connection.

Usage

From source file:com.navercorp.pinpoint.demo.gateway.configuration.GatewayConfiguration.java

@Bean
Connection amqConnection(ConnectionFactory amqConnectionFactory) throws JMSException {
    Connection connection = amqConnectionFactory.createConnection();
    connection.start();//from   w  w w  .j  a v  a2 s.  c  o m
    connection.setExceptionListener(e -> System.out.println("JMS Exception occurred. Shutting down client."));
    return connection;
}

From source file:org.apache.servicemix.jbi.cluster.requestor.ActiveMQJmsRequestorPool.java

protected Connection createConnection() throws JMSException {
    Connection con = getConnectionFactory().createConnection();
    con.setExceptionListener(this);
    return con;/*from w  ww .  ja  va2  s.  c  om*/
}

From source file:ConsumerTool.java

public void run() {
    try {// w  w w.j  a  v  a 2  s.c o m
        running = true;

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        Connection connection = connectionFactory.createConnection();
        if (durable && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
            connection.setClientID(clientId);
        }
        connection.setExceptionListener(this);
        connection.start();

        session = connection.createSession(transacted, ackMode);
        if (topic) {
            destination = session.createTopic(subject);
        } else {
            destination = session.createQueue(subject);
        }

        replyProducer = session.createProducer(null);
        replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        MessageConsumer consumer = null;
        if (durable && topic) {
            consumer = session.createDurableSubscriber((Topic) destination, consumerName);
        } else {
            consumer = session.createConsumer(destination);
        }

        if (maxiumMessages > 0) {
            consumeMessagesAndClose(connection, session, consumer);
        } else {
            if (receiveTimeOut == 0) {
                consumer.setMessageListener(this);
            } else {
                consumeMessagesAndClose(connection, session, consumer, receiveTimeOut);
            }
        }

    } catch (Exception e) {
        System.out.println("[" + this.getName() + "] Caught: " + e);
        e.printStackTrace();
    }
}

From source file:com.chinamobile.bcbsp.comm.ConsumerTool.java

/** Run of Thread. */
public void run() {
    try {/*www.  j  a va  2 s .c  om*/
        running = true;
        this.url = "failover://vm://" + this.brokerName;
        BSPActiveMQConnFactory connectionFactory = new BSPActiveMQConnFactoryImpl();
        connectionFactory.activeMQConnFactoryMethod(url);
        connectionFactory.setOptimizeAcknowledge(true);
        Connection connection = connectionFactory.createConnection();
        if (durable && clientId != null && clientId.length() > 0 && !"null".equals(clientId)) {
            connection.setClientID(clientId);
        }
        connection.setExceptionListener(this);
        connection.start();
        session = connection.createSession(transacted, ackMode);
        if (topic) {
            destination = session.createTopic(subject);
        } else {
            destination = session.createQueue(subject);
        }
        replyProducer = session.createProducer(null);
        replyProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        if (durable && topic) {
            consumer = session.createDurableSubscriber((Topic) destination, consumerName);
        } else {
            consumer = session.createConsumer(destination);
        }
        consumeMessages(connection, session, consumer, receiveTimeOut);
        LOG.info("[ConsumerTool] has received " + this.messagesReceived + " object messages from <"
                + this.subject + ">.");
        LOG.info("[ConsumerTool] has received " + this.messageCount + " BSP messages from <" + this.subject
                + ">.");
        this.receiver.addMessageCount(this.messageCount);
        this.receiver.addMessageBytesCount(messageBytesCount);
    } catch (Exception e) {
        throw new RuntimeException("[ConsumerTool] caught: ", e);
    }
}

From source file:com.mirth.connect.connectors.jms.JmsConnector.java

protected Connection createConnection() throws NamingException, JMSException, InitialisationException {
    Connection connection = null;

    if (connectionFactory == null) {
        connectionFactory = createConnectionFactory();
    }/*from   w ww  .ja v  a 2 s.  co  m*/

    if (connectionFactory != null && connectionFactory instanceof XAConnectionFactory) {
        if (MuleManager.getInstance().getTransactionManager() != null) {
            connectionFactory = new ConnectionFactoryWrapper(connectionFactory,
                    MuleManager.getInstance().getTransactionManager());
        }
    }

    if (username != null) {
        connection = jmsSupport.createConnection(connectionFactory, replacer.replaceValues(username),
                replacer.replaceValues(password));
    } else {
        connection = jmsSupport.createConnection(connectionFactory);
    }

    if (clientId != null) {
        connection.setClientID(replacer.replaceValues(getClientId()));
    }

    if (recoverJmsConnections && connection != null) {
        connection.setExceptionListener(new ExceptionListener() {
            public void onException(JMSException jmsException) {
                logger.debug("About to recycle myself due to remote JMS connection shutdown.");
                final JmsConnector jmsConnector = JmsConnector.this;
                try {
                    jmsConnector.doStop();
                    jmsConnector.initialised.set(false);
                } catch (UMOException e) {
                    logger.warn(e.getMessage(), e);
                }

                try {
                    jmsConnector.doConnect();
                    jmsConnector.doInitialise();
                    jmsConnector.doStart();
                } catch (FatalConnectException fcex) {
                    logger.fatal("Failed to reconnect to JMS server. I'm giving up.");
                } catch (UMOException umoex) {
                    throw new UnhandledException("Failed to recover a connector.", umoex);
                }
            }
        });
    }

    return connection;
}

From source file:org.fusesource.jms.pool.ConnectionPool.java

public ConnectionPool(Connection connection, ObjectPoolFactory poolFactory) throws JMSException {
    this(connection, new ConcurrentHashMap<SessionKey, SessionPool>(), poolFactory);
    /*//from  w w  w  .  j a v  a  2 s.  c o  m
    TODO: activemq specific
    // Add a transport Listener so that we can notice if this connection
    // should be expired due to a connection failure.
    connection.addTransportListener(new TransportListener() {
    public void onCommand(Object command) {
    }
            
    public void onException(IOException error) {
        synchronized (ConnectionPool.this) {
            hasFailed = true;
        }
    }
            
    public void transportInterupted() {
    }
            
    public void transportResumed() {
    }
    });
            
    // make sure that we set the hasFailed flag, in case the transport already failed
    // prior to the addition of our new TransportListener
    if(connection.isTransportFailed()) {
    hasFailed = true;
    }
    */
    connection.setExceptionListener(new ExceptionListener() {
        public void onException(JMSException exception) {
            synchronized (ConnectionPool.this) {
                hasFailed = true;
            }
        }
    });
}

From source file:org.genemania.broker.Worker.java

private void startNewConnection() throws JMSException {
    PooledConnectionFactory connectionFactory = new PooledConnectionFactory(brokerUrl);
    Connection connection = connectionFactory.createConnection();
    connection.setExceptionListener(this);
    connection.start();//from  w w w. jav a  2s. c  o  m
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    responseHandler = session.createProducer(null);
    Queue requestsQueue = session.createQueue(mqRequestsQueueName);
    requestHandler = session.createConsumer(requestsQueue);
    requestHandler.setMessageListener(this);
    LOG.info("Listening to " + requestsQueue.getQueueName());
}

From source file:org.seedstack.jms.internal.JmsFactoryImpl.java

@Override
public Connection createConnection(ConnectionDefinition connectionDefinition) throws JMSException {
    Connection connection;

    if (connectionDefinition.isManaged()) {
        connection = new ManagedConnection(connectionDefinition, this);
        if (connectionDefinition.getExceptionListenerClass() != null) {
            LOGGER.debug("Setting exception listener {} on managed connection {}",
                    connectionDefinition.getExceptionListenerClass(), connectionDefinition.getName());
            connection.setExceptionListener(new ExceptionListenerAdapter(connectionDefinition.getName()));
        }//from w ww. ja  v a  2 s.  co  m
    } else {
        connection = createRawConnection(connectionDefinition);
        if (!connectionDefinition.isJeeMode()) {
            if (connectionDefinition.getExceptionListenerClass() != null) {
                LOGGER.debug("Setting exception listener {} on connection {}",
                        connectionDefinition.getExceptionListenerClass(), connectionDefinition.getName());
                connection.setExceptionListener(new ExceptionListenerAdapter(connectionDefinition.getName()));
            }
        }
    }

    return connection;
}

From source file:org.seedstack.seed.jms.internal.JmsFactoryImpl.java

@Override
public Connection createConnection(ConnectionDefinition connectionDefinition) throws JMSException {
    Connection connection;

    if (connectionDefinition.isManaged()) {
        connection = new ManagedConnection(connectionDefinition, this);
        LOGGER.debug("Setting exception listener {} on managed connection {}",
                connectionDefinition.getExceptionListenerClass(), connectionDefinition.getName());
        connection.setExceptionListener(new ExceptionListenerAdapter(connectionDefinition.getName()));
    } else {//from   ww  w  . ja  va2  s. c  o m
        connection = createRawConnection(connectionDefinition);
        if (!connectionDefinition.isJeeMode()) {
            LOGGER.debug("Setting exception listener {} on connection {}",
                    connectionDefinition.getExceptionListenerClass(), connectionDefinition.getName());
            connection.setExceptionListener(new ExceptionListenerAdapter(connectionDefinition.getName()));
        }
    }

    return connection;
}

From source file:org.springframework.jms.connection.SingleConnectionFactory.java

/**
 * Prepare the given Connection before it is exposed.
 * <p>The default implementation applies ExceptionListener and client id.
 * Can be overridden in subclasses.//www  . j ava2s  . c o  m
 * @param con the Connection to prepare
 * @throws JMSException if thrown by JMS API methods
 * @see #setExceptionListener
 * @see #setReconnectOnException
 */
protected void prepareConnection(Connection con) throws JMSException {
    if (getClientId() != null) {
        con.setClientID(getClientId());
    }
    if (this.aggregatedExceptionListener != null) {
        con.setExceptionListener(this.aggregatedExceptionListener);
    } else if (getExceptionListener() != null || isReconnectOnException()) {
        ExceptionListener listenerToUse = getExceptionListener();
        if (isReconnectOnException()) {
            this.aggregatedExceptionListener = new AggregatedExceptionListener();
            this.aggregatedExceptionListener.delegates.add(this);
            if (listenerToUse != null) {
                this.aggregatedExceptionListener.delegates.add(listenerToUse);
            }
            listenerToUse = this.aggregatedExceptionListener;
        }
        con.setExceptionListener(listenerToUse);
    }
}