Search in sources :

Example 36 with QueueConnection

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

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

use of javax.jms.QueueConnection in project iaf by ibissource.

the class MessagingSource method createSession.

public Session createSession(boolean transacted, int acknowledgeMode) throws IbisException {
    Connection connection = null;
    ;
    Session session;
    try {
        connection = getConnection();
    } catch (JMSException e) {
        throw new JmsException("could not obtain Connection", e);
    }
    try {
        // if (log.isDebugEnabled()) log.debug(getLogPrefix()+"creating Session, openSessionCount before ["+openSessionCount.getValue()+"]");
        if (useJms102()) {
            if (connection instanceof QueueConnection) {
                session = ((QueueConnection) connection).createQueueSession(transacted, acknowledgeMode);
            } else {
                session = ((TopicConnection) connection).createTopicSession(transacted, acknowledgeMode);
            }
        } else {
            session = connection.createSession(transacted, acknowledgeMode);
        }
        openSessionCount.increase();
        if (connectionsArePooled()) {
            connectionTable.put(session, connection);
        }
        return session;
    } catch (JMSException e) {
        releaseConnection(connection);
        throw new JmsException("could not create Session", e);
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 39 with QueueConnection

use of javax.jms.QueueConnection in project qpid-broker-j by apache.

the class QueueReceiverTest method createReceiver.

@Test
public void createReceiver() throws Exception {
    Queue queue = createQueue(getTestName());
    QueueConnection queueConnection = getQueueConnection();
    try {
        queueConnection.start();
        Utils.sendMessages(queueConnection, queue, 3);
        QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueReceiver receiver = session.createReceiver(queue, String.format("%s=2", INDEX));
        assertEquals("Queue names should match from QueueReceiver", queue.getQueueName(), receiver.getQueue().getQueueName());
        Message received = receiver.receive(getReceiveTimeout());
        assertNotNull("Message is not received", received);
        assertEquals("Unexpected message is received", 2, received.getIntProperty(INDEX));
    } finally {
        queueConnection.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 40 with QueueConnection

use of javax.jms.QueueConnection in project qpid-broker-j by apache.

the class QueueSessionTest method testQueueSessionCannotCreateTemporaryTopics.

@Test
public void testQueueSessionCannotCreateTemporaryTopics() throws Exception {
    QueueConnection queueConnection = getQueueConnection();
    try {
        QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        try {
            queueSession.createTemporaryTopic();
            fail("expected exception did not occur");
        } catch (javax.jms.IllegalStateException s) {
        // PASS
        }
    } finally {
        queueConnection.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

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