Example usage for org.springframework.amqp.rabbit.connection ConnectionFactoryUtils registerDeliveryTag

List of usage examples for org.springframework.amqp.rabbit.connection ConnectionFactoryUtils registerDeliveryTag

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection ConnectionFactoryUtils registerDeliveryTag.

Prototype

public static void registerDeliveryTag(ConnectionFactory connectionFactory, Channel channel, Long tag) 

Source Link

Usage

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

@Override
public Message receive(final String queueName) {
    return execute(new ChannelCallback<Message>() {

        @Override//from  www. j av a 2s .c  o  m
        public Message doInRabbit(Channel channel) throws IOException {
            GetResponse response = channel.basicGet(queueName, !isChannelTransacted());
            // Response can be null is the case that there is no message on
            // the queue.
            if (response != null) {
                long deliveryTag = response.getEnvelope().getDeliveryTag();
                if (isChannelLocallyTransacted(channel)) {
                    channel.basicAck(deliveryTag, false);
                    channel.txCommit();
                } else if (isChannelTransacted()) {
                    // Not locally transacted but it is transacted so it
                    // could be synchronized with an external transaction
                    ConnectionFactoryUtils.registerDeliveryTag(getConnectionFactory(), channel, deliveryTag);
                }

                return RabbitTemplate.this.buildMessageFromResponse(response);
            }
            return null;
        }
    });
}

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/*ww 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;
        }
    });
}

From source file:org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.java

/**
 * Perform a commit or message acknowledgement, as appropriate.
 * //w  ww .j a  v  a  2  s  .c o  m
 * @param locallyTransacted
 * @throws IOException
 */
public boolean commitIfNecessary(boolean locallyTransacted) throws IOException {

    if (deliveryTags.isEmpty()) {
        return false;
    }

    try {

        boolean ackRequired = !acknowledgeMode.isAutoAck() && !acknowledgeMode.isManual();

        if (ackRequired) {

            if (transactional && !locallyTransacted) {

                // Not locally transacted but it is transacted so it
                // could be synchronized with an external transaction
                for (Long deliveryTag : deliveryTags) {
                    ConnectionFactoryUtils.registerDeliveryTag(connectionFactory, channel, deliveryTag);
                }

            } else {

                if (!deliveryTags.isEmpty()) {
                    long deliveryTag = new ArrayList<Long>(deliveryTags).get(deliveryTags.size() - 1);
                    channel.basicAck(deliveryTag, true);
                }

            }
        }

        if (locallyTransacted) {
            // For manual acks we still need to commit
            RabbitUtils.commitIfNecessary(channel);
        }

    } finally {
        deliveryTags.clear();
        retryDeliveryTags.clear();
    }

    return true;

}