GH-3204: Add DSL intercept() operator

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

* Add an `intercept(ChannelInterceptor...)` method into `BaseIntegrationFlowDefinition` 
to register one or more channel interceptors at the current flow position.
* refactor to reuse `InterceptableChannel` creation from `wireTap`
* document the new operator
This commit is contained in:
Artem Bilan
2020-03-05 16:15:52 -05:00
committed by GitHub
parent c84d264294
commit 1511dd8748
4 changed files with 133 additions and 8 deletions

View File

@@ -90,6 +90,7 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.InterceptableChannel;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -109,6 +110,7 @@ import reactor.util.function.Tuple2;
* @author Artem Bilan
* @author Gary Russell
* @author Gabriele Del Prete
* @author Tim Feuerbach
*
* @since 5.2.1
*
@@ -179,6 +181,24 @@ public abstract class BaseIntegrationFlowDefinition<B extends BaseIntegrationFlo
return this.currentMessageChannel;
}
/**
* Return the current channel if it is an {@link InterceptableChannel}, otherwise register a new implicit
* {@link DirectChannel} in the flow and return that one.
* @return the current channel after the operation
*/
protected InterceptableChannel currentInterceptableChannel() {
MessageChannel currentChannel = getCurrentMessageChannel();
if (currentChannel instanceof InterceptableChannel) {
return (InterceptableChannel) currentChannel;
}
else {
DirectChannel newCurrentChannel = new DirectChannel();
channel(newCurrentChannel);
setImplicitChannel(true);
return newCurrentChannel;
}
}
protected void setImplicitChannel(boolean implicitChannel) {
this.implicitChannel = implicitChannel;
}
@@ -488,14 +508,9 @@ public abstract class BaseIntegrationFlowDefinition<B extends BaseIntegrationFlo
*/
public B wireTap(WireTapSpec wireTapSpec) {
WireTap interceptor = wireTapSpec.get();
MessageChannel currentChannel = getCurrentMessageChannel();
if (!(currentChannel instanceof InterceptableChannel)) {
currentChannel = new DirectChannel();
channel(currentChannel);
setImplicitChannel(true);
}
InterceptableChannel currentChannel = currentInterceptableChannel();
addComponent(wireTapSpec);
((InterceptableChannel) currentChannel).addInterceptor(interceptor);
currentChannel.addInterceptor(interceptor);
return _this();
}
@@ -2829,6 +2844,26 @@ public abstract class BaseIntegrationFlowDefinition<B extends BaseIntegrationFlo
return handle(new ServiceActivatingHandler(triggerAction, "trigger"), endpointConfigurer);
}
/**
* Add one or more {@link ChannelInterceptor} implementations
* to the current {@link #currentMessageChannel}, in the given order, after any interceptors already registered.
* @param interceptorArray one or more {@link ChannelInterceptor}s.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @throws IllegalArgumentException if one or more null arguments are provided
* @since 5.3
*/
public B intercept(ChannelInterceptor... interceptorArray) {
Assert.notNull(interceptorArray, "'interceptorArray' must not be null");
Assert.noNullElements(interceptorArray, "'interceptorArray' must not contain null elements");
InterceptableChannel currentChannel = currentInterceptableChannel();
for (ChannelInterceptor interceptor : interceptorArray) {
currentChannel.addInterceptor(interceptor);
}
return _this();
}
/**
* Populate a {@link FluxMessageChannel} to start a reactive processing for upstream data,
* wrap it to a {@link Flux}, apply provided {@link Function} via {@link Flux#transform(Function)}