Search in sources :

Example 26 with MessageListener

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

the class JMSConsumerTest method testMessageListenerWithConsumerCanBeStopped.

public void testMessageListenerWithConsumerCanBeStopped() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch done1 = new CountDownLatch(1);
    final CountDownLatch done2 = new CountDownLatch(1);
    // Receive a message with the JMS API
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = createDestination(session, destinationType);
    ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            counter.incrementAndGet();
            if (counter.get() == 1) {
                done1.countDown();
            }
            if (counter.get() == 2) {
                done2.countDown();
            }
        }
    });
    // Send a first message to make sure that the consumer dispatcher is
    // running
    sendMessages(session, destination, 1);
    assertTrue(done1.await(1, TimeUnit.SECONDS));
    assertEquals(1, counter.get());
    // Stop the consumer.
    consumer.stop();
    // Send a message, but should not get delivered.
    sendMessages(session, destination, 1);
    assertFalse(done2.await(1, TimeUnit.SECONDS));
    assertEquals(1, counter.get());
    // Start the consumer, and the message should now get delivered.
    consumer.start();
    assertTrue(done2.await(1, TimeUnit.SECONDS));
    assertEquals(2, counter.get());
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session)

Example 27 with MessageListener

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

the class JMSConsumerTest method testMessageListenerOnMessageCloseUnackedWithPrefetch1StayInQueue.

public void testMessageListenerOnMessageCloseUnackedWithPrefetch1StayInQueue() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch sendDone = new CountDownLatch(1);
    final CountDownLatch got2Done = new CountDownLatch(1);
    // Set prefetch to 1
    connection.getPrefetchPolicy().setAll(1);
    // This test case does not work if optimized message dispatch is used as
    // the main thread send block until the consumer receives the
    // message. This test depends on thread decoupling so that the main
    // thread can stop the consumer thread.
    connection.setOptimizedMessageDispatch(false);
    connection.start();
    // Use all the ack modes
    Session session = connection.createSession(false, ackMode);
    destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            try {
                TextMessage tm = (TextMessage) m;
                LOG.info("Got in first listener: " + tm.getText());
                assertEquals("" + counter.get(), tm.getText());
                counter.incrementAndGet();
                if (counter.get() == 2) {
                    sendDone.await();
                    connection.close();
                    got2Done.countDown();
                }
                tm.acknowledge();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    });
    // Send the messages
    sendMessages(session, destination, 4);
    sendDone.countDown();
    // Wait for first 2 messages to arrive.
    assertTrue(got2Done.await(100000, TimeUnit.MILLISECONDS));
    // Re-start connection.
    connection = (ActiveMQConnection) factory.createConnection();
    connections.add(connection);
    connection.getPrefetchPolicy().setAll(1);
    connection.start();
    // Pickup the remaining messages.
    final CountDownLatch done2 = new CountDownLatch(1);
    session = connection.createSession(false, ackMode);
    consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            try {
                TextMessage tm = (TextMessage) m;
                LOG.info("Got in second listener: " + tm.getText());
                // order is not guaranteed as the connection is started before the listener is set.
                // assertEquals("" + counter.get(), tm.getText());
                counter.incrementAndGet();
                if (counter.get() == 4) {
                    done2.countDown();
                }
            } catch (Throwable e) {
                LOG.error("unexpected ex onMessage: ", e);
            }
        }
    });
    assertTrue(done2.await(1000, TimeUnit.MILLISECONDS));
    Thread.sleep(200);
    // assert msg 2 was redelivered as close() from onMessages() will only ack in auto_ack and dups_ok mode
    assertEquals(5, counter.get());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 28 with MessageListener

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

the class JMSConsumerTest method testMessageListenerWithConsumerCanBeStoppedConcurently.

public void testMessageListenerWithConsumerCanBeStoppedConcurently() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch closeDone = new CountDownLatch(1);
    connection.start();
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);
    // preload the queue
    sendMessages(session, destination, 2000);
    final ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
    final Map<Thread, Throwable> exceptions = Collections.synchronizedMap(new HashMap<Thread, Throwable>());
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            LOG.error("Uncaught exception:", e);
            exceptions.put(t, e);
        }
    });
    final class AckAndClose implements Runnable {

        private final Message message;

        public AckAndClose(Message m) {
            this.message = m;
        }

        @Override
        public void run() {
            try {
                int count = counter.incrementAndGet();
                if (count == 590) {
                    // close in a separate thread is ok by jms
                    consumer.close();
                    closeDone.countDown();
                }
                if (count % 200 == 0) {
                    // ensure there are some outstanding messages
                    // ack every 200
                    message.acknowledge();
                }
            } catch (Exception e) {
                LOG.error("Exception on close or ack:", e);
                exceptions.put(Thread.currentThread(), e);
            }
        }
    }
    final ExecutorService executor = Executors.newCachedThreadPool();
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            // ack and close eventually in separate thread
            executor.execute(new AckAndClose(m));
        }
    });
    assertTrue(closeDone.await(20, TimeUnit.SECONDS));
    // await possible exceptions
    Thread.sleep(1000);
    assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Session(javax.jms.Session)

Example 29 with MessageListener

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

the class JMSConsumerTest method testSetMessageListenerAfterStart.

public void testSetMessageListenerAfterStart() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch done = new CountDownLatch(1);
    // Receive a message with the JMS API
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    // Send the messages
    sendMessages(session, destination, 4);
    // See if the message get sent to the listener
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            counter.incrementAndGet();
            if (counter.get() == 4) {
                done.countDown();
            }
        }
    });
    assertTrue(done.await(1000, TimeUnit.MILLISECONDS));
    Thread.sleep(200);
    // Make sure only 4 messages were delivered.
    assertEquals(4, counter.get());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session)

Example 30 with MessageListener

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

the class JMSConsumerTest method testMessageListenerWithConsumer.

public void testMessageListenerWithConsumer() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch done = new CountDownLatch(1);
    // Receive a message with the JMS API
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = createDestination(session, destinationType);
    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            counter.incrementAndGet();
            if (counter.get() == 4) {
                done.countDown();
            }
        }
    });
    // Send the messages
    sendMessages(session, destination, 4);
    assertTrue(done.await(1000, TimeUnit.MILLISECONDS));
    Thread.sleep(200);
    // Make sure only 4 messages were delivered.
    assertEquals(4, counter.get());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) 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