use of javax.jms.QueueSender 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.QueueSender 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.QueueSender in project rabbitmq-jms-client by rabbitmq.
the class RmqDestinationIT method createRmqDestinationWithArguments.
@Test
void createRmqDestinationWithArguments() throws Exception {
this.queueConn.start();
QueueSession session = this.queueConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = new RMQDestination(QUEUE_NAME, true, false, Collections.singletonMap("x-queue-type", "quorum"));
QueueSender sender = session.createSender(queue);
drainQueue(session, queue);
String body = String.valueOf(System.currentTimeMillis());
TextMessage msg = session.createTextMessage(body);
sender.send(msg);
CountDownLatch consumerLatch = new CountDownLatch(1);
AtomicReference<TextMessage> message = new AtomicReference<>();
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(m -> {
message.set((TextMessage) m);
consumerLatch.countDown();
});
assertThat(consumerLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(message.get().getText()).isEqualTo(body);
}
use of javax.jms.QueueSender in project rabbitmq-jms-client by rabbitmq.
the class SimpleAmqpQueueMessageIT method testSendFromJmsAndReceiveJmsTextMessage.
@Test
public void testSendFromJmsAndReceiveJmsTextMessage() throws Exception {
String queueName = UUID.randomUUID().toString();
channel.queueDeclare(queueName, // durable
false, // non-exclusive
false, // autoDelete
true, // options
null);
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
// write-only AMQP-mapped queue
Queue queue = new RMQDestination(queueName, "", queueName, null);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = queueSession.createTextMessage(MESSAGE);
message.setStringProperty("JMSType", "TextMessage");
message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
queueSender.send(message);
queueSession.close();
queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
// read-only AMQP-mapped queue
queue = new RMQDestination(queueName, null, null, queueName);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
message = (TextMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
channel.queueDelete(queueName);
assertNotNull(message, "No message received");
assertEquals(MESSAGE, message.getText(), "Payload doesn't match");
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
assertThat(message.getJMSTimestamp()).isGreaterThan(0);
}
use of javax.jms.QueueSender in project rabbitmq-jms-client by rabbitmq.
the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveBytesMessage.
@Test
public void testSendToAmqpAndReceiveBytesMessage() throws Exception {
channel.queueDeclare(QUEUE_NAME, // durable
false, // exclusive
true, // autoDelete
true, // options
null);
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
// write-only AMQP-mapped queue
Queue queue = new RMQDestination(QUEUE_NAME, "", QUEUE_NAME, null);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
queueSender.setPriority(9);
BytesMessage message = queueSession.createBytesMessage();
message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
message.writeBytes(BYTE_ARRAY);
queueSender.send(message);
queueConn.close();
GetResponse response = channel.basicGet(QUEUE_NAME, false);
assertNotNull(response, "basicGet failed to retrieve a response");
byte[] body = response.getBody();
assertNotNull(body, "body of response is null");
{
Map<String, Object> hdrs = response.getProps().getHeaders();
assertEquals(new HashSet<String>(Arrays.asList("JMSMessageID", "JMSDeliveryMode", "JMSPriority", "JMSTimestamp", USER_STRING_PROPERTY_NAME)), hdrs.keySet(), "Some keys missing");
assertEquals(9, hdrs.get("JMSPriority"), "Priority wrong");
// toString is a bit wiffy
assertEquals("NON_PERSISTENT", hdrs.get("JMSDeliveryMode").toString(), "Delivery mode wrong");
assertEquals(STRING_PROP_VALUE, hdrs.get(USER_STRING_PROPERTY_NAME).toString(), "String property wrong");
}
assertArrayEquals(BYTE_ARRAY, body, "Message received not identical to message sent");
}
Aggregations