use of com.rabbitmq.client.GetResponse 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 = (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.client.GetResponse 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 = (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");
}
use of com.rabbitmq.client.GetResponse in project rabbitmq-jms-client by rabbitmq.
the class MessageListenerConsumer method handleDelivery.
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException {
logger.trace("consumerTag='{}' envelope='{}'", consumerTag, envelope);
if (this.rejecting) {
long dtag = envelope.getDeliveryTag();
logger.debug("basicNack: dtag='{}'", dtag);
this.messageConsumer.getSession().explicitNack(dtag);
return;
}
/* Wrap the incoming message in a GetResponse */
// last parameter is remaining message count, which we don't know.
GetResponse response = new GetResponse(envelope, properties, body, 0);
try {
long dtag = envelope.getDeliveryTag();
if (this.messageListener != null) {
if (this.requeueOnMessageListenerException) {
// requeuing in case of RuntimeException from the listener
// see https://github.com/rabbitmq/rabbitmq-jms-client/issues/23
// see section 4.5.2 of JMS 1.1 specification
RMQMessage msg = RMQMessage.convertMessage(this.messageConsumer.getSession(), this.messageConsumer.getDestination(), response);
boolean runtimeExceptionInListener = false;
try {
this.messageConsumer.getSession().deliverMessage(msg, this.messageListener);
} catch (RMQMessageListenerExecutionJMSException e) {
if (e.getCause() instanceof RuntimeException) {
runtimeExceptionInListener = true;
this.messageConsumer.getSession().explicitNack(dtag);
this.abort();
} else {
throw e;
}
}
if (!runtimeExceptionInListener) {
this.messageConsumer.dealWithAcknowledgements(this.autoAck, dtag);
}
} else {
// this is the "historical" behavior, not compliant with the spec
this.messageConsumer.dealWithAcknowledgements(this.autoAck, dtag);
RMQMessage msg = RMQMessage.convertMessage(this.messageConsumer.getSession(), this.messageConsumer.getDestination(), response);
this.messageConsumer.getSession().deliverMessage(msg, this.messageListener);
}
} else {
// We are unable to deliver the message, nack it
logger.debug("basicNack: dtag='{}' (null MessageListener)", dtag);
this.messageConsumer.getSession().explicitNack(dtag);
}
} catch (JMSException x) {
logger.error("Error while delivering message", x);
throw new IOException(x);
} catch (InterruptedException ie) {
logger.warn("Message delivery has been interrupted", ie);
throw new IOException("Interrupted while delivering message", ie);
}
}
use of com.rabbitmq.client.GetResponse in project rabbitmq-jms-client by rabbitmq.
the class AmqpPropertiesCustomiserIT method customiserIsApplied.
@Test
public void customiserIsApplied() 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);
TextMessage message = queueSession.createTextMessage(MESSAGE);
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");
assertEquals(new String(body), MESSAGE, "body of response is not correct");
assertEquals(response.getProps().getContentType(), TEXT_PLAIN, "body of response is not correct");
}
use of com.rabbitmq.client.GetResponse in project rabbitmq-queue-management by gessnerfl.
the class MessageOperationExecutorTest method shouldThrowExceptionWhenFunctionCannotBePerformedSuccessfulChecksumCheck.
@Test
public void shouldThrowExceptionWhenFunctionCannotBePerformedSuccessfulChecksumCheck() throws Exception {
GetResponse getResponse = mockDefaultGetResponse();
when(channel.basicGet(DEFAULT_QUEUE_NAME, false)).thenReturn(getResponse);
when(messageChecksum.createFor(DEFAULT_BASIC_PROPERTIES, DEFAULT_PAYLOAD)).thenReturn(DEFAULT_CHECKSUM);
IOException expectedException = new IOException();
doThrow(expectedException).when(function).apply(channel, getResponse);
try {
sut.consumeMessageApplyFunctionAndAckknowlegeOnSuccess(DEFAULT_BROKER_NAME, DEFAULT_QUEUE_NAME, DEFAULT_CHECKSUM, function);
} catch (MessageOperationFailedException e) {
assertSame(expectedException, e.getCause());
}
verify(messageChecksum).createFor(DEFAULT_BASIC_PROPERTIES, DEFAULT_PAYLOAD);
verify(channel).basicGet(DEFAULT_QUEUE_NAME, false);
verify(function).apply(channel, getResponse);
verifyNoMoreInteractions(channel);
}
Aggregations