use of com.rabbitmq.client.GetResponse in project wso2-synapse by wso2.
the class RabbitMQConsumer method receive.
/**
* Get a single message from the queue and deserialize for set into the message context
*
* @return the {@link MessageContext} with received message
*/
@Override
public MessageContext receive() {
if (isAlive()) {
GetResponse delivery = null;
try {
delivery = channel.basicGet(queueName, false);
if (delivery != null) {
StorableMessage storableMessage = deserializeMessage(delivery);
org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
MessageContext synapseMc = store.newSynapseMc(axis2Mc);
synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
updateCache(delivery, delivery.getProps().getMessageId());
if (log.isDebugEnabled()) {
log.debug(getId() + " Received MessageId: " + delivery.getProps().getMessageId());
}
return synapseMc;
}
} catch (ShutdownSignalException | IOException e) {
log.error(getId() + " connection error when receiving messages.", e);
cleanup();
} catch (ClassNotFoundException e) {
log.error(getId() + "unable to read the stored message.", e);
try {
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
} catch (IOException ex) {
log.error(getId() + "unable to acknowledge the stored message.", e);
}
}
} else {
log.warn("The connection and channel to the RabbitMQ broker are unhealthy.");
cleanup();
setConnection(store.createConnection());
setChannel(store.createChannel(connection));
}
return null;
}
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 = 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 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.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);
nack(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, this.receivingContextConsumer);
this.messageConsumer.getSession().addUncommittedTag(dtag);
boolean alreadyNacked = false;
try {
this.messageConsumer.getSession().deliverMessage(msg, this.messageListener);
} catch (DeliveryExecutor.DeliveryProcessingTimeoutException timeoutException) {
// happens only if requeueOnTimeout is true
logger.debug("nacking {} because of timeout", dtag);
alreadyNacked = true;
nack(dtag);
} catch (RMQMessageListenerExecutionJMSException e) {
if (e.getCause() instanceof RuntimeException) {
alreadyNacked = true;
nack(dtag);
this.abort();
} else {
throw e;
}
}
if (!alreadyNacked) {
dealWithAcknowledgments(dtag);
}
} else {
// this is the "historical" behavior, not compliant with the spec
dealWithAcknowledgments(dtag);
RMQMessage msg = RMQMessage.convertMessage(this.messageConsumer.getSession(), this.messageConsumer.getDestination(), response, this.receivingContextConsumer);
this.messageConsumer.getSession().addUncommittedTag(dtag);
this.messageConsumer.getSession().deliverMessage(msg, this.messageListener);
}
} else {
// We are unable to deliver the message, nack it
logger.debug("basicNack: dtag='{}' (null MessageListener)", dtag);
nack(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");
}
Aggregations