Example usage for org.springframework.amqp.core ReceiveAndReplyCallback getClass

List of usage examples for org.springframework.amqp.core ReceiveAndReplyCallback getClass

Introduction

In this page you can find the example usage for org.springframework.amqp.core ReceiveAndReplyCallback getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.kurento.rabbitmq.RabbitTemplate.java

@SuppressWarnings("unchecked")
private <R, S> boolean doReceiveAndReply(final String queueName, final ReceiveAndReplyCallback<R, S> callback,
        final ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException {
    return this.execute(new ChannelCallback<Boolean>() {

        @Override// w w w  .  j a  va  2  s .c  o  m
        public Boolean doInRabbit(Channel channel) throws Exception {
            boolean channelTransacted = RabbitTemplate.this.isChannelTransacted();

            GetResponse response = channel.basicGet(queueName, !channelTransacted);
            // Response can be null in the case that there is no message on
            // the queue.
            if (response != null) {
                long deliveryTag = response.getEnvelope().getDeliveryTag();
                boolean channelLocallyTransacted = RabbitTemplate.this.isChannelLocallyTransacted(channel);

                if (channelLocallyTransacted) {
                    channel.basicAck(deliveryTag, false);
                } else if (channelTransacted) {
                    // Not locally transacted but it is transacted so it
                    // could be synchronized with an external transaction
                    ConnectionFactoryUtils.registerDeliveryTag(RabbitTemplate.this.getConnectionFactory(),
                            channel, deliveryTag);
                }

                Message receiveMessage = RabbitTemplate.this.buildMessageFromResponse(response);

                Object receive = receiveMessage;
                if (!(ReceiveAndReplyMessageCallback.class.isAssignableFrom(callback.getClass()))) {
                    receive = RabbitTemplate.this.getRequiredMessageConverter().fromMessage(receiveMessage);
                }

                S reply;
                try {
                    reply = callback.handle((R) receive);
                } catch (ClassCastException e) {
                    StackTraceElement[] trace = e.getStackTrace();
                    if (trace[0].getMethodName().equals("handle")
                            && trace[1].getFileName().equals("RabbitTemplate.java")) {
                        throw new IllegalArgumentException("ReceiveAndReplyCallback '" + callback
                                + "' can't handle received object '" + receive + "'", e);
                    } else {
                        throw e;
                    }
                }

                if (reply != null) {
                    Address replyTo = replyToAddressCallback.getReplyToAddress(receiveMessage, reply);

                    Message replyMessage = RabbitTemplate.this.convertMessageIfNecessary(reply);

                    MessageProperties receiveMessageProperties = receiveMessage.getMessageProperties();
                    MessageProperties replyMessageProperties = replyMessage.getMessageProperties();

                    Object correlation = RabbitTemplate.this.correlationKey == null
                            ? receiveMessageProperties.getCorrelationId()
                            : receiveMessageProperties.getHeaders().get(RabbitTemplate.this.correlationKey);

                    if (RabbitTemplate.this.correlationKey == null || correlation == null) {
                        // using standard correlationId property
                        if (correlation == null) {
                            String messageId = receiveMessageProperties.getMessageId();
                            if (messageId != null) {
                                correlation = messageId.getBytes(RabbitTemplate.this.encoding);
                            }
                        }
                        replyMessageProperties.setCorrelationId((byte[]) correlation);
                    } else {
                        replyMessageProperties.setHeader(RabbitTemplate.this.correlationKey, correlation);
                    }

                    // 'doSend()' takes care about 'channel.txCommit()'.
                    RabbitTemplate.this.doSend(channel, replyTo.getExchangeName(), replyTo.getRoutingKey(),
                            replyMessage, null);
                } else if (channelLocallyTransacted) {
                    channel.txCommit();
                }

                return true;
            }
            return false;
        }
    });
}