Example usage for com.rabbitmq.client GetResponse getProps

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

Introduction

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

Prototype

public BasicProperties getProps() 

Source Link

Document

Get the BasicProperties included in this response

Usage

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

License:Apache License

@Override
protected AbstractIntegrationMessageBuilder<Object> doReceive() {
    Connection connection = this.connectionFactory.createConnection(); // NOSONAR - RabbitUtils
    Channel channel = connection.createChannel(this.transacted);
    try {/*from  w  ww  .  j a  v  a 2  s. co  m*/
        GetResponse resp = channel.basicGet(this.queue, false);
        if (resp == null) {
            RabbitUtils.closeChannel(channel);
            RabbitUtils.closeConnection(connection);
            return null;
        }
        AcknowledgmentCallback callback = this.ackCallbackFactory
                .createCallback(new AmqpAckInfo(connection, channel, this.transacted, resp));
        MessageProperties messageProperties = this.propertiesConverter.toMessageProperties(resp.getProps(),
                resp.getEnvelope(), StandardCharsets.UTF_8.name());
        messageProperties.setConsumerQueue(this.queue);
        Map<String, Object> headers = this.headerMapper.toHeadersFromRequest(messageProperties);
        org.springframework.amqp.core.Message amqpMessage = new org.springframework.amqp.core.Message(
                resp.getBody(), messageProperties);
        Object payload;
        if (this.batchingStrategy.canDebatch(messageProperties)) {
            List<Object> payloads = new ArrayList<>();
            this.batchingStrategy.deBatch(amqpMessage,
                    fragment -> payloads.add(this.messageConverter.fromMessage(fragment)));
            payload = payloads;
        } else {
            payload = this.messageConverter.fromMessage(amqpMessage);
        }
        AbstractIntegrationMessageBuilder<Object> builder = getMessageBuilderFactory().withPayload(payload)
                .copyHeaders(headers)
                .setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback);
        if (this.rawMessageHeader) {
            builder.setHeader(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, amqpMessage);
            builder.setHeader(IntegrationMessageHeaderAccessor.SOURCE_DATA, amqpMessage);
        }
        return builder;
    } catch (IOException e) {
        RabbitUtils.closeChannel(channel);
        RabbitUtils.closeConnection(connection);
        throw RabbitExceptionTranslator.convertRabbitAccessException(e);
    }
}

From source file:org.trpr.mule.transport.rabbitmq.RabbitMessageRequester.java

License:Apache License

/**
 * Abstract method implementation. Tries to get a message from the queue identified by the endpoint until a timeout occurs.
 * @see org.mule.transport.AbstractMessageRequester#doRequest(long)
 *//*from  ww w .  j a v  a 2 s  . c o  m*/
protected MuleMessage doRequest(long timeout) throws Exception {
    try {
        String queue = EndpointUtils.getQueue(endpoint);
        if (queue == null) {
            throw new IllegalArgumentException(RabbitMessages.noQueueDefined(endpoint).getMessage());
        }
        long time = System.currentTimeMillis() + timeout;
        long count = System.currentTimeMillis();
        while (count < time) {
            GetResponse response = rpcClient.getChannel().basicGet(queue, false);
            if (response == null) {
                Thread.sleep(50);
                count += 50;
            } else {
                AMQP.BasicProperties props = response.getProps();
                byte[] body = response.getBody();
                MessageAdapter adapter = connector.getMessageAdapter(new Object[] { body, props });
                return new DefaultMuleMessage(adapter);
            }
        }
        return null;
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}

From source file:pl.nask.hsn2.bus.rabbitmq.endpoint.RbtMulticastEndPoint.java

License:Open Source License

@Override
public final List<Message> sendMulticast(Message message, String[] servicesNames) throws BusException {

    if (servicesNames == null)
        throw new IllegalArgumentException("No multicast destinations provided.");

    BasicProperties.Builder propertiesBuilder = new BasicProperties.Builder().contentType(DEFAULT_CONTENT_TYPE)
            .type(message.getType()).replyTo(responseQueue);

    // setup correct correlation id if provided
    if (message.getCorrelationId() != null && !"".equals(message.getCorrelationId())) {
        propertiesBuilder.correlationId(message.getCorrelationId());
    }/*from  ww w .j a  va2s .c o  m*/

    message.setDestination(new RbtDestination(responseQueue));
    try {
        for (String destination : servicesNames) {
            channel.basicPublish("", destination, propertiesBuilder.build(), message.getBody());
        }

        long currTime = System.currentTimeMillis();

        GetResponse response = null;
        List<Message> responseList = new LinkedList<Message>();

        int i = 0;
        while (System.currentTimeMillis() < currTime + ((long) timeout) * 1000 && i < servicesNames.length) {
            response = channel.basicGet(responseQueue, true);
            if (response != null) {
                responseList.add(new Message(response.getProps().getType(), response.getBody(),
                        response.getProps().getCorrelationId(),
                        new RbtDestination(response.getProps().getReplyTo())));
                i++;
            }
        }

        if (i < servicesNames.length) {
            LOGGER.debug("There are no responses from all services (expected={}, got={}).",
                    servicesNames.length, i);
        }
        return responseList;
    } catch (IOException e) {
        throw new BusException("Cannot send the message!", e);
    }
}

From source file:pl.nask.hsn2.bus.rabbitmq.endpoint.RbtRequestResponseEndPoint.java

License:Open Source License

@Override
public final Message sendAndGet(Message message) throws BusException {
    BasicProperties.Builder propertiesBuilder = new BasicProperties.Builder().contentType(DEFAULT_CONTENT_TYPE)
            .type(message.getType()).replyTo(responseQueue);

    // setup correct correlation id if provided
    if (message.getCorrelationId() != null && !"".equals(message.getCorrelationId())) {
        propertiesBuilder.correlationId(message.getCorrelationId());
    }//from ww w . j ava2  s .c o m

    String destinationRoutingKey = message.getDestination().getService();
    String destinationExchange = ((RbtDestination) (message.getDestination())).getExchange();
    message.setReplyTo(new RbtDestination(responseQueue));

    try {
        channel.basicPublish(destinationExchange, destinationRoutingKey, propertiesBuilder.build(),
                message.getBody());

        long currTime = System.currentTimeMillis();

        GetResponse response = null;

        while (response == null && System.currentTimeMillis() < currTime + ((long) timeout) * 1000) {
            response = channel.basicGet(responseQueue, true);
        }

        if (response == null) {
            throw new TimeoutException("Cannot get message, timeout after " + timeout + " seconds!");
        }

        return new Message(response.getProps().getType(), response.getBody(),
                response.getProps().getCorrelationId(), new RbtDestination(response.getProps().getReplyTo()));
    } catch (IOException e) {
        throw new BusException("Cannot sent message!", e);
    }
}

From source file:wunderrabbit.RabbitConnection.java

License:Apache License

@Override
public Message receive(Endpoint endpoint, Map<ReceiveOption, Object> options) throws Exception {
    Options<ReceiveOption> opts = new Options<>(options);

    Channel channel = null;/*from  w w  w .  ja  v  a  2  s.co  m*/
    RabbitMessage message = null;
    try {
        channel = this.connection.createChannel();
        declareQueue(channel, endpoint);
        //TODO: this auto-acks
        //TODO: what about timeouts?
        GetResponse response = channel.basicGet((String) endpoint.implementation(), true);
        if (response != null) {
            message = new RabbitMessage(response.getBody(), response.getProps(), endpoint);
        }
    } finally {
        if (channel != null) {
            channel.close();
        }
    }

    return message;
}