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);
}
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();
}
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);
}
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);
}
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();
}
}
}
Aggregations