Search in sources :

Example 86 with MessageListener

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

the class JmsConsumerResetActiveListenerTest method testSetListenerFromListener.

/**
 * verify the (undefined by spec) behaviour of setting a listener while receiving a message.
 *
 * @throws Exception
 */
public void testSetListenerFromListener() throws Exception {
    Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Destination dest = session.createQueue("Queue-" + getName());
    final MessageConsumer consumer = session.createConsumer(dest);
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicBoolean first = new AtomicBoolean(true);
    final Vector<Object> results = new Vector<>();
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            if (first.compareAndSet(true, false)) {
                try {
                    consumer.setMessageListener(this);
                    results.add(message);
                } catch (JMSException e) {
                    results.add(e);
                }
            } else {
                results.add(message);
            }
            latch.countDown();
        }
    });
    connection.start();
    MessageProducer producer = session.createProducer(dest);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.send(session.createTextMessage("First"));
    producer.send(session.createTextMessage("Second"));
    assertTrue("we did not timeout", latch.await(5, TimeUnit.SECONDS));
    assertEquals("we have a result", 2, results.size());
    Object result = results.get(0);
    assertTrue(result instanceof TextMessage);
    assertEquals("result is first", "First", ((TextMessage) result).getText());
    result = results.get(1);
    assertTrue(result instanceof TextMessage);
    assertEquals("result is first", "Second", ((TextMessage) result).getText());
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageProducer(javax.jms.MessageProducer) Vector(java.util.Vector) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 87 with MessageListener

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

the class JMSConsumerTest method testMessageListenerWithConsumerWithPrefetch1.

public void testMessageListenerWithConsumerWithPrefetch1() throws Exception {
    final AtomicInteger counter = new AtomicInteger(0);
    final CountDownLatch done = new CountDownLatch(1);
    // Receive a message with the JMS API
    connection.getPrefetchPolicy().setAll(1);
    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)

Example 88 with MessageListener

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

the class JMSConsumerTest method testMessageListenerAutoAckOnCloseWithPrefetch1.

public void testMessageListenerAutoAckOnCloseWithPrefetch1() 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();
                m.acknowledge();
                if (counter.get() == 2) {
                    sendDone.await();
                    connection.close();
                    got2Done.countDown();
                }
            } 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());
                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);
    // close from onMessage with Auto_ack will ack
    // 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) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 89 with MessageListener

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

the class JMSDurableConsumerTest method testDurableConsumerAsync.

@Test(timeout = 30000)
public void testDurableConsumerAsync() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Message> received = new AtomicReference<>();
    String durableClientId = getTopicName() + "-ClientId";
    Connection connection = createConnection(durableClientId);
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(getTopicName());
        MessageConsumer consumer = session.createDurableSubscriber(topic, "DurbaleTopic");
        consumer.setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message message) {
                received.set(message);
                latch.countDown();
            }
        });
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
        TextMessage message = session.createTextMessage();
        message.setText("hello");
        producer.send(message);
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertNotNull("Should have received a message by now.", received.get());
        assertTrue("Should be an instance of TextMessage", received.get() instanceof TextMessage);
    } finally {
        connection.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 90 with MessageListener

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

the class MQTTNetworkOfBrokersFailoverTest method listenForConsumersOn.

private CountDownLatch listenForConsumersOn(BrokerService broker) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    URI brokerUri = broker.getVmConnectorURI();
    final ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(brokerUri.toASCIIString());
    final Connection connection = cf.createConnection();
    connection.start();
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination dest = session.createTopic("ActiveMQ.Advisory.Consumer.Queue.Consumer.foo:AT_LEAST_ONCE.VirtualTopic.foo.bar");
    MessageConsumer consumer = session.createConsumer(dest);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            latch.countDown();
            // shutdown this connection
            Dispatch.getGlobalQueue().execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        session.close();
                        connection.close();
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    });
    return latch;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) 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