GH-3219: Add HandleMessageAdviceAdapter (#3234)

* GH-3219: Add `HandleMessageAdviceAdapter`

Fixes https://github.com/spring-projects/spring-integration/issues/3219

* * Fix language in doc

Co-Authored-By: Gary Russell <grussell@pivotal.io>

Co-authored-by: Gary Russell <grussell@pivotal.io>
This commit is contained in:
Artem Bilan
2020-04-01 11:10:33 -04:00
committed by GitHub
parent 7433e41036
commit 76e79012fe
5 changed files with 88 additions and 2 deletions

View File

@@ -22,12 +22,14 @@ import java.util.List;
import java.util.function.BiFunction;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.reactivestreams.Publisher;
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.handler.advice.HandleMessageAdviceAdapter;
import org.springframework.integration.handler.advice.ReactiveRequestHandlerAdvice;
import org.springframework.integration.router.AbstractMessageRouter;
import org.springframework.integration.scheduling.PollerMetadata;
@@ -101,6 +103,21 @@ public abstract class ConsumerEndpointSpec<S extends ConsumerEndpointSpec<S, H>,
return _this();
}
/**
* Configure a list of {@link MethodInterceptor} objects to be applied, in nested order, to the
* endpoint's handler. The advice objects are applied to the {@code handleMessage()} method
* and therefore to the whole sub-flow afterwards.
* @param interceptors the advice chain.
* @return the endpoint spec.
* @since 5.3
*/
public S handleMessageAdvice(MethodInterceptor... interceptors) {
for (MethodInterceptor interceptor: interceptors) {
advice(new HandleMessageAdviceAdapter(interceptor));
}
return _this();
}
/**
* Configure a list of {@link Advice} objects to be applied, in nested order, to the
* endpoint's handler. The advice objects are applied only to the handler.

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.handler.advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.Assert;
/**
* A {@link HandleMessageAdvice} implementation with a plain delegation
* to the provided {@link MethodInterceptor}.
* <p> This advice should be used for consumer endpoints to proxy exactly
* a {@link org.springframework.messaging.MessageHandler#handleMessage} and the whole-subflow therefore;
* unlike regular proxying which is applied only for the
* {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}.
*
* @author Artem Bilan
*
* @since 5.3
*/
public class HandleMessageAdviceAdapter implements HandleMessageAdvice {
private final MethodInterceptor delegate;
public HandleMessageAdviceAdapter(MethodInterceptor delegate) {
Assert.notNull(delegate, "The 'delegate' must not be null");
this.delegate = delegate;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return this.delegate.invoke(invocation);
}
}