Search in sources :

Example 76 with QueueConnection

use of javax.jms.QueueConnection 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)

Example 77 with QueueConnection

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

the class RequeueMessageOnTimeoutIT method connection.

private QueueConnection connection(boolean requeueOnException, boolean requeueOnTimeout) throws Exception {
    RMQConnectionFactory connectionFactory = (RMQConnectionFactory) AbstractTestConnectionFactory.getTestConnectionFactory().getConnectionFactory();
    connectionFactory.setRequeueOnMessageListenerException(requeueOnException);
    connectionFactory.setRequeueOnTimeout(requeueOnTimeout);
    connectionFactory.setOnMessageTimeoutMs(REQUEUE_TIMEOUT_MS);
    QueueConnection queueConnection = connectionFactory.createQueueConnection();
    queueConnection.start();
    return queueConnection;
}
Also used : QueueConnection(javax.jms.QueueConnection) RMQConnectionFactory(com.rabbitmq.jms.admin.RMQConnectionFactory)

Aggregations

QueueConnection (javax.jms.QueueConnection)77 QueueSession (javax.jms.QueueSession)53 Test (org.junit.Test)41 TextMessage (javax.jms.TextMessage)36 Queue (javax.jms.Queue)33 Message (javax.jms.Message)26 MessageProducer (javax.jms.MessageProducer)20 QueueConnectionFactory (javax.jms.QueueConnectionFactory)20 Session (javax.jms.Session)17 JMSException (javax.jms.JMSException)16 InitialContext (javax.naming.InitialContext)15 QueueSender (javax.jms.QueueSender)14 XAQueueConnection (javax.jms.XAQueueConnection)14 ActiveMQRAConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory)14 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)14 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)14 QueueReceiver (javax.jms.QueueReceiver)13 MessageConsumer (javax.jms.MessageConsumer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)9 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6