Search in sources :

Example 26 with ExceptionListener

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

the class ExceptionListenerTest method testExceptionListenerStopsConnection_ThrowsIllegalStateException.

@Test
public void testExceptionListenerStopsConnection_ThrowsIllegalStateException() 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.stop();
                fail("Exception not thrown");
            } catch (IllegalStateException ise) {
            // 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#stop() 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) IllegalStateException(javax.jms.IllegalStateException) 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)

Example 27 with ExceptionListener

use of javax.jms.ExceptionListener in project eap-additional-testsuite by jboss-set.

the class HornetQServerManagementTestCase method testCloseConsumerConnectionsForAddress.

@Test
public void testCloseConsumerConnectionsForAddress() throws Exception {
    JMSOperations adminSupport = JMSOperationsProvider.getInstance(managementClient);
    adminSupport.createJmsQueue(getQueueName(), getQueueJndiName());
    Connection connection = connectionFactory.createConnection("guest", "guest");
    HornetQQueue queue = HornetQDestination.createQueue(getQueueName());
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(queue);
    connection.start();
    final CountDownLatch connectionClosed = new CountDownLatch(1);
    connection.setExceptionListener(new ExceptionListener() {

        @Override
        public void onException(JMSException e) {
            System.out.println("HornetQServerManagementTestCase.onException");
            connectionClosed.countDown();
        }
    });
    ModelNode operation = getHornetQServerOperation("close-consumer-connections-for-address");
    operation.get("address-match").set("jms.#");
    System.out.println("operation = " + operation);
    ModelNode result = execute(operation, true);
    System.out.println("result = " + result);
    assertTrue(result.isDefined());
    assertEquals(true, result.asBoolean());
    assertTrue(connectionClosed.await(500, MILLISECONDS));
    // consumer is no longer valid
    try {
        consumer.receiveNoWait();
        connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Assert.fail("consumer can no longer be used after it has been closed");
    } catch (JMSException e) {
    }
    // connection is no longer valid
    try {
        connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Assert.fail("connection can no longer be used after it has been closed");
    } catch (JMSException e) {
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) HornetQQueue(org.hornetq.jms.client.HornetQQueue) JMSOperations(org.jboss.as.test.integration.common.jms.JMSOperations) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) ModelNode(org.jboss.dmr.ModelNode) Session(javax.jms.Session) Test(org.junit.Test)

Example 28 with ExceptionListener

use of javax.jms.ExceptionListener in project scout.rt by eclipse.

the class JmsMomImplementor method postCreateConnection.

protected void postCreateConnection(Connection connection) throws JMSException {
    connection.setClientID(m_clientId);
    connection.setExceptionListener(new ExceptionListener() {

        @Override
        public void onException(final JMSException exception) {
            BEANS.get(MomExceptionHandler.class).handle(exception);
        }
    });
    // we directly start the shared connection
    connection.start();
}
Also used : ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException)

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