Search in sources :

Example 16 with MessageListener

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

the class JmsSessionRecoverTest method doTestAsynchRecover.

/**
 * Test to make sure that an Async recover works.
 *
 * @throws JMSException
 * @throws InterruptedException
 */
public void doTestAsynchRecover() throws JMSException, InterruptedException {
    final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    final String[] errorMessage = new String[] { null };
    final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
    MessageConsumer consumer = session.createConsumer(dest);
    MessageProducer producer = session.createProducer(dest);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.send(session.createTextMessage("First"));
    producer.send(session.createTextMessage("Second"));
    consumer.setMessageListener(new MessageListener() {

        int counter;

        @Override
        public void onMessage(Message msg) {
            counter++;
            try {
                TextMessage message = (TextMessage) msg;
                switch(counter) {
                    case 1:
                        assertEquals("First", message.getText());
                        assertFalse(message.getJMSRedelivered());
                        message.acknowledge();
                        break;
                    case 2:
                        assertEquals("Second", message.getText());
                        assertFalse(message.getJMSRedelivered());
                        session.recover();
                        break;
                    case 3:
                        assertEquals("Second", message.getText());
                        assertTrue(message.getJMSRedelivered());
                        message.acknowledge();
                        doneCountDownLatch.countDown();
                        break;
                    default:
                        errorMessage[0] = "Got too many messages: " + counter;
                        doneCountDownLatch.countDown();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorMessage[0] = "Got exception: " + e;
                doneCountDownLatch.countDown();
            }
        }
    });
    connection.start();
    if (doneCountDownLatch.await(5, TimeUnit.SECONDS)) {
        if (errorMessage[0] != null) {
            fail(errorMessage[0]);
        }
    } else {
        fail("Timeout waiting for async message delivery to complete.");
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 17 with MessageListener

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

the class JmsSessionRecoverTest method doTestAsynchRecoverWithAutoAck.

/**
 * Test to make sure that an Async recover works when using AUTO_ACKNOWLEDGE.
 *
 * @throws JMSException
 * @throws InterruptedException
 */
public void doTestAsynchRecoverWithAutoAck() throws JMSException, InterruptedException {
    final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final String[] errorMessage = new String[] { null };
    final CountDownLatch doneCountDownLatch = new CountDownLatch(1);
    MessageConsumer consumer = session.createConsumer(dest);
    MessageProducer producer = session.createProducer(dest);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    producer.send(session.createTextMessage("First"));
    producer.send(session.createTextMessage("Second"));
    consumer.setMessageListener(new MessageListener() {

        int counter;

        @Override
        public void onMessage(Message msg) {
            counter++;
            try {
                TextMessage message = (TextMessage) msg;
                switch(counter) {
                    case 1:
                        assertEquals("First", message.getText());
                        assertFalse(message.getJMSRedelivered());
                        break;
                    case 2:
                        // This should rollback the delivery of this message..
                        // and re-deliver.
                        assertEquals("Second", message.getText());
                        assertFalse(message.getJMSRedelivered());
                        session.recover();
                        break;
                    case 3:
                        assertEquals("Second", message.getText());
                        assertTrue(message.getJMSRedelivered());
                        doneCountDownLatch.countDown();
                        break;
                    default:
                        errorMessage[0] = "Got too many messages: " + counter;
                        doneCountDownLatch.countDown();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errorMessage[0] = "Got exception: " + e;
                doneCountDownLatch.countDown();
            }
        }
    });
    connection.start();
    if (doneCountDownLatch.await(5000, TimeUnit.SECONDS)) {
        if (errorMessage[0] != null) {
            fail(errorMessage[0]);
        }
    } else {
        fail("Timeout waiting for async message delivery to complete.");
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 18 with MessageListener

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

the class AdvisoryTempDestinationTests method testNoSlowConsumerAdvisory.

public void testNoSlowConsumerAdvisory() throws Exception {
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue queue = s.createTemporaryQueue();
    MessageConsumer consumer = s.createConsumer(queue);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
        }
    });
    Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
    s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
    // start throwing messages at the consumer
    MessageProducer producer = s.createProducer(queue);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        BytesMessage m = s.createBytesMessage();
        m.writeBytes(new byte[1024]);
        producer.send(m);
    }
    Message msg = advisoryConsumer.receive(1000);
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) TemporaryQueue(javax.jms.TemporaryQueue) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) Session(javax.jms.Session)

Example 19 with MessageListener

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

the class AdvisoryTests method testNoSlowConsumerAdvisory.

public void testNoSlowConsumerAdvisory() throws Exception {
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = s.createQueue(getClass().getName());
    MessageConsumer consumer = s.createConsumer(queue);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
        }
    });
    Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
    s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
    // start throwing messages at the consumer
    MessageProducer producer = s.createProducer(queue);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        BytesMessage m = s.createBytesMessage();
        m.writeBytes(new byte[1024]);
        producer.send(m);
    }
    Message msg = advisoryConsumer.receive(1000);
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 20 with MessageListener

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

the class ConsumerListenerTest method createConsumer.

protected Session createConsumer() throws JMSException {
    final String consumerText = "Consumer: " + (++consumerCounter);
    LOG.info("Creating consumer: " + consumerText + " on destination: " + destination);
    Session answer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = answer.createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            LOG.info("Received message by: " + consumerText + " message: " + message);
        }
    });
    return answer;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) 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