Example usage for com.rabbitmq.client BasicProperties getReplyTo

List of usage examples for com.rabbitmq.client BasicProperties getReplyTo

Introduction

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

Prototype

public abstract String getReplyTo();

Source Link

Document

Retrieve the value in the replyTo field.

Usage

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

License:Open Source License

@Override
public final void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
        throws IOException {

    super.handleDelivery(consumerTag, envelope, properties, body);

    Message message = new Message(properties.getType(), body, properties.getCorrelationId(),
            new RbtDestination(RABBITMQ_DEFAULT_EXCHANGE_NAME, properties.getReplyTo()));

    message.setDestination(new RbtDestination(RABBITMQ_DEFAULT_EXCHANGE_NAME, envelope.getRoutingKey()));

    message.setContentType(properties.getContentType());

    // take retries count
    try {/* www .  j  a  v a 2  s .c  om*/
        if (properties.getHeaders() != null) {
            Object xretriesObject = properties.getHeaders().get("x-retries");
            if (xretriesObject != null) {
                int xretries = 0;
                if (xretriesObject instanceof Integer) {
                    xretries = (Integer) xretriesObject;
                } else if (xretriesObject instanceof String) {
                    xretries = Integer.parseInt((String) xretriesObject);
                } else {
                    LOGGER.error("Unknown object type of x-retries property.");
                }
                message.setRetries(xretries);
            }
        }
    } catch (NumberFormatException ex) {
        // not important
    }

    try {
        responseHandler.handleMessage(message);
        if (!autoack) {
            getChannel().basicAck(envelope.getDeliveryTag(), false);
        }
    } catch (ConsumeHandlerException ex) {
        if (!autoack) {
            getChannel().basicReject(envelope.getDeliveryTag(), true);
        }
        // nothing can do :(
    } catch (Throwable t) {
        if (!autoack) {
            getChannel().basicReject(envelope.getDeliveryTag(), true);
        }
        LOGGER.error("Error handling message.", t);
    }
}

From source file:pl.polsl.java.znaj.dawid.rabbit.RabbitInvoiceServer.java

@Override
public void run() {

    try {//w w  w  . j  a  v  a 2s  .c  om
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        BasicProperties props = delivery.getProperties();
        BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                .build();

        String message = new String(delivery.getBody());

        Gson g = new Gson();
        Type type = new TypeToken<HashMap<String, Object>>() {
        }.getType();

        Map<String, Object> invoiceModelProperties = g.fromJson(message, type);
        Object result = processInvoiceBasedOnEventType(invoiceModelProperties);

        String response = result != null ? g.toJson(result) : "false";

        channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

        channel.close();

    } catch (InterruptedException | ShutdownSignalException | ConsumerCancelledException | IOException
            | ParseException ex) {
        Logger.getLogger(RabbitInvoiceServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}