Search in sources :

Example 26 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 = (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 27 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 = (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 28 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);
        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);
    }
}
Also used : JMSException(javax.jms.JMSException) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse)

Example 29 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)

Example 30 with GetResponse

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);
}
Also used : IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse) Test(org.junit.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