Search in sources :

Example 66 with MessageListener

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

the class JmsSchedulerTest method testScheduleRepeated.

@Test
public void testScheduleRepeated() throws Exception {
    final int NUMBER = 10;
    final AtomicInteger count = new AtomicInteger();
    Connection connection = createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(destination);
    final CountDownLatch latch = new CountDownLatch(NUMBER);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            latch.countDown();
            count.incrementAndGet();
        }
    });
    connection.start();
    MessageProducer producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage("test msg");
    long time = 1000;
    message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
    message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 500);
    message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, NUMBER - 1);
    producer.send(message);
    producer.close();
    assertEquals(latch.getCount(), NUMBER);
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
    // wait a little longer - make sure we only get NUMBER of replays
    Thread.sleep(1000);
    assertEquals(NUMBER, count.get());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 67 with MessageListener

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

the class JmsSchedulerTest method testSchedule.

@Test
public void testSchedule() throws Exception {
    final int COUNT = 1;
    Connection connection = createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(destination);
    final CountDownLatch latch = new CountDownLatch(COUNT);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            latch.countDown();
        }
    });
    connection.start();
    long time = 5000;
    MessageProducer producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage("test msg");
    message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
    producer.send(message);
    producer.close();
    // make sure the message isn't delivered early
    Thread.sleep(2000);
    assertEquals(latch.getCount(), COUNT);
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(latch.getCount(), 0);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) ScheduledMessage(org.apache.activemq.ScheduledMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 68 with MessageListener

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

the class MessageConsumerTest method testCloseAfterStop.

// Test that stop doesn't in any way break subsequent close
@Test
public void testCloseAfterStop() throws Exception {
    Connection producerConnection = null;
    Connection consumerConnection = null;
    try {
        producerConnection = createConnection();
        consumerConnection = createConnection();
        Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer queueProducer = producerSession.createProducer(queue1);
        MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
        MessageListener myListener = new MessageListener() {

            @Override
            public void onMessage(final Message message) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                // Ignore
                }
            }
        };
        queueConsumer.setMessageListener(myListener);
        consumerConnection.start();
        for (int i = 0; i < 100; i++) {
            queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
        }
        consumerConnection.stop();
        consumerConnection.close();
        consumerConnection = null;
    } finally {
        if (producerConnection != null) {
            producerConnection.close();
        }
        if (consumerConnection != null) {
            consumerConnection.close();
        }
        removeAllMessages(queue1.getQueueName(), true);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 69 with MessageListener

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

the class MessageConsumerTest method testRelayMessage.

@Test
public void testRelayMessage() throws Exception {
    Connection conn = null;
    try {
        conn = createConnection();
        conn.start();
        final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageProducer prod = sess.createProducer(queue2);
        MessageConsumer cons = sess.createConsumer(queue1);
        final int numMessages = 100;
        class MyListener implements MessageListener {

            boolean failed;

            int count;

            @Override
            public synchronized void onMessage(final Message m) {
                try {
                    prod.send(m);
                    count++;
                    if (count == numMessages) {
                        notify();
                    }
                } catch (JMSException e) {
                    failed = true;
                }
            }

            synchronized void waitForMessages() throws Exception {
                while (count < numMessages) {
                    this.wait();
                }
            }
        }
        MyListener listener = new MyListener();
        cons.setMessageListener(listener);
        final Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod2 = sess2.createProducer(queue1);
        for (int i = 0; i < numMessages; i++) {
            prod2.send(sess2.createMessage());
        }
        listener.waitForMessages();
        conn.close();
        ProxyAssertSupport.assertFalse(listener.failed);
    } finally {
        if (conn != null) {
            conn.close();
        }
        removeAllMessages(queue2.getQueueName(), true);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Example 70 with MessageListener

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

the class MessageConsumerTest method testStopConnectionDuringOnMessage.

@Test
public void testStopConnectionDuringOnMessage() throws Exception {
    if (log.isTraceEnabled()) {
        log.trace("testStopConnectionWhileOnMessageIsExecuting");
    }
    final AtomicInteger messagesReceived = new AtomicInteger(0);
    CountDownLatch messagesReceivedLatch = new CountDownLatch(1);
    MessageListener myListener = message -> {
        if (messagesReceived.incrementAndGet() == 10) {
            messagesReceivedLatch.countDown();
            try {
                Thread.sleep(200L);
            } catch (InterruptedException e) {
            // Ignore
            }
        }
    };
    Connection producerConnection = null;
    Connection consumerConnection = null;
    try {
        producerConnection = createConnection();
        consumerConnection = createConnection();
        Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer queueProducer = producerSession.createProducer(queue1);
        MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);
        queueConsumer.setMessageListener(myListener);
        log.trace("Starting consumer connection");
        consumerConnection.start();
        final int MESSAGE_COUNT = 100;
        log.trace("Sending the first batch of messages");
        for (int i = 0; i < MESSAGE_COUNT / 2; i++) {
            queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
        }
        messagesReceivedLatch.await(500, TimeUnit.MILLISECONDS);
        log.trace("Stopping consumer connection");
        consumerConnection.stop();
        int countAfterStop = messagesReceived.get();
        ProxyAssertSupport.assertTrue("Should have received some messages before stopping", countAfterStop > 0);
        log.trace("Sending the second batch of messages");
        for (int i = MESSAGE_COUNT / 2; i < MESSAGE_COUNT; i++) {
            queueProducer.send(producerSession.createTextMessage("Message #" + Integer.toString(i)));
        }
        ProxyAssertSupport.assertFalse("Should not receive any messages after the connection has been stopped", Wait.waitFor(() -> messagesReceived.get() > countAfterStop, 2000, 1000));
        log.trace("Restarting consumer connection");
        consumerConnection.start();
        ProxyAssertSupport.assertTrue("Should have received all messages after restarting", Wait.waitFor(() -> messagesReceived.get() == MESSAGE_COUNT, 15000, 100));
    } finally {
        if (producerConnection != null) {
            producerConnection.close();
        }
        if (consumerConnection != null) {
            consumerConnection.close();
        }
        removeAllMessages(queue1.getQueueName(), true);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) MessageListener(javax.jms.MessageListener) ArrayList(java.util.ArrayList) Session(javax.jms.Session) ActiveMQTestBase(org.apache.activemq.artemis.tests.util.ActiveMQTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvalidDestinationException(javax.jms.InvalidDestinationException) QueueReceiver(javax.jms.QueueReceiver) TopicSession(javax.jms.TopicSession) MessageProducer(javax.jms.MessageProducer) TopicSubscriber(javax.jms.TopicSubscriber) DeliveryMode(javax.jms.DeliveryMode) Message(javax.jms.Message) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) Iterator(java.util.Iterator) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) Topic(javax.jms.Topic) Test(org.junit.Test) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) MessageConsumer(javax.jms.MessageConsumer) ProxyAssertSupport(org.apache.activemq.artemis.jms.tests.util.ProxyAssertSupport) Assert(org.junit.Assert) Collections(java.util.Collections) Wait(org.apache.activemq.artemis.junit.Wait) MessageConsumer(javax.jms.MessageConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(javax.jms.MessageListener) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

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