use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.
the class SimpleAmqpQueueMessageIT method testSendFromAmqpAndReceiveBytesMessage.
@Test
public void testSendFromAmqpAndReceiveBytesMessage() throws Exception {
channel.queueDeclare(QUEUE_NAME_NON_EXCLUSIVE, // durable
false, // non-exclusive
false, // autoDelete
true, // options
null);
Map<String, Object> hdrs = new HashMap<String, Object>();
hdrs.put(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
hdrs.put("JMSType", USER_JMS_TYPE_SETTING);
hdrs.put("JMSPriority", 21);
hdrs.put("JMSDeliveryMode", 2);
hdrs.put("DummyProp", 42);
hdrs.put("rmq.jms.silly", "silly attempt");
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().deliveryMode(1).priority(6).headers(hdrs).build();
channel.basicPublish("", QUEUE_NAME_NON_EXCLUSIVE, props, BYTE_ARRAY);
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.DUPS_OK_ACKNOWLEDGE);
// read-only AMQP-mapped queue
Queue queue = new RMQDestination(QUEUE_NAME_NON_EXCLUSIVE, null, null, QUEUE_NAME_NON_EXCLUSIVE);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
BytesMessage message = (BytesMessage) queueReceiver.receive(TEST_RECEIVE_TIMEOUT);
assertNotNull(message, "No message received");
byte[] bytes = new byte[BYTE_ARRAY.length + 2];
int bytesIn = message.readBytes(bytes);
assertEquals(BYTE_ARRAY.length, bytesIn, "Message payload not correct size");
byte[] bytesTrunc = new byte[bytesIn];
System.arraycopy(bytes, 0, bytesTrunc, 0, bytesIn);
assertArrayEquals(BYTE_ARRAY, bytesTrunc, "Payload doesn't match");
// override should work
assertEquals(21, message.getJMSPriority(), "Priority incorrect");
// override should fail
assertEquals(1, message.getJMSDeliveryMode(), "Delivery mode incorrect");
// override should work
assertEquals(USER_JMS_TYPE_SETTING, message.getJMSType(), "JMSType not set correctly");
Enumeration<?> propNames = message.getPropertyNames();
Set<String> propNameSet = new HashSet<String>();
while (propNames.hasMoreElements()) {
propNameSet.add((String) propNames.nextElement());
}
assertEquals(new HashSet<>(Arrays.asList(USER_STRING_PROPERTY_NAME, "DummyProp")), propNameSet, "Headers not set correctly");
assertEquals(STRING_PROP_VALUE, message.getStringProperty(USER_STRING_PROPERTY_NAME), "String property not transferred");
assertEquals("42", message.getStringProperty("DummyProp"), "Numeric property not transferred");
assertThat(message.getJMSTimestamp()).isZero();
}
use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.
the class SimpleAmqpQueueMessageIT method testSendToAmqpAndReceiveTextMessage.
@Test
public void testSendToAmqpAndReceiveTextMessage() 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);
TextMessage message = queueSession.createTextMessage(MESSAGE);
message.setStringProperty(USER_STRING_PROPERTY_NAME, STRING_PROP_VALUE);
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<>(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");
}
assertEquals(MESSAGE, new String(body, "UTF-8"), "Message received not identical to message sent");
}
use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.
the class NackMessageOnRollbackIT method nackParameterTrueRollbackMessageShouldBeInDlxQueue.
@ParameterizedTest
@MethodSource("messageProviderArguments")
public void nackParameterTrueRollbackMessageShouldBeInDlxQueue(MessageProvider messageProvider) throws Exception {
sendMessage();
QueueConnection connection = null;
try {
connection = connection();
QueueSession queueSession = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
RMQDestination queue = new RMQDestination();
queue.setAmqp(true);
queue.setAmqpQueueName(QUEUE_NAME);
QueueReceiver queueReceiver = queueSession.createReceiver(queue);
Message message = messageProvider.message(queueReceiver);
assertNotNull(message);
queueSession.rollback();
// the message has been consumed, no longer in the queue
queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
message = queueReceiver.receive(1000L);
assertNull(message);
// the message has been redelivered to the dead letter queue
queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = new RMQDestination();
queue.setAmqp(true);
queue.setAmqpQueueName(QUEUE_DLX_NAME);
queueReceiver = queueSession.createReceiver(queue);
message = queueReceiver.receive(1000L);
assertNotNull(message);
} finally {
if (connection != null) {
connection.close();
}
}
}
use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.
the class NackMessageOnRollbackIT method sendMessage.
private void sendMessage() throws Exception {
try {
queueConn.start();
QueueSession queueSession = queueConn.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
RMQDestination queue = new RMQDestination();
queue.setAmqp(true);
queue.setAmqpQueueName(QUEUE_NAME);
queue.setAmqpExchangeName(EXCHANGE_NAME);
queue.setAmqpRoutingKey(ROUTING_KEY);
QueueSender queueSender = queueSession.createSender(queue);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = queueSession.createTextMessage(MESSAGE);
queueSender.send(message);
} finally {
reconnect();
}
}
use of com.rabbitmq.jms.admin.RMQDestination in project rabbitmq-jms-client by rabbitmq.
the class RMQSession method createDurableSubscriber.
/**
* {@inheritDoc}
*/
@Override
public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal) throws JMSException {
illegalStateExceptionIfClosed();
RMQDestination topicDest = (RMQDestination) topic;
RMQMessageConsumer previousConsumer = this.subscriptions.get(name);
if (previousConsumer != null) {
// we are changing subscription, or not, if called with the same topic
RMQDestination prevDest = previousConsumer.getDestination();
if (prevDest.equals(topicDest)) {
if (previousConsumer.isClosed()) {
// They called TopicSubscriber.close but didn't unsubscribe
// and they are simply resubscribing with a new one
logger.warn("Re-subscribing to topic '{}' with name '{}'", topicDest, name);
} else {
logger.error("Subscription with name '{}' for topic '{}' already exists", name, topicDest);
throw new JMSException(String.format("Subscription with name [%s] and topic [%s] already exists", name, topicDest));
}
} else {
logger.warn("Previous subscription with name '{}' was for topic '{}' and is replaced by one for topic '{}'", name, prevDest, topicDest);
unsubscribe(name);
}
}
// Create a new subscription
RMQMessageConsumer consumer = (RMQMessageConsumer) createConsumerInternal(topicDest, name, true, messageSelector);
consumer.setDurable(true);
consumer.setNoLocal(noLocal);
this.subscriptions.put(name, consumer);
return consumer;
}
Aggregations