Search in sources :

Example 21 with MessageListener

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

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionDlq.

public void testQueueSessionListenerExceptionDlq() throws Exception {
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("queue-" + getName());
    MessageProducer producer = createProducer(session, queue);
    Message message = createTextMessage(session);
    producer.send(message);
    final Message[] dlqMessage = new Message[1];
    ActiveMQDestination dlqDestination = new ActiveMQQueue("ActiveMQ.DLQ");
    MessageConsumer dlqConsumer = session.createConsumer(dlqDestination);
    final CountDownLatch gotDlqMessage = new CountDownLatch(1);
    dlqConsumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("DLQ Message Received: " + message);
            dlqMessage[0] = message;
            gotDlqMessage.countDown();
        }
    });
    MessageConsumer consumer = session.createConsumer(queue);
    final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
    final CountDownLatch gotMessage = new CountDownLatch(maxDeliveries);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("Message Received: " + message);
            gotMessage.countDown();
            throw new RuntimeException(getName() + " force a redelivery");
        }
    });
    assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
    // check DLQ
    assertTrue("got dlq message", gotDlqMessage.await(20, TimeUnit.SECONDS));
    // check DLQ message cause is captured
    message = dlqMessage[0];
    assertNotNull("dlq message captured", message);
    String cause = message.getStringProperty(ActiveMQMessage.DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY);
    LOG.info("DLQ'd message cause reported as: {}", cause);
    assertTrue("cause 'cause' exception is remembered", cause.contains("RuntimeException"));
    assertTrue("is correct exception", cause.contains(getName()));
    assertTrue("cause exception is remembered", cause.contains("Throwable"));
    assertTrue("cause policy is remembered", cause.contains("RedeliveryPolicy"));
    session.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 22 with MessageListener

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

the class MessageListenerRedeliveryTest method testQueueSessionListenerExceptionRetry.

public void testQueueSessionListenerExceptionRetry() throws Exception {
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue("queue-" + getName());
    MessageProducer producer = createProducer(session, queue);
    Message message = createTextMessage(session, "1");
    producer.send(message);
    message = createTextMessage(session, "2");
    producer.send(message);
    MessageConsumer consumer = session.createConsumer(queue);
    final CountDownLatch gotMessage = new CountDownLatch(2);
    final AtomicInteger count = new AtomicInteger(0);
    final int maxDeliveries = getRedeliveryPolicy().getMaximumRedeliveries();
    final ArrayList<String> received = new ArrayList<>();
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("Message Received: " + message);
            try {
                received.add(((TextMessage) message).getText());
            } catch (JMSException e) {
                e.printStackTrace();
                fail(e.toString());
            }
            if (count.incrementAndGet() < maxDeliveries) {
                throw new RuntimeException(getName() + " force a redelivery");
            }
            // new blood
            count.set(0);
            gotMessage.countDown();
        }
    });
    assertTrue("got message before retry expiry", gotMessage.await(20, TimeUnit.SECONDS));
    for (int i = 0; i < maxDeliveries; i++) {
        assertEquals("got first redelivered: " + i, "1", received.get(i));
    }
    for (int i = maxDeliveries; i < maxDeliveries * 2; i++) {
        assertEquals("got first redelivered: " + i, "2", received.get(i));
    }
    session.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(javax.jms.Message) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 23 with MessageListener

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

the class TempDestDeleteTest method createConsumer.

protected MessageConsumer createConsumer(Destination dest) throws JMSException {
    final String consumerText = "Consumer: " + (++consumerCounter);
    LOG.info("Creating consumer: " + consumerText + " on destination: " + dest);
    MessageConsumer consumer = session.createConsumer(dest);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("Received message by: " + consumerText + " message: " + message);
        }
    });
    return consumer;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener)

Example 24 with MessageListener

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

the class TopicDurableTests method receiveMessagesAsync.

protected List<CompletableFuture<List<Message>>> receiveMessagesAsync(int count, MessageConsumer... consumer) throws JMSException {
    AtomicInteger totalCount = new AtomicInteger(count);
    List<CompletableFuture<List<Message>>> resultsList = new ArrayList<>();
    List<List<Message>> receivedResList = new ArrayList<>();
    for (int i = 0; i < consumer.length; i++) {
        final int index = i;
        resultsList.add(new CompletableFuture<>());
        receivedResList.add(new ArrayList<>());
        MessageListener myListener = message -> {
            System.out.println("Mesages received" + message + " count: " + totalCount.get());
            receivedResList.get(index).add(message);
            if (totalCount.decrementAndGet() == 0) {
                for (int j = 0; j < consumer.length; j++) {
                    resultsList.get(j).complete(receivedResList.get(j));
                }
            }
        };
        consumer[i].setMessageListener(myListener);
    }
    return resultsList;
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers.is(org.hamcrest.CoreMatchers.is) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) NamingException(javax.naming.NamingException) MessageListener(javax.jms.MessageListener) ArrayList(java.util.ArrayList) Session(javax.jms.Session) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) Context(javax.naming.Context) DeliveryMode(javax.jms.DeliveryMode) Message(javax.jms.Message) Hashtable(java.util.Hashtable) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) TextMessage(javax.jms.TextMessage) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Topic(javax.jms.Topic) Test(org.junit.Test) JMSException(javax.jms.JMSException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) ConnectionFactory(javax.jms.ConnectionFactory) CompletableFuture(java.util.concurrent.CompletableFuture) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) ArrayList(java.util.ArrayList) List(java.util.List)

Example 25 with MessageListener

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

the class JMSMessageConsumerTest method doTestProduceAndConsumeLargeNumbersOfMessages.

public void doTestProduceAndConsumeLargeNumbersOfMessages(boolean topic, int ackMode) throws Exception {
    final int MSG_COUNT = 1000;
    final CountDownLatch done = new CountDownLatch(MSG_COUNT);
    JmsConnection connection = (JmsConnection) createConnection();
    connection.setForceAsyncSend(true);
    connection.start();
    Session session = connection.createSession(false, ackMode);
    final Destination destination;
    if (topic) {
        destination = session.createTopic(getTopicName());
    } else {
        destination = session.createQueue(getQueueName());
    }
    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            try {
                message.acknowledge();
                done.countDown();
            } catch (JMSException ex) {
                LOG.info("Caught exception.", ex);
            }
        }
    });
    MessageProducer producer = session.createProducer(destination);
    TextMessage textMessage = session.createTextMessage();
    textMessage.setText("messageText");
    for (int i = 0; i < MSG_COUNT; i++) {
        producer.send(textMessage);
    }
    assertTrue("Did not receive all messages: " + MSG_COUNT, done.await(15, TimeUnit.SECONDS));
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JmsConnection(org.apache.qpid.jms.JmsConnection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

MessageListener (javax.jms.MessageListener)146 Message (javax.jms.Message)128 MessageConsumer (javax.jms.MessageConsumer)103 Session (javax.jms.Session)97 TextMessage (javax.jms.TextMessage)95 CountDownLatch (java.util.concurrent.CountDownLatch)72 Test (org.junit.Test)71 Connection (javax.jms.Connection)60 MessageProducer (javax.jms.MessageProducer)58 JMSException (javax.jms.JMSException)57 Destination (javax.jms.Destination)32 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)31 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)26 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)16 ScheduledMessage (org.apache.activemq.ScheduledMessage)15 ArrayList (java.util.ArrayList)14 Topic (javax.jms.Topic)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)13