Search in sources :

Example 46 with MessageListener

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

the class QueueRepeaterTest method testTransaction.

public void testTransaction() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    connection = factory.createConnection();
    queue = new ActiveMQQueue(getClass().getName() + "." + getName());
    producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    consumerSession = connection.createSession(true, 0);
    producer = producerSession.createProducer(queue);
    consumer = consumerSession.createConsumer(queue);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message m) {
            try {
                TextMessage tm = (TextMessage) m;
                receivedText = tm.getText();
                latch.countDown();
                LOG.info("consumer received message :" + receivedText);
                consumerSession.commit();
                LOG.info("committed transaction");
            } catch (JMSException e) {
                try {
                    consumerSession.rollback();
                    LOG.info("rolled back transaction");
                } catch (JMSException e1) {
                    LOG.info(e1.toString());
                    e1.printStackTrace();
                }
                LOG.info(e.toString());
                e.printStackTrace();
            }
        }
    });
    connection.start();
    TextMessage tm = null;
    try {
        tm = producerSession.createTextMessage();
        tm.setText("Hello, " + new Date());
        producer.send(tm);
        LOG.info("producer sent message :" + tm.getText());
    } catch (JMSException e) {
        e.printStackTrace();
    }
    LOG.info("Waiting for latch");
    latch.await(2, TimeUnit.SECONDS);
    assertNotNull(receivedText);
    LOG.info("test completed, destination=" + receivedText);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage) Date(java.util.Date)

Example 47 with MessageListener

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

the class NonBlockingConsumerRedeliveryTest method testMessageDeleiveredWhenNonBlockingEnabled.

@Test
public void testMessageDeleiveredWhenNonBlockingEnabled() throws Exception {
    final LinkedHashSet<Message> received = new LinkedHashSet<>();
    final LinkedHashSet<Message> beforeRollback = new LinkedHashSet<>();
    final LinkedHashSet<Message> afterRollback = new LinkedHashSet<>();
    Connection connection = connectionFactory.createConnection();
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(destinationName);
    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            received.add(message);
        }
    });
    sendMessages();
    session.commit();
    connection.start();
    assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Consumer has received " + received.size() + " messages.");
            return received.size() == MSG_COUNT;
        }
    }));
    beforeRollback.addAll(received);
    received.clear();
    session.rollback();
    assertTrue("Post-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Consumer has received " + received.size() + " messages since rollback.");
            return received.size() == MSG_COUNT;
        }
    }));
    afterRollback.addAll(received);
    received.clear();
    assertEquals(beforeRollback.size(), afterRollback.size());
    assertEquals(beforeRollback, afterRollback);
    session.commit();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session) Test(org.junit.Test)

Example 48 with MessageListener

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

the class NonBlockingConsumerRedeliveryTest method testNonBlockingMessageDeleiveryIsDelayed.

@Test
public void testNonBlockingMessageDeleiveryIsDelayed() throws Exception {
    final LinkedHashSet<Message> received = new LinkedHashSet<>();
    ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
    connection.getRedeliveryPolicy().setInitialRedeliveryDelay(TimeUnit.SECONDS.toMillis(6));
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(destinationName);
    MessageConsumer consumer = session.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            received.add(message);
        }
    });
    sendMessages();
    connection.start();
    assertTrue("Pre-Rollback expects to receive: " + MSG_COUNT + " messages.", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Consumer has received " + received.size() + " messages.");
            return received.size() == MSG_COUNT;
        }
    }));
    received.clear();
    session.rollback();
    assertFalse("Delayed redelivery test not expecting any messages yet.", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return received.size() > 0;
        }
    }, TimeUnit.SECONDS.toMillis(4)));
    session.commit();
    session.close();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) MessageListener(javax.jms.MessageListener) Session(javax.jms.Session) Test(org.junit.Test)

Example 49 with MessageListener

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

the class ConnectorXBeanConfigTest method testBrokerWontStop.

public void testBrokerWontStop() throws Exception {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?async=false");
    factory.setDispatchAsync(false);
    factory.setAlwaysSessionAsync(false);
    Connection conn = factory.createConnection();
    final Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    conn.start();
    final Destination dest = new ActiveMQQueue("TEST");
    final CountDownLatch stop = new CountDownLatch(1);
    final CountDownLatch sendSecond = new CountDownLatch(1);
    final CountDownLatch shutdown = new CountDownLatch(1);
    final CountDownLatch test = new CountDownLatch(1);
    ActiveMQConnectionFactory testFactory = new ActiveMQConnectionFactory("vm://localhost?async=false");
    Connection testConn = testFactory.createConnection();
    testConn.start();
    Destination testDestination = sess.createQueue("NEW");
    Session testSess = testConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer testProducer = testSess.createProducer(testDestination);
    final Thread consumerThread = new Thread() {

        @Override
        public void run() {
            try {
                MessageProducer producer = sess.createProducer(dest);
                producer.send(sess.createTextMessage("msg1"));
                MessageConsumer consumer = sess.createConsumer(dest);
                consumer.setMessageListener(new MessageListener() {

                    @Override
                    public void onMessage(Message message) {
                        try {
                            // send a message that will block
                            Thread.sleep(2000);
                            sendSecond.countDown();
                            // try to stop the broker
                            Thread.sleep(5000);
                            stop.countDown();
                            // run the test
                            Thread.sleep(5000);
                            test.countDown();
                            shutdown.await();
                        } catch (InterruptedException ie) {
                        }
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    consumerThread.start();
    final Thread producerThread = new Thread() {

        @Override
        public void run() {
            try {
                sendSecond.await();
                MessageProducer producer = sess.createProducer(dest);
                producer.send(sess.createTextMessage("msg2"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    producerThread.start();
    final Thread stopThread = new Thread() {

        @Override
        public void run() {
            try {
                stop.await();
                brokerService.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    stopThread.start();
    test.await();
    try {
        testSess.createConsumer(testDestination);
        fail("Should have failed creating a consumer!");
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        testProducer.send(testSess.createTextMessage("msg3"));
        fail("Should have failed sending a message!");
    } catch (Exception e) {
        e.printStackTrace();
    }
    shutdown.countDown();
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 50 with MessageListener

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

the class JmsConsumerTest method testIndividualACKMessageConsumer.

@Test
public void testIndividualACKMessageConsumer() throws Exception {
    Connection conn = cf.createConnection();
    Session session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
    MessageProducer producer = session.createProducer(jBossQueue);
    MessageConsumer consumer = session.createConsumer(jBossQueue);
    int noOfMessages = 100;
    for (int i = 0; i < noOfMessages; i++) {
        producer.setPriority(2);
        producer.send(session.createTextMessage("m" + i));
    }
    conn.start();
    final AtomicInteger errors = new AtomicInteger(0);
    final ReusableLatch latch = new ReusableLatch();
    latch.setCount(noOfMessages);
    class MessageAckEven implements MessageListener {

        int count = 0;

        @Override
        public void onMessage(Message msg) {
            try {
                TextMessage txtmsg = (TextMessage) msg;
                if (!txtmsg.getText().equals("m" + count)) {
                    errors.incrementAndGet();
                }
                if (count % 2 == 0) {
                    msg.acknowledge();
                }
                count++;
            } catch (Exception e) {
                errors.incrementAndGet();
            } finally {
                latch.countDown();
            }
        }
    }
    consumer.setMessageListener(new MessageAckEven());
    Assert.assertTrue(latch.await(5000));
    session.close();
    session = conn.createSession(false, ActiveMQJMSConstants.INDIVIDUAL_ACKNOWLEDGE);
    consumer = session.createConsumer(jBossQueue);
    // Consume odd numbers first
    for (int i = 0; i < noOfMessages; i++) {
        if (i % 2 == 0) {
            continue;
        }
        TextMessage m = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(m);
        m.acknowledge();
        Assert.assertEquals("m" + i, m.getText());
    }
    SimpleString queueName = new SimpleString(JmsConsumerTest.Q_NAME);
    conn.close();
    Queue queue = server.locateQueue(queueName);
    Wait.assertEquals(0, queue::getDeliveringCount);
    Wait.assertEquals(0, queue::getMessageCount);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageListener(javax.jms.MessageListener) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JMSException(javax.jms.JMSException) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageProducer(javax.jms.MessageProducer) Queue(org.apache.activemq.artemis.core.server.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) 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