Search in sources :

Example 36 with QueueReceiver

use of javax.jms.QueueReceiver in project zipkin by openzipkin.

the class ActiveMQSpanConsumer method registerInNewSession.

/**
 * JMS contract is one session per thread: we need a new session up to our concurrency level.
 */
void registerInNewSession(ActiveMQConnection connection, String queue) throws JMSException {
    // Pass redundant info as we can't use default method in activeMQ
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    // No need to do anything on ActiveMQ side as physical queues are created on demand
    Queue destination = session.createQueue(queue);
    QueueReceiver receiver = session.createReceiver(destination);
    receiver.setMessageListener(this);
    sessionToReceiver.put(session, receiver);
}
Also used : QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 37 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class RabbitMQRedeliverOnNackIT method testRMQRedeliverOnNack.

/**
 * Test that Rabbit re-delivers a received message which has been rolled-back.
 * @throws Exception on test failure
 */
@Test
public void testRMQRedeliverOnNack() throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, /*not transacted*/
        Session.CLIENT_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage message = queueSession.createTextMessage(MESSAGE);
        queueSender.send(message);
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(true, /*transacted*/
    Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message delivered initially");
    assertEquals(MESSAGE, message.getText(), "Wrong message delivered initially");
    queueSession.rollback();
    message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
    assertNotNull(message, "No message delivered after rollback");
    assertEquals(MESSAGE, message.getText(), "Wrong message redelivered");
    assertTrue(message.getJMSRedelivered(), "Message not marked 'redelivered'");
    queueSession.commit();
}
Also used : QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 38 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class RecoverMessagesIT method testRecoverTextMessageAsyncSync.

@Test
public void testRecoverTextMessageAsyncSync() throws Exception {
    final ArrayList<Message> messages = new ArrayList<Message>();
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    final CountDownLatch latch = new CountDownLatch(2);
    queueReceiver.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
            messages.add(message);
            latch.countDown();
        }
    });
    // allow subscription to take place
    Thread.sleep(100);
    // we should have received one message
    assertEquals(1, messages.size());
    TextMessage tmsg1 = (TextMessage) messages.get(0);
    assertFalse(tmsg1.getJMSRedelivered());
    queueSession.recover();
    latch.await(1000, TimeUnit.MILLISECONDS);
    // we should have received two messages
    // There is no synchronisation so no guarantee we see the latest messages!
    assertEquals(2, messages.size());
    TextMessage tmsg2 = (TextMessage) messages.get(1);
    assertEquals(tmsg1, tmsg2);
    assertTrue(tmsg2.getJMSRedelivered());
    tmsg2.acknowledge();
    queueReceiver.setMessageListener(null);
    TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(tmsg3);
}
Also used : TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ArrayList(java.util.ArrayList) MessageListener(javax.jms.MessageListener) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 39 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class RecoverMessagesIT method testRecoverTextMessageSync.

@Test
public void testRecoverTextMessageSync() throws Exception {
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueSender queueSender = queueSession.createSender(queue);
    queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = queueSession.createTextMessage(MESSAGE);
    queueSender.send(message);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    TextMessage tmsg1 = (TextMessage) queueReceiver.receive();
    assertFalse(tmsg1.getJMSRedelivered());
    queueSession.recover();
    TextMessage tmsg2 = (TextMessage) queueReceiver.receive();
    assertEquals(tmsg1, tmsg2);
    assertTrue(tmsg2.getJMSRedelivered());
    tmsg2.acknowledge();
    TextMessage tmsg3 = (TextMessage) queueReceiver.receiveNoWait();
    assertNull(tmsg3);
}
Also used : QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 40 with QueueReceiver

use of javax.jms.QueueReceiver in project rabbitmq-jms-client by rabbitmq.

the class RequeueMessageOnTimeoutIT method requeueParameterFalseTimeoutConsumerIsClosed.

@Test
void requeueParameterFalseTimeoutConsumerIsClosed() throws Exception {
    sendMessage();
    QueueConnection connection = null;
    try {
        connection = connection(true, false);
        QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        List<Message> receivedMessages = Collections.synchronizedList(new ArrayList<>());
        AtomicBoolean interrupted = new AtomicBoolean(false);
        queueReceiver.setMessageListener(message -> {
            receivedMessages.add(message);
            if (receivedMessages.size() == 1) {
                try {
                    Thread.sleep(REQUEUE_TIMEOUT_MS * 2);
                } catch (InterruptedException e) {
                    interrupted.set(true);
                }
            }
        });
        TestUtils.waitUntil(Duration.ofSeconds(5), () -> receivedMessages.size() == 1);
        assertThat(receivedMessages).hasSize(1);
        assertThat(receivedMessages.get(0).getJMSRedelivered()).isFalse();
        // another consumer can consume the message
        queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueReceiver = queueSession.createReceiver(queue);
        Message message = queueReceiver.receive(1000L);
        assertNotNull(message);
        assertTrue(message.getJMSRedelivered());
        assertThat(interrupted.get()).isTrue();
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Aggregations

QueueReceiver (javax.jms.QueueReceiver)56 QueueSession (javax.jms.QueueSession)47 Queue (javax.jms.Queue)40 TextMessage (javax.jms.TextMessage)28 Message (javax.jms.Message)25 QueueSender (javax.jms.QueueSender)25 Test (org.junit.jupiter.api.Test)17 QueueConnection (javax.jms.QueueConnection)13 Serializable (java.io.Serializable)11 JMSException (javax.jms.JMSException)10 Test (org.junit.Test)10 QueueConnectionFactory (javax.jms.QueueConnectionFactory)6 BytesMessage (javax.jms.BytesMessage)5 InitialContext (javax.naming.InitialContext)5 Destination (javax.jms.Destination)4 MessageProducer (javax.jms.MessageProducer)4 NamingException (javax.naming.NamingException)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 Enumeration (java.util.Enumeration)3 HashMap (java.util.HashMap)3