Example usage for org.springframework.amqp ImmediateAcknowledgeAmqpException ImmediateAcknowledgeAmqpException

List of usage examples for org.springframework.amqp ImmediateAcknowledgeAmqpException ImmediateAcknowledgeAmqpException

Introduction

In this page you can find the example usage for org.springframework.amqp ImmediateAcknowledgeAmqpException ImmediateAcknowledgeAmqpException.

Prototype

public ImmediateAcknowledgeAmqpException(String message, Throwable cause) 

Source Link

Usage

From source file:org.springframework.amqp.rabbit.config.StatefulRetryOperationsInterceptorFactoryBean.java

public StatefulRetryOperationsInterceptor getObject() {

    StatefulRetryOperationsInterceptor retryInterceptor = new StatefulRetryOperationsInterceptor();
    RetryOperations retryTemplate = getRetryOperations();
    if (retryTemplate == null) {
        retryTemplate = new RetryTemplate();
    }/*  ww w.  j a  va  2 s.  c o  m*/
    retryInterceptor.setRetryOperations(retryTemplate);

    retryInterceptor.setNewItemIdentifier(new NewMethodArgumentsIdentifier() {
        public boolean isNew(Object[] args) {
            Message message = (Message) args[1];
            if (newMessageIdentifier == null) {
                return !message.getMessageProperties().isRedelivered();
            } else {
                return newMessageIdentifier.isNew(message);
            }
        }
    });

    final MessageRecoverer messageRecoverer = getMessageRecoverer();
    retryInterceptor.setRecoverer(new MethodInvocationRecoverer<Void>() {
        public Void recover(Object[] args, Throwable cause) {
            Message message = (Message) args[1];
            if (messageRecoverer == null) {
                logger.warn("Message dropped on recovery: " + message, cause);
            } else {
                messageRecoverer.recover(message, cause);
            }
            // This is actually a normal outcome. It means the recovery was successful, but we don't want to consume
            // any more messages until the acks and commits are sent for this (problematic) message...
            throw new ImmediateAcknowledgeAmqpException(
                    "Recovered message forces ack (if ack mode requires it): " + message, cause);
        }
    });

    retryInterceptor.setKeyGenerator(new MethodArgumentsKeyGenerator() {
        public Object getKey(Object[] args) {
            Message message = (Message) args[1];
            if (messageKeyGenerator == null) {
                String messageId = message.getMessageProperties().getMessageId();
                if (messageId == null) {
                    throw new FatalListenerExecutionException(
                            "Illegal null id in message. Failed to manage retry for message: " + message);
                }
                return messageId;
            } else {
                return messageKeyGenerator.getKey(message);
            }
        }
    });

    return retryInterceptor;

}