Example usage for com.rabbitmq.client GetResponse GetResponse

List of usage examples for com.rabbitmq.client GetResponse GetResponse

Introduction

In this page you can find the example usage for com.rabbitmq.client GetResponse GetResponse.

Prototype

public GetResponse(Envelope envelope, BasicProperties props, byte[] body, int messageCount) 

Source Link

Document

Construct a GetResponse with the specified construction parameters

Usage

From source file:com.cisco.oss.foundation.message.AbstractRabbitMQMessageHandler.java

License:Apache License

/**
 * No-op implementation of {@link Consumer#handleDelivery}.
 *//*  w ww .j a  v  a  2 s . c o  m*/
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws IOException {

    Object fc = properties.getHeaders().get(QueueConstants.FLOW_CONTEXT_HEADER);
    String flowContextStr = fc != null ? fc.toString() : null;
    if (StringUtils.isNotBlank(flowContextStr)) {
        FlowContextFactory.deserializeNativeFlowContext(flowContextStr);
    }
    GetResponse getResponse = new GetResponse(envelope, properties, body, 0);

    Message msg = new RabbitMQMessage(getResponse, consumerTag);
    preMessageProcessing(msg);
    onMessage(msg);
    postMessageProcessing(msg);
}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumer.java

License:Apache License

@Override
public Message receive() {

    try {//from ww w  .  ja  v  a  2  s.c om
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        GetResponse getResponse = new GetResponse(delivery.getEnvelope(), delivery.getProperties(),
                delivery.getBody(), 0);
        RabbitMQMessage rabbitMQMessage = new RabbitMQMessage(getResponse, "");
        return rabbitMQMessage;
    } catch (InterruptedException e) {
        throw new QueueException("can't get new message: " + e, e);
    }

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumer.java

License:Apache License

@Override
public Message receive(long timeout) {
    try {/*from w ww . ja  v a 2 s  .  c  o  m*/
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(timeout);
        GetResponse getResponse = new GetResponse(delivery.getEnvelope(), delivery.getProperties(),
                delivery.getBody(), 0);
        RabbitMQMessage rabbitMQMessage = new RabbitMQMessage(getResponse, "");
        return rabbitMQMessage;
    } catch (InterruptedException e) {
        throw new QueueException("can't get new message: " + e, e);
    }
}

From source file:com.github.larsq.spring.embeddedamqp.QueueInfo.java

License:Open Source License

Optional<GetResponse> receive() {
    Optional<Message> optionalMessage = container.messages(queue).map(q -> q.poll());

    if (!optionalMessage.isPresent()) {
        return Optional.empty();
    }//from   ww w. j  a  v a  2 s  .  c  om

    Message m = optionalMessage.get();

    refresh();

    GetResponse response = new GetResponse(m.getEnvelope(), m.getBasicProperties(), m.getPayload(),
            messageCount);
    return Optional.of(response);
}

From source file:org.apache.nifi.amqp.processors.TestChannel.java

License:Apache License

@Override
public void basicPublish(final String exchange, final String routingKey, boolean mandatory,
        final BasicProperties props, final byte[] body) throws IOException {
    if (this.corrupted) {
        throw new IOException("Channel is corrupted");
    }/*from   w w  w.j a v a  2s.  c  om*/

    if (exchange.equals("")) { // default exchange; routingKey corresponds to a queue.
        BlockingQueue<GetResponse> messages = this.getMessageQueue(routingKey);
        GetResponse response = new GetResponse(null, props, body, messages.size());
        messages.offer(response);
    } else {
        String rKey = this.exchangeToRoutingKeyMappings.get(exchange);

        if (rKey.equals(routingKey)) {
            List<String> queueNames = this.routingKeyToQueueMappings.get(routingKey);
            if (queueNames == null || queueNames.isEmpty()) {
                this.discard(exchange, routingKey, mandatory, props, body);
            } else {
                for (String queueName : queueNames) {
                    BlockingQueue<GetResponse> messages = this.getMessageQueue(queueName);
                    GetResponse response = new GetResponse(null, props, body, messages.size());
                    messages.offer(response);
                }
            }
        } else {
            this.discard(exchange, routingKey, mandatory, props, body);
        }

    }
}

From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java

License:Apache License

@Test
public void testWithConnectionFactoryCacheSize() throws IOException {
    com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(
            com.rabbitmq.client.ConnectionFactory.class);
    com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
    Channel mockChannel1 = mock(Channel.class);
    Channel mockChannel2 = mock(Channel.class);

    when(mockConnectionFactory.newConnection((ExecutorService) null)).thenReturn(mockConnection);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockConnection.createChannel()).thenReturn(mockChannel1).thenReturn(mockChannel2);

    when(mockChannel1.basicGet("foo", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel2.basicGet("bar", false)).thenReturn(new GetResponse(null, null, null, 1));
    when(mockChannel1.isOpen()).thenReturn(true);
    when(mockChannel2.isOpen()).thenReturn(true);

    CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
    ccf.setChannelCacheSize(2);//from w ww .j a  va2 s  .c  om

    Connection con = ccf.createConnection();

    Channel channel1 = con.createChannel(false);
    Channel channel2 = con.createChannel(false);

    channel1.basicGet("foo", true);
    channel2.basicGet("bar", true);

    channel1.close(); // should be ignored, and add last into channel cache.
    channel2.close(); // should be ignored, and add last into channel cache.

    Channel ch1 = con.createChannel(false); // remove first entry in cache
    // (channel1)
    Channel ch2 = con.createChannel(false); // remove first entry in cache
    // (channel2)

    assertNotSame(ch1, ch2);
    assertSame(ch1, channel1);
    assertSame(ch2, channel2);

    ch1.close();
    ch2.close();

    verify(mockConnection, times(2)).createChannel();

    con.close(); // should be ignored

    verify(mockConnection, never()).close();
    verify(mockChannel1, never()).close();
    verify(mockChannel2, never()).close();

}

From source file:org.springframework.integration.amqp.inbound.AmqpMessageSourceTests.java

License:Apache License

@Test
public void testAck() throws Exception {
    Channel channel = mock(Channel.class);
    willReturn(true).given(channel).isOpen();
    Envelope envelope = new Envelope(123L, false, "ex", "rk");
    BasicProperties props = new BasicProperties.Builder().build();
    GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0);
    willReturn(getResponse).given(channel).basicGet("foo", false);
    Connection connection = mock(Connection.class);
    willReturn(true).given(connection).isOpen();
    willReturn(channel).given(connection).createChannel();
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString());

    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    AmqpMessageSource source = new AmqpMessageSource(ccf, "foo");
    source.setRawMessageHeader(true);// www. j ava  2s  . com
    Message<?> received = source.receive();
    assertThat(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE))
            .isInstanceOf(org.springframework.amqp.core.Message.class);
    assertThat(received.getHeaders().get(IntegrationMessageHeaderAccessor.SOURCE_DATA))
            .isSameAs(received.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE));
    assertThat(received.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE)).isEqualTo("foo");
    // make sure channel is not cached
    org.springframework.amqp.rabbit.connection.Connection conn = ccf.createConnection();
    Channel notCached = conn.createChannel(false); // should not have been "closed"
    verify(connection, times(2)).createChannel();
    StaticMessageHeaderAccessor.getAcknowledgmentCallback(received).acknowledge(Status.ACCEPT);
    verify(channel).basicAck(123L, false);
    Channel cached = conn.createChannel(false); // should have been "closed"
    verify(connection, times(2)).createChannel();
    notCached.close();
    cached.close();
    ccf.destroy();
    verify(channel, times(2)).close();
    verify(connection).close(30000);
}

From source file:org.springframework.integration.amqp.inbound.AmqpMessageSourceTests.java

License:Apache License

private void testNackOrRequeue(boolean requeue) throws Exception {
    Channel channel = mock(Channel.class);
    willReturn(true).given(channel).isOpen();
    Envelope envelope = new Envelope(123L, false, "ex", "rk");
    BasicProperties props = new BasicProperties.Builder().build();
    GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0);
    willReturn(getResponse).given(channel).basicGet("foo", false);
    Connection connection = mock(Connection.class);
    willReturn(true).given(connection).isOpen();
    willReturn(channel).given(connection).createChannel();
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString());

    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    AmqpMessageSource source = new AmqpMessageSource(ccf, "foo");
    Message<?> received = source.receive();
    verify(connection).createChannel();/*w  w  w.jav  a 2s  .c  om*/
    StaticMessageHeaderAccessor.getAcknowledgmentCallback(received)
            .acknowledge(requeue ? Status.REQUEUE : Status.REJECT);
    verify(channel).basicReject(123L, requeue);
    verify(connection).createChannel();
    ccf.destroy();
    verify(channel).close();
    verify(connection).close(30000);
}

From source file:org.springframework.integration.amqp.inbound.AmqpMessageSourceTests.java

License:Apache License

@SuppressWarnings({ "unchecked" })
@Test/*from w  ww.j av a 2 s.  c  om*/
public void testBatch() throws Exception {
    SimpleBatchingStrategy bs = new SimpleBatchingStrategy(2, 10_000, 10_000L);
    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("text/plain");
    org.springframework.amqp.core.Message message = new org.springframework.amqp.core.Message(
            "test1".getBytes(), messageProperties);
    bs.addToBatch("foo", "bar", message);
    message = new org.springframework.amqp.core.Message("test2".getBytes(), messageProperties);
    MessageBatch batched = bs.addToBatch("foo", "bar", message);

    Channel channel = mock(Channel.class);
    willReturn(true).given(channel).isOpen();
    Envelope envelope = new Envelope(123L, false, "ex", "rk");
    BasicProperties props = new BasicProperties.Builder()
            .headers(batched.getMessage().getMessageProperties().getHeaders()).contentType("text/plain")
            .build();
    GetResponse getResponse = new GetResponse(envelope, props, batched.getMessage().getBody(), 0);
    willReturn(getResponse).given(channel).basicGet("foo", false);
    Connection connection = mock(Connection.class);
    willReturn(true).given(connection).isOpen();
    willReturn(channel).given(connection).createChannel();
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString());

    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    AmqpMessageSource source = new AmqpMessageSource(ccf, "foo");
    Message<?> received = source.receive();
    assertThat(received).isNotNull();
    assertThat(((List<String>) received.getPayload())).contains("test1", "test2");
}