Search in sources :

Example 6 with ExceptionListener

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

the class TopicRedeliverTest method testNoExceptionOnRedeliveryAckWithSimpleTopicConsumer.

public void testNoExceptionOnRedeliveryAckWithSimpleTopicConsumer() throws Exception {
    Destination destination = createDestination(getClass().getName());
    Connection connection = createConnection();
    final AtomicBoolean gotException = new AtomicBoolean();
    connection.setExceptionListener(new ExceptionListener() {

        @Override
        public void onException(JMSException exception) {
            LOG.error("unexpected ex:" + exception);
            gotException.set(true);
        }
    });
    connection.setClientID(idGen.generateId());
    connection.start();
    Session consumerSession = connection.createSession(true, Session.CLIENT_ACKNOWLEDGE);
    MessageConsumer consumer = null;
    if (topic) {
        consumer = consumerSession.createConsumer(destination);
    } else {
        consumer = consumerSession.createConsumer(destination);
    }
    Session producerSession = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = producerSession.createProducer(destination);
    producer.setDeliveryMode(deliveryMode);
    TextMessage sentMsg = producerSession.createTextMessage();
    sentMsg.setText("msg1");
    producer.send(sentMsg);
    producerSession.commit();
    Message recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertFalse(recMsg.getJMSRedelivered());
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    consumerSession.rollback();
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(recMsg.getJMSRedelivered());
    consumerSession.rollback();
    recMsg = consumer.receive(RECEIVE_TIMEOUT);
    assertTrue(recMsg.getJMSRedelivered());
    consumerSession.commit();
    assertTrue(recMsg.equals(sentMsg));
    assertTrue(recMsg.getJMSRedelivered());
    connection.close();
    assertFalse("no exception", gotException.get());
}
Also used : Destination(javax.jms.Destination) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 7 with ExceptionListener

use of javax.jms.ExceptionListener in project vcell by virtualcell.

the class ConsumerContextJms method init.

public void init() throws JMSException {
    boolean bTransacted = true;
    int acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
    try {
        this.jmsConnection = vcMessagingServiceJms.createConnectionFactory().createConnection();
        this.jmsConnection.setExceptionListener(new ExceptionListener() {

            public void onException(JMSException arg0) {
                ConsumerContextJms.this.onException(arg0);
            }
        });
        this.jmsConnection.start();
        this.jmsSession = this.jmsConnection.createSession(bTransacted, acknowledgeMode);
        this.jmsMessageConsumer = this.vcMessagingServiceJms.createConsumer(this.jmsSession, vcConsumer.getVCDestination(), vcConsumer.getSelector(), vcConsumer.getPrefetchLimit());
    } catch (JMSException | VCMessagingException e) {
        e.printStackTrace(System.out);
        onException(e);
    }
}
Also used : ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) VCMessagingException(cbit.vcell.message.VCMessagingException)

Example 8 with ExceptionListener

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

the class JMSDestination method createTargetDestinationListener.

private JMSListenerContainer createTargetDestinationListener() {
    Session session = null;
    try {
        // NOPMD - UseTryWithResources
        ExceptionListener exListener = new ExceptionListener() {

            private boolean restartTriggered;

            public synchronized void onException(JMSException exception) {
                if (!shutdown && !restartTriggered) {
                    LOG.log(Level.WARNING, "Exception on JMS connection. Trying to reconnect", exception);
                    new Thread(() -> restartConnection()).start();
                    restartTriggered = true;
                }
            }
        };
        PollingMessageListenerContainer container;
        if (!jmsConfig.isOneSessionPerConnection()) {
            connection = JMSFactory.createConnection(jmsConfig);
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = jmsConfig.getTargetDestination(session);
            container = new PollingMessageListenerContainer(connection, destination, this, exListener);
        } else {
            container = new PollingMessageListenerContainer(jmsConfig, false, this, exListener);
        }
        container.setConcurrentConsumers(jmsConfig.getConcurrentConsumers());
        container.setTransactionManager(jmsConfig.getTransactionManager());
        container.setMessageSelector(jmsConfig.getMessageSelector());
        container.setTransacted(jmsConfig.isSessionTransacted());
        container.setDurableSubscriptionName(jmsConfig.getDurableSubscriptionName());
        container.setPubSubNoLocal(jmsConfig.isPubSubNoLocal());
        Object executor = bus.getProperty(JMSFactory.JMS_DESTINATION_EXECUTOR);
        if (executor instanceof Executor) {
            container.setExecutor((Executor) executor);
        }
        container.setJndiEnvironment(jmsConfig.getJndiEnvironment());
        container.start();
        suspendedContinuations.setListenerContainer(container);
        if (!jmsConfig.isOneSessionPerConnection()) {
            connection.start();
        }
        return container;
    } catch (JMSException e) {
        ResourceCloser.close(connection);
        this.connection = null;
        throw JMSUtil.convertJmsException(e);
    } finally {
        ResourceCloser.close(session);
    }
}
Also used : AbstractMultiplexDestination(org.apache.cxf.transport.AbstractMultiplexDestination) Destination(javax.jms.Destination) Executor(java.util.concurrent.Executor) PollingMessageListenerContainer(org.apache.cxf.transport.jms.util.PollingMessageListenerContainer) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 9 with ExceptionListener

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

the class MessageListenerTest method testWithJTA.

@Test
public void testWithJTA() throws JMSException, XAException, InterruptedException {
    TransactionManager transactionManager = new GeronimoTransactionManager();
    Connection connection = createXAConnection("brokerJTA", transactionManager);
    Queue dest = JMSUtil.createQueue(connection, "test");
    MessageListener listenerHandler = new TestMessageListener();
    ExceptionListener exListener = new TestExceptionListener();
    PollingMessageListenerContainer container = new PollingMessageListenerContainer(connection, dest, listenerHandler, exListener);
    container.setTransacted(false);
    container.setAcknowledgeMode(Session.SESSION_TRANSACTED);
    container.setTransactionManager(transactionManager);
    container.start();
    testTransactionalBehaviour(connection, dest);
    container.stop();
    connection.close();
}
Also used : GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) ExceptionListener(javax.jms.ExceptionListener) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 10 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)

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