Example usage for javax.jms ExceptionListener ExceptionListener

List of usage examples for javax.jms ExceptionListener ExceptionListener

Introduction

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

Prototype

ExceptionListener

Source Link

Usage

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();
    }//  w w  w. java2 s . c om

    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.apache.qpid.server.security.acl.AbstractACLTestCase.java

/**
 * Creates a connection to the broker, and sets a connection listener to prevent failover and an exception listener 
 * with a {@link CountDownLatch} to synchronise in the {@link #check403Exception(Throwable)} method and allow the
 * {@link #tearDown()} method to complete properly.
 *///from w  w w  .j a  v  a2  s .  c  o m
public Connection getConnection(String vhost, String username, String password)
        throws NamingException, JMSException, URLSyntaxException {
    AMQConnection connection = (AMQConnection) getConnection(createConnectionURL(vhost, username, password));

    //Prevent Failover
    connection.setConnectionListener(this);

    //QPID-2081: use a latch to sync on exception causing connection close, to work 
    //around the connection close race during tearDown() causing sporadic failures
    _exceptionReceived = new CountDownLatch(1);

    connection.setExceptionListener(new ExceptionListener() {
        public void onException(JMSException e) {
            _exceptionReceived.countDown();
        }
    });

    return (Connection) connection;
}

From source file:org.apache.stratos.messaging.broker.connect.amqp.AmqpTopicConnector.java

@Override
public void connect() {
    try {/* ww  w . jav a 2  s .  c o  m*/
        if (StringUtils.isNotEmpty(mbUsername)) {
            topicConnection = connectionFactory.createTopicConnection(mbUsername, mbPassword);
        } else {
            topicConnection = connectionFactory.createTopicConnection();
        }

        topicConnection.setExceptionListener(new ExceptionListener() {
            @Override
            public void onException(JMSException e) {
                log.warn("Connection to the message broker failed");
                reconnect();
            }
        });
        topicConnection.start();
    } catch (JMSException e) {
        String message = "Could not connect to message broker";
        log.error(message, e);
        throw new MessagingException(message, e);
    }
}

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.openanzo.combus.bayeux.BridgeConnectionManager.java

/**
 * Creates a single JMS connection and session for use by the BayeuxJMSBridge. It connects to the combus using a configured sysadmin account.
 * /*from w  w w. ja  v  a  2s .  com*/
 * @param factory
 *            this will be used to create the JMS connection and session.
 * @param properties
 *            must contain the username and password
 * @throws JMSException
 */
protected void initialize(ConnectionFactory factory, Properties properties) throws AnzoException {
    try {
        conn = factory.createConnection(credentials.getUserName(), credentials.getPassword());
        conn.setExceptionListener(new ExceptionListener() {
            public void onException(JMSException exception) {
                if (!closed) { // if user has not requested disconnect
                    if (exception.getCause() instanceof BrokerStoppedException
                            || exception.getCause() instanceof TransportDisposedIOException) {
                        closed = true;
                        if (conn != null) {
                            try {
                                conn.close();
                            } catch (JMSException e) {
                                log.debug(LogUtils.COMBUS_MARKER, "Error closing JMS connection", e);
                            }
                        }
                    } else {
                        log.error(LogUtils.COMBUS_MARKER, "Exception over Bayeux JMS connection", exception);
                    }
                }
            }
        });
        conn.start();
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        mp = session.createProducer(null);
        mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // setup all the destination queues
        destinations.put(COMBUS.NOTIFICATION_SERVICE_QUEUE,
                session.createQueue(COMBUS.NOTIFICATION_SERVICE_QUEUE));
        destinations.put(COMBUS.MODEL_SERVICE_QUEUE, session.createQueue(COMBUS.MODEL_SERVICE_QUEUE));
        destinations.put(COMBUS.UPDATE_SERVICE_QUEUE, session.createQueue(COMBUS.UPDATE_SERVICE_QUEUE));
        destinations.put(COMBUS.AUTHENTICATION_SERVICE_QUEUE,
                session.createQueue(COMBUS.AUTHENTICATION_SERVICE_QUEUE));
        destinations.put(COMBUS.REPLICATION_SERVICE_QUEUE,
                session.createQueue(COMBUS.REPLICATION_SERVICE_QUEUE));
        destinations.put(COMBUS.QUERY_SERVICE_QUEUE, session.createQueue(COMBUS.QUERY_SERVICE_QUEUE));
        destinations.put(COMBUS.RESET_SERVICE_QUEUE, session.createQueue(COMBUS.RESET_SERVICE_QUEUE));
        destinations.put(COMBUS.EXECUTION_SERVICE_QUEUE, session.createQueue(COMBUS.EXECUTION_SERVICE_QUEUE));
        destinations.put(COMBUS.AUTHORIZATION_SERVICE_QUEUE,
                session.createQueue(COMBUS.AUTHORIZATION_SERVICE_QUEUE));
    } catch (JMSException jmsex) {
        throw new AnzoException(ExceptionConstants.COMBUS.JMS_CONNECT_FAILED, jmsex);
    }
}

From source file:org.openanzo.combus.CombusConnection.java

private void performConnect() throws JMSException, AnzoException {
    if (connected || closing) {
        return;//from  w w w.  j ava 2 s  .c  o  m
    }
    if (jmsProvider == null) {
        throw new AnzoException(ExceptionConstants.COMBUS.NOTIFICATION_SERVICE_ERROR);
    }
    Properties propertiesNew = new Properties();
    CombusProperties.setHost(propertiesNew, host);
    CombusProperties.setPort(propertiesNew, port);
    CombusProperties.setUseSsl(propertiesNew, useSsl);
    ConnectionFactory connectionFactory = jmsProvider.createConnectionFactory(propertiesNew);
    if (connectionFactory != null) {
        if (userName != null && password != null) {
            connection = connectionFactory.createConnection(userName, password);
        } else {
            connection = connectionFactory.createConnection();
        }
        connection.setExceptionListener(new ExceptionListener() {

            public void onException(JMSException exception) {
                if (!closed) { // if user has not requested disconnect
                    if (exception.getCause() instanceof BrokerStoppedException
                            || exception.getCause() instanceof TransportDisposedIOException) {
                        closed = true;
                    } else {
                        try {
                            fireConnectionStateChange(INotificationConnectionListener.CONNECTIONFAILED);
                            performDisconnect(false);
                        } catch (AnzoException e) {
                            log.error(LogUtils.COMBUS_MARKER,
                                    Messages.formatString(ExceptionConstants.COMBUS.JMS_DISCONNECT_FAILED), e);
                        } finally {
                            connected = false;
                        }
                    }
                }
            }
        });
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        destinations.clear();
        destinations.put(INotificationRegistrationService.SERVICE_NAME,
                session.createQueue(COMBUS.NOTIFICATION_SERVICE_QUEUE));
        destinations.put(IModelService.SERVICE_NAME, session.createQueue(COMBUS.MODEL_SERVICE_QUEUE));
        destinations.put(IAuthorizationService.SERVICE_NAME,
                session.createQueue(COMBUS.AUTHORIZATION_SERVICE_QUEUE));
        destinations.put(IAuthenticationService.SERVICE_NAME,
                session.createQueue(COMBUS.AUTHENTICATION_SERVICE_QUEUE));
        destinations.put(IReplicationService.SERVICE_NAME,
                session.createQueue(COMBUS.REPLICATION_SERVICE_QUEUE));
        destinations.put(IResetService.SERVICE_NAME, session.createQueue(COMBUS.RESET_SERVICE_QUEUE));
        destinations.put(IUpdateService.SERVICE_NAME, session.createQueue(COMBUS.UPDATE_SERVICE_QUEUE));
        destinations.put(IQueryService.SERVICE_NAME, session.createQueue(COMBUS.QUERY_SERVICE_QUEUE));
        destinations.put(IIndexService.SERVICE_NAME, session.createQueue(COMBUS.INDEX_SERVICE_QUEUE));
        destinations.put(IExecutionService.SERVICE_NAME, session.createQueue(COMBUS.EXECUTION_SERVICE_QUEUE));
        tempQueue = session.createTemporaryQueue();
        messageProducer = session.createProducer(null);
        messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        messageConsumer = session.createConsumer(tempQueue);
        listener = new JMSMessageListener();
        messageConsumer.setMessageListener(listener);
        connected = true;
        fireConnectionStateChange(INotificationConnectionListener.CONNECTED);
    }
    return;

}