Search in sources :

Example 21 with ExceptionListener

use of javax.jms.ExceptionListener in project oozie by apache.

the class DefaultConnectionContext method createConnection.

@Override
public void createConnection(Properties props) throws NamingException, JMSException {
    Context jndiContext = new InitialContext(props);
    connectionFactoryName = (String) jndiContext.getEnvironment().get("connectionFactoryNames");
    if (connectionFactoryName == null || connectionFactoryName.trim().length() == 0) {
        connectionFactoryName = "ConnectionFactory";
    }
    ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup(connectionFactoryName);
    LOG.info("Connecting with the following properties \n" + jndiContext.getEnvironment().toString());
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException je) {
                LOG.error("Error in JMS connection", je);
            }
        });
    } catch (JMSException e1) {
        LOG.error(e1.getMessage(), e1);
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e2) {
                LOG.error(e2.getMessage(), e2);
            } finally {
                connection = null;
            }
        }
        throw e1;
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 22 with ExceptionListener

use of javax.jms.ExceptionListener in project jaffa-framework by jaffa-projects.

the class JaffaConnectionFactory method createConnection.

/**
 * Creates a JMS connection, only if one doesn't already exist.
 *
 * @throws FrameworkException
 *           in case any internal error occurs.
 * @throws ApplicationExceptions
 *           Indicates application error(s).
 */
private void createConnection() throws FrameworkException, ApplicationExceptions {
    if (connection == null) {
        Connection _connection = null;
        try {
            final ConnectionFactory connectionFactory = JaffaConnectionFactory.obtainConnectionFactory();
            if (connectionFactory == null) {
                throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.CONNECTION_ERROR, null);
            }
            final JmsConfig jmsConfig = ConfigurationService.getInstance().getJmsConfig();
            if (jmsConfig == null || jmsConfig.getUser() == null)
                _connection = connectionFactory.createConnection();
            else
                _connection = connectionFactory.createConnection(jmsConfig.getUser(), jmsConfig.getPassword());
            // Register a listener to detect connection breakage
            _connection.setExceptionListener(new ExceptionListener() {

                public void onException(JMSException exception) {
                    if (LOGGER.isInfoEnabled())
                        LOGGER.info("The ExceptionListener registered with the JMS Connection '" + connection + "' has been invoked. Will recreate the JMS connection", exception);
                    try {
                        forciblyCreateConnection();
                    } catch (Exception e) {
                        LOGGER.error("Error in recreating a JMS Connection", e);
                    }
                }
            });
            // This will ensure that messages reach the temporary consumers that may
            // be created for deleting/resubmitting
            _connection.start();
            connection = _connection;
        } catch (JMSException e) {
            if (_connection != null) {
                try {
                    _connection.close();
                } catch (JMSException e1) {
                    LOGGER.error("Error closing a JMS Connection", e1);
                }
            }
            LOGGER.error("Error in creating a JMS Connection", e);
            throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.CONNECTION_ERROR, null, e);
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) JmsConfig(org.jaffa.modules.messaging.services.configdomain.JmsConfig) FrameworkException(org.jaffa.exceptions.FrameworkException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 23 with ExceptionListener

use of javax.jms.ExceptionListener in project hive by apache.

the class NotificationListener method createConnection.

/**
 * Create the JMS connection
 * @return newly created JMS connection
 */
protected Connection createConnection() {
    LOG.info("Will create new JMS connection");
    Context jndiCntxt;
    Connection jmsConnection = null;
    try {
        jndiCntxt = new InitialContext();
        ConnectionFactory connFac = (ConnectionFactory) jndiCntxt.lookup("ConnectionFactory");
        jmsConnection = connFac.createConnection();
        jmsConnection.start();
        jmsConnection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException jmse) {
                LOG.error("JMS Exception listener received exception. Ignored the error", jmse);
            }
        });
    } catch (NamingException e) {
        LOG.error("JNDI error while setting up Message Bus connection. " + "Please make sure file named 'jndi.properties' is in " + "classpath and contains appropriate key-value pairs.", e);
    } catch (JMSException e) {
        LOG.error("Failed to initialize connection to message bus", e);
    } catch (Throwable t) {
        LOG.error("Unable to connect to JMS provider", t);
    }
    return jmsConnection;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 24 with ExceptionListener

use of javax.jms.ExceptionListener in project qpid-broker-j by apache.

the class ClientJmsDelegate method setConnectionLostListener.

public void setConnectionLostListener(final ConnectionLostListener connectionLostListener) {
    try {
        _controllerConnection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(final JMSException exception) {
                LOGGER.warn("Caught ", exception);
                if (connectionLostListener != null) {
                    try {
                        _controllerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE).close();
                    } catch (JMSException e) {
                        LOGGER.warn("Unable to create/close a new session, assuming the connection is lost ", exception);
                        connectionLostListener.connectionLost();
                    }
                }
            }
        });
    } catch (JMSException e) {
    // ignore
    }
}
Also used : ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException)

Example 25 with ExceptionListener

use of javax.jms.ExceptionListener in project qpid-broker-j by apache.

the class ExceptionListenerTest method testExceptionListenerClosesConnectionIsAllowed.

@Test
public void testExceptionListenerClosesConnectionIsAllowed() throws Exception {
    assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true)));
    final Connection connection = getConnection();
    try {
        final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1);
        final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>();
        final AtomicReference<Throwable> unexpectedExceptionHolder = new AtomicReference<>();
        final ExceptionListener listener = exception -> {
            exceptionHolder.set(exception);
            try {
                connection.close();
            // PASS
            } catch (Throwable t) {
                unexpectedExceptionHolder.set(t);
            } finally {
                exceptionReceivedLatch.countDown();
            }
        };
        connection.setExceptionListener(listener);
        getBrokerAdmin().restart();
        assertTrue("Exception was not propagated into exception listener in timely manner", exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS));
        assertNotNull("Unexpected exception", exceptionHolder.get());
        assertNull("Connection#close() should not have thrown exception", unexpectedExceptionHolder.get());
    } finally {
        connection.close();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Connection(javax.jms.Connection) Assume.assumeThat(org.junit.Assume.assumeThat) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IllegalStateException(javax.jms.IllegalStateException) AtomicReference(java.util.concurrent.atomic.AtomicReference) JMSException(javax.jms.JMSException) ExceptionListener(javax.jms.ExceptionListener) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) Assert.fail(org.junit.Assert.fail) JmsTestBase(org.apache.qpid.systests.JmsTestBase) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExceptionListener(javax.jms.ExceptionListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ExceptionListener (javax.jms.ExceptionListener)28 JMSException (javax.jms.JMSException)25 Connection (javax.jms.Connection)17 Test (org.junit.Test)13 CountDownLatch (java.util.concurrent.CountDownLatch)7 Session (javax.jms.Session)7 MessageConsumer (javax.jms.MessageConsumer)5 ConnectionFactory (javax.jms.ConnectionFactory)4 MessageProducer (javax.jms.MessageProducer)4 TextMessage (javax.jms.TextMessage)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Queue (javax.jms.Queue)3 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)3 TimeUnit (java.util.concurrent.TimeUnit)2 Destination (javax.jms.Destination)2 IllegalStateException (javax.jms.IllegalStateException)2 Message (javax.jms.Message)2 QueueConnection (javax.jms.QueueConnection)2 Topic (javax.jms.Topic)2