Search in sources :

Example 51 with QueueReceiver

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

the class AsyncConsumerOnMessageHangIT method testSendAndAsyncReceiveTextMessageWithHang.

/**
 * Basic send and receive with a Consumer. Uses serial send followed by async receive.
 * @throws Exception if test error.
 */
@Test
public void testSendAndAsyncReceiveTextMessageWithHang() throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_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(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    MessageListener listener = new MessageListener();
    queueReceiver.setMessageListener(listener);
    Thread.sleep(4000);
    TextMessage message = (TextMessage) listener.getLastMessage();
    assertEquals(MESSAGE, message.getText());
    assertEquals(1, listener.getMessageCount());
    assertTrue(listener.isSuccess());
    assertTrue(listener.isInterrupted());
}
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 52 with QueueReceiver

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

the class IndividualAcknowledgeQueueMessageIT method messageTestBase.

private void messageTestBase(MessageTestType mtt) throws Exception {
    int noToSend = 10;
    int noToAck = 5;
    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 < noToSend; ++i) {
            queueSender.send(mtt.gen(queueSession, (Serializable) queue));
        }
    } finally {
        reconnect();
    }
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, RMQSession.CLIENT_INDIVIDUAL_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        int i = 0;
        for (; i < noToSend - noToAck; ++i) {
            Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
            mtt.check(msg, (Serializable) queue);
        }
        for (; i < noToSend; ++i) {
            Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
            mtt.check(msg, (Serializable) queue);
            msg.acknowledge();
        }
    } finally // requeues unacknowledged messages
    {
        reconnect();
    }
    {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        for (int i = noToSend - noToAck; i < noToSend; ++i) {
            Message msg = queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
            mtt.check(msg, (Serializable) queue);
        }
    }
}
Also used : Serializable(java.io.Serializable) Message(javax.jms.Message) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 53 with QueueReceiver

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

the class MixedConsumerCheckIT method testSyncThenAsyncReceive.

/**
 * Receive on another thread. Try to set a Listener (asynchronous receive).
 * @throws javax.jms.IllegalStateException if test succeeds.
 */
@Test
public void testSyncThenAsyncReceive() throws Exception {
    assertThrows(javax.jms.IllegalStateException.class, () -> {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
        Queue queue = queueSession.createQueue(QUEUE_NAME);
        Queue queueTwo = queueSession.createQueue(QUEUE_NAME_TWO);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        final QueueReceiver queueReceiverTwo = queueSession.createReceiver(queueTwo);
        new Thread() {

            @Override
            public void run() {
                try {
                    // half a sec’
                    queueReceiverTwo.receive(500);
                } catch (JMSException e) {
                }
            }
        }.start();
        // tenth of a sec’
        Thread.sleep(100);
        // should throw exception
        queueReceiver.setMessageListener(new TestMessageListener());
    });
}
Also used : QueueReceiver(javax.jms.QueueReceiver) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 54 with QueueReceiver

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

the class AbstractITQueue method drainQueue.

protected static void drainQueue(QueueSession session, Queue queue) throws Exception {
    QueueReceiver receiver = session.createReceiver(queue);
    int n = 0;
    Message msg = receiver.receiveNoWait();
    while (msg != null) {
        ++n;
        msg = receiver.receiveNoWait();
    }
    if (n > 0) {
        System.out.println(">> INFO >> Drained messages (n=" + n + ") from queue " + queue + " prior to test.");
    }
}
Also used : Message(javax.jms.Message) QueueReceiver(javax.jms.QueueReceiver)

Example 55 with QueueReceiver

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

the class AsyncConsumerIT method testSendAndAsyncReceiveTextMessage.

/**
 * Basic send and receive with a Consumer. Uses serial send followed by async receive.
 * @throws Exception if test error.
 */
@Test
public void testSendAndAsyncReceiveTextMessage() throws Exception {
    try {
        queueConn.start();
        QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_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(false, Session.DUPS_OK_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue(QUEUE_NAME);
    QueueReceiver queueReceiver = queueSession.createReceiver(queue);
    CountDownLatch latch = new CountDownLatch(1);
    MessageListener listener = new MessageListener(latch);
    queueReceiver.setMessageListener(listener);
    latch.await(1000, TimeUnit.MILLISECONDS);
    TextMessage message = (TextMessage) listener.getLastMessage();
    assertEquals(MESSAGE, message.getText());
    assertEquals(1, listener.getMessageCount());
    assertTrue(listener.isSuccess());
}
Also used : QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) 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