Search in sources :

Example 11 with QueueReceiver

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

the class AcknowledgeAnomaliesQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        for (int i = 0; i < 2; ++i) {
            // send two messages
            queueSender.send(mtt.gen(queueSession, (Serializable) queue));
        }
    } finally {
        reconnect();
    }
    Message msgNotAcked = null;
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, RMQSession.CLIENT_INDIVIDUAL_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        msgNotAcked = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
        mtt.check(msgNotAcked, (Serializable) queue);
        Message msg2 = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
        mtt.check(msg2, (Serializable) queue);
        msg2.acknowledge();
        // repeat acknowledgement: should be a no-op
        msg2.acknowledge();
    } finally // requeues unacknowledged messages
    {
        reconnect();
    }
    try {
        // should get an illegal state exception
        msgNotAcked.acknowledge();
    } catch (IllegalStateException ise) {
    // brilliant
    } catch (Exception e) {
        fail(String.format("Message acknowledged outside session gave wrong exception: %s", e));
    }
    {
        queueConn.start();
        // automatically acknowledged
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
        mtt.check(msg, (Serializable) queue);
        // repeat ack: should be a no-op
        msg.acknowledge();
    }
}
Also used : IllegalStateException(javax.jms.IllegalStateException) Serializable(java.io.Serializable) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) IllegalStateException(javax.jms.IllegalStateException)

Example 12 with QueueReceiver

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

the class ReceiveNoWaitIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receiveNoWait(), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 13 with QueueReceiver

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

the class RequeueMessageOnTimeoutIT method requeueParameterTrueTimeoutMessageShouldBeNackedAndConsumerStillActive.

@Test
void requeueParameterTrueTimeoutMessageShouldBeNackedAndConsumerStillActive() throws Exception {
    sendMessage();
    QueueConnection connection = null;
    try {
        connection = connection(true, true);
        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() == 2);
        assertThat(interrupted.get()).isTrue();
        assertThat(receivedMessages).hasSize(2);
        assertThat(receivedMessages.get(0).getJMSRedelivered()).isFalse();
        assertThat(receivedMessages.get(1).getJMSRedelivered()).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)

Example 14 with QueueReceiver

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

the class RequeueMessageOnTimeoutIT method requeueParameterTrueNoExceptionInListenerQueueShouldBeEmpty.

@Test
public void requeueParameterTrueNoExceptionInListenerQueueShouldBeEmpty() 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);
        final CountDownLatch latch = new CountDownLatch(1);
        queueReceiver.setMessageListener(message -> latch.countDown());
        // the message has been consumed, no longer in the queue
        queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueReceiver = queueSession.createReceiver(queue);
        Message message = queueReceiver.receive(1000L);
        assertNull(message);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueReceiver(javax.jms.QueueReceiver) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 15 with QueueReceiver

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

the class SimpleBrowseQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt, String selector, int numExpected) throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        drainQueue(queueSession, queue);
        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        queueSender.send(mtt.gen(queueSession, (Serializable) queue));
    } finally {
        reconnect();
    }
    queueConn.start();
    QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    {
        // Browse queue before receiving message
        QueueBrowser queueBrowser = queueSession.createBrowser(queue, selector);
        Enumeration<?> e = queueBrowser.getEnumeration();
        int numE = 0;
        Message msg = null;
        while (e.hasMoreElements()) {
            ++numE;
            msg = (Message) e.nextElement();
            mtt.check(msg, (Serializable) queue);
        }
        assertEquals(numExpected, numE, "Wrong number of messages on browse queue");
    }
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    mtt.check(queueReceiver.receive(TEST_RECEIVE_TIMEOUT), (Serializable) queue);
}
Also used : Serializable(java.io.Serializable) Enumeration(java.util.Enumeration) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) QueueBrowser(javax.jms.QueueBrowser)

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