Example usage for org.springframework.amqp.rabbit.retry MessageRecoverer recover

List of usage examples for org.springframework.amqp.rabbit.retry MessageRecoverer recover

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.retry MessageRecoverer recover.

Prototype

void recover(Message message, Throwable cause);

Source Link

Document

Callback for message that was consumed but failed all retry attempts.

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();
    }/*from  ww w  .ja  v a2  s .co  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;

}

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

public RetryOperationsInterceptor getObject() {

    RetryOperationsInterceptor retryInterceptor = new RetryOperationsInterceptor();
    RetryOperations retryTemplate = getRetryOperations();
    if (retryTemplate == null) {
        retryTemplate = new RetryTemplate();
    }//from   w  w  w. ja  v a 2s  .  com
    retryInterceptor.setRetryOperations(retryTemplate);

    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);
            }
            return null;
        }
    });

    return retryInterceptor;

}