Search in sources :

Example 1 with ExceptionListener

use of javax.jms.ExceptionListener in project rabbitmq-jms-client by rabbitmq.

the class ConnectionCloseIT method testCloseDuringReceiveWithExceptionListener.

@Test
public void testCloseDuringReceiveWithExceptionListener() throws Exception {
    ExceptionListener eListener = new ExceptionListener() {

        @Override
        public void onException(JMSException exception) {
            atomBool.set(true);
            atomJMSExceptionRef.set(exception);
        }
    };
    topicConn.setExceptionListener(eListener);
    topicConn.start();
    TopicSession topicSession = topicConn.createTopicSession(true, Session.DUPS_OK_ACKNOWLEDGE);
    Topic topicDestination = topicSession.createTopic(TOPIC_NAME);
    MessageConsumer messageConsumer = topicSession.createConsumer(topicDestination);
    Completion receiveCompletion = new Completion();
    Thread receiver = new DelayedReceive(ZERO_SECONDS, messageConsumer, receiveCompletion);
    Thread closer = new DelayedClose(ONE_SECOND, topicConn);
    receiver.start();
    closer.start();
    try {
        receiveCompletion.waitUntilComplete(FIVE_SECONDS, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        fail("Timeout before receive returns!");
    }
    if (atomBool.get()) {
        fail(String.format("ExceptionListener driven with exception %s", atomJMSExceptionRef.get()));
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TopicSession(javax.jms.TopicSession) Completion(com.rabbitmq.jms.client.Completion) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 2 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class NioQueueSubscriptionTestListener method testLotsOfConcurrentConnections.

@Ignore("See AMQ-4286")
@Test(timeout = 60 * 1000)
public void testLotsOfConcurrentConnections() throws Exception {
    ExecutorService executor = Executors.newCachedThreadPool();
    final ConnectionFactory factory = createConnectionFactory();
    int connectionCount = 400;
    final AtomicInteger threadId = new AtomicInteger(0);
    for (int i = 0; i < connectionCount; i++) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                final int innerId = threadId.incrementAndGet();
                try {
                    ExceptionListener listener = new NioQueueSubscriptionTestListener(innerId, exceptions, LOG);
                    ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
                    connection.setExceptionListener(listener);
                    connection.start();
                    assertNotNull(connection.getBrokerName());
                    connections.add(connection);
                } catch (Exception e) {
                    LOG.error(">>>> Exception in run() on thread " + innerId, e);
                    exceptions.put(Thread.currentThread(), e);
                }
            }
        });
    }
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    if (!exceptions.isEmpty()) {
        LOG.error(">>>> " + exceptions.size() + " exceptions like", exceptions.values().iterator().next());
        fail("unexpected exceptions in worker threads: " + exceptions.values().iterator().next());
    }
    LOG.info("created " + connectionCount + " connections");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class FailureDeadlockTest method testDeadlock.

// https://jira.jboss.org/jira/browse/JBMESSAGING-1702
// Test that two failures concurrently executing and calling the same exception listener
// don't deadlock
@Test
public void testDeadlock() throws Exception {
    for (int i = 0; i < 100; i++) {
        final Connection conn1 = cf1.createConnection();
        Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
        RemotingConnection rc1 = ((ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession()).getConnection();
        final Connection conn2 = cf2.createConnection();
        Session sess2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        RemotingConnection rc2 = ((ClientSessionInternal) ((ActiveMQSession) sess2).getCoreSession()).getConnection();
        ExceptionListener listener1 = new ExceptionListener() {

            @Override
            public void onException(final JMSException exception) {
                try {
                    conn2.close();
                } catch (Exception e) {
                    FailureDeadlockTest.log.error("Failed to close connection2", e);
                }
            }
        };
        conn1.setExceptionListener(listener1);
        conn2.setExceptionListener(listener1);
        Failer f1 = new Failer(rc1);
        Failer f2 = new Failer(rc2);
        f1.start();
        f2.start();
        f1.join();
        f2.join();
        conn1.close();
        conn2.close();
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 4 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class ActiveMQConnectionFactoryTest method testSetExceptionListener.

public void testSetExceptionListener() throws Exception {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    connection = (ActiveMQConnection) cf.createConnection();
    assertNull(connection.getExceptionListener());
    ExceptionListener exListener = new ExceptionListener() {

        @Override
        public void onException(JMSException arg0) {
        }
    };
    cf.setExceptionListener(exListener);
    connection.close();
    connection = (ActiveMQConnection) cf.createConnection();
    assertNotNull(connection.getExceptionListener());
    assertEquals(exListener, connection.getExceptionListener());
    connection.close();
    connection = (ActiveMQConnection) cf.createConnection();
    assertEquals(exListener, connection.getExceptionListener());
    assertEquals(exListener, cf.getExceptionListener());
    connection.close();
}
Also used : ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException)

Example 5 with ExceptionListener

use of javax.jms.ExceptionListener in project activemq-artemis by apache.

the class DisconnectOnCriticalFailureTest method testClientDisconnectLarge.

@Test(timeout = 60000)
@BMRules(rules = { @BMRule(name = "Corrupt Decoding", targetClass = "org.apache.activemq.artemis.core.protocol.ClientPacketDecoder", targetMethod = "decode(org.apache.activemq.artemis.api.core.ActiveMQBuffer)", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.DisconnectOnCriticalFailureTest.doThrow($1);") })
public void testClientDisconnectLarge() throws Exception {
    Queue q1 = createQueue("queue1");
    final Connection connection = nettyCf.createConnection();
    final CountDownLatch latch = new CountDownLatch(1);
    ServerLocator locator = ((ActiveMQConnectionFactory) nettyCf).getServerLocator();
    int minSize = locator.getMinLargeMessageSize();
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < minSize; i++) {
        builder.append("a");
    }
    try {
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(JMSException e) {
                latch.countDown();
            }
        });
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(q1);
        TextMessage m = session.createTextMessage(builder.toString());
        producer.send(m);
        connection.start();
        corruptPacket.set(true);
        MessageConsumer consumer = session.createConsumer(q1);
        Message lm = consumer.receive(2000);
        // first receive won't crash because the packet
        // is SESS_RECEIVE_LARGE_MSG
        assertNotNull(lm);
        // second receive will force server to send a
        // "forced delivery" message, and will cause
        // the exception to be thrown.
        lm = consumer.receive(5000);
        assertNull(lm);
        assertTrue(latch.await(5, TimeUnit.SECONDS));
    } finally {
        corruptPacket.set(false);
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ExceptionListener(javax.jms.ExceptionListener) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

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