Example usage for org.springframework.amqp.rabbit.connection RabbitUtils rollbackIfNecessary

List of usage examples for org.springframework.amqp.rabbit.connection RabbitUtils rollbackIfNecessary

Introduction

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

Prototype

public static void rollbackIfNecessary(Channel channel) 

Source Link

Usage

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

public void rollbackAll() {
    for (Channel channel : this.channels) {
        if (logger.isDebugEnabled()) {
            logger.debug("Rolling back messages to channel: " + channel);
        }//from   ww w. java 2 s  . c  om
        RabbitUtils.rollbackIfNecessary(channel);
        if (deliveryTags.containsKey(channel)) {
            for (Long deliveryTag : deliveryTags.get(channel)) {
                try {
                    channel.basicReject(deliveryTag, true);
                } catch (IOException ex) {
                    throw new AmqpIOException(ex);
                }
            }
            // Need to commit the reject (=nack)
            RabbitUtils.commitIfNecessary(channel);
        }
    }
}

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

/**
 * Perform a rollback, handling rollback exceptions properly.
 * // w  ww  .ja  v  a2s  . c om
 * @param ex
 *            the thrown application exception or error
 * @throws Exception
 *             in case of a rollback error
 */
public void rollbackOnExceptionIfNecessary(Throwable ex) throws Exception {

    boolean ackRequired = !acknowledgeMode.isAutoAck() && !acknowledgeMode.isManual();
    try {
        if (transactional) {
            if (logger.isDebugEnabled()) {
                logger.debug("Initiating transaction rollback on application exception: " + ex);
            }
            RabbitUtils.rollbackIfNecessary(channel);
        }
        if (ackRequired) {
            if (logger.isDebugEnabled()) {
                logger.debug("Rejecting messages");
            }
            boolean shouldRequeue = this.defaultRequeuRejected;
            Throwable t = ex;
            while (shouldRequeue && t != null) {
                if (t instanceof AmqpRejectAndDontRequeueException) {
                    shouldRequeue = false;
                }
                t = t.getCause();
            }
            for (Long deliveryTag : deliveryTags) {
                // With newer RabbitMQ brokers could use basicNack here...
                channel.basicReject(deliveryTag, shouldRequeue);
            }
            if (transactional) {
                // Need to commit the reject (=nack)
                RabbitUtils.commitIfNecessary(channel);
            }
        }
    } catch (Exception e) {
        logger.error("Application exception overridden by rollback exception", ex);
        throw e;
    } finally {
        deliveryTags.clear();
        retryDeliveryTags.clear();
    }
}