Search in sources :

Example 51 with GetResponse

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;
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) MessageContext(org.apache.synapse.MessageContext) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse)

Example 52 with GetResponse

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");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) BytesMessage(javax.jms.BytesMessage) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 53 with GetResponse

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");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) HashMap(java.util.HashMap) Map(java.util.Map) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 54 with GetResponse

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);
    }
}
Also used : JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse)

Example 55 with GetResponse

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");
}
Also used : RMQDestination(com.rabbitmq.jms.admin.RMQDestination) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) GetResponse(com.rabbitmq.client.GetResponse) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

GetResponse (com.rabbitmq.client.GetResponse)55 Test (org.junit.Test)27 Channel (com.rabbitmq.client.Channel)10 IOException (java.io.IOException)10 Envelope (com.rabbitmq.client.Envelope)8 Connection (com.rabbitmq.client.Connection)7 Message (de.gessnerfl.rabbitmq.queue.management.model.Message)6 HashMap (java.util.HashMap)6 AMQP (com.rabbitmq.client.AMQP)5 LongString (com.rabbitmq.client.LongString)5 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)4 BasicProperties (de.gessnerfl.rabbitmq.queue.management.model.BasicProperties)4 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Queue (javax.jms.Queue)3 QueueSender (javax.jms.QueueSender)3 QueueSession (javax.jms.QueueSession)3