Example usage for org.springframework.integration IntegrationMessageHeaderAccessor DUPLICATE_MESSAGE

List of usage examples for org.springframework.integration IntegrationMessageHeaderAccessor DUPLICATE_MESSAGE

Introduction

In this page you can find the example usage for org.springframework.integration IntegrationMessageHeaderAccessor DUPLICATE_MESSAGE.

Prototype

String DUPLICATE_MESSAGE

To view the source code for org.springframework.integration IntegrationMessageHeaderAccessor DUPLICATE_MESSAGE.

Click Source Link

Usage

From source file:org.springframework.integration.handler.advice.IdempotentReceiverInterceptor.java

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    Object invocationThis = invocation.getThis();
    Object[] arguments = invocation.getArguments();
    boolean isMessageHandler = invocationThis != null && invocationThis instanceof MessageHandler;
    boolean isMessageMethod = method.getName().equals("handleMessage")
            && (arguments.length == 1 && arguments[0] instanceof Message);
    if (!isMessageHandler || !isMessageMethod) {
        if (logger.isWarnEnabled()) {
            String clazzName = invocationThis == null ? method.getDeclaringClass().getName()
                    : invocationThis.getClass().getName();
            logger.warn("This advice " + this.getClass().getName()
                    + " can only be used for MessageHandlers; an attempt to advise method '" + method.getName()
                    + "' in '" + clazzName + "' is ignored");
        }/*from w  ww  . java2  s . c  o m*/
        return invocation.proceed();
    }

    Message<?> message = (Message<?>) arguments[0];
    boolean accept = this.messageSelector.accept(message);
    if (!accept) {
        boolean discarded = false;
        if (this.discardChannel != null) {
            this.messagingTemplate.send(this.discardChannel, message);
            discarded = true;
        }
        if (this.throwExceptionOnRejection) {
            throw new MessageRejectedException(message,
                    "IdempotentReceiver '" + this + "' rejected duplicate Message: " + message);
        }

        if (!discarded) {
            arguments[0] = getMessageBuilderFactory().fromMessage(message)
                    .setHeader(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, true).build();
        } else {
            return null;
        }
    }
    return invocation.proceed();
}