Introduce high-level API for flows composition (#3624)
* Introduce high-level API for flows composition
For better end-user experience and more smooth integration logic
decomposition and distribution introduce an `IntegrationFlows.from(IntegrationFlow)`
to let to start the current flow from existing one.
On the other hand introduce an `BaseIntegrationFlowDefinition.to(IntegrationFlow)`
to let to continue the flow logic in the other existing one.
This way we can extract some templating logic into separate `IntegrationFlow` definitions
allowing at the same time to decompose a complex flow definition into logical reusable parts
* * Add more tests
* * Fix Checkstyle violation
* Add `@SuppressWarnings("overloads")` to new `from(IntegrationFlow)` and existing `from(Publisher)`.
Technically it does not make sense since `PublisherIntegrationFlow` is not a `public` class
* * Add docs
* Fix language in JavaDocs according review
* Fix language in the docs after review
Co-authored-by: Gary Russell <grussell@vmware.com>
Co-authored-by: Gary Russell <grussell@vmware.com>
This commit is contained in:
@@ -137,6 +137,10 @@ public class ConsumerEndpointFactoryBean
|
||||
}
|
||||
}
|
||||
|
||||
public MessageHandler getHandler() {
|
||||
return this.handler;
|
||||
}
|
||||
|
||||
public void setInputChannel(MessageChannel inputChannel) {
|
||||
this.inputChannel = inputChannel;
|
||||
}
|
||||
|
||||
@@ -2917,6 +2917,18 @@ public abstract class BaseIntegrationFlowDefinition<B extends BaseIntegrationFlo
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish this flow with delegation to other {@link IntegrationFlow} instance.
|
||||
* @param other the {@link IntegrationFlow} to compose with.
|
||||
* @return The {@link IntegrationFlow} instance based on this definition.
|
||||
* @since 5.5.4
|
||||
*/
|
||||
public IntegrationFlow to(IntegrationFlow other) {
|
||||
MessageChannel otherFlowInputChannel = obtainInputChannelFromFlow(other);
|
||||
return channel(otherFlowInputChannel)
|
||||
.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represent an Integration Flow as a Reactive Streams {@link Publisher} bean.
|
||||
* @param <T> the expected {@code payload} type
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2019 the original author or authors.
|
||||
* Copyright 2016-2021 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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
|
||||
/**
|
||||
@@ -92,4 +94,13 @@ public interface IntegrationFlow {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a map of integration components managed by this flow (if any).
|
||||
* @return the map of integration components managed by this flow.
|
||||
* @since 5.5.4
|
||||
*/
|
||||
default Map<Object, String> getIntegrationComponents() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2020 the original author or authors.
|
||||
* Copyright 2016-2021 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.
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
@@ -79,6 +80,10 @@ public abstract class IntegrationFlowAdapter implements IntegrationFlow, Managea
|
||||
return this.targetIntegrationFlow.getInputChannel();
|
||||
}
|
||||
|
||||
@Override public Map<Object, String> getIntegrationComponents() {
|
||||
return this.targetIntegrationFlow.getIntegrationComponents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
assertTargetIntegrationFlow();
|
||||
|
||||
@@ -16,22 +16,29 @@
|
||||
|
||||
package org.springframework.integration.dsl;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.FluxMessageChannel;
|
||||
import org.springframework.integration.channel.PublishSubscribeChannel;
|
||||
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
import org.springframework.integration.dsl.support.FixedSubscriberChannelPrototype;
|
||||
import org.springframework.integration.dsl.support.MessageChannelReference;
|
||||
import org.springframework.integration.endpoint.AbstractMessageSource;
|
||||
import org.springframework.integration.endpoint.MessageProducerSupport;
|
||||
import org.springframework.integration.gateway.MessagingGatewaySupport;
|
||||
import org.springframework.integration.handler.AbstractMessageProducingHandler;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -328,12 +335,52 @@ public final class IntegrationFlows {
|
||||
* @param publisher the {@link Publisher} to subscribe to.
|
||||
* @return new {@link IntegrationFlowBuilder}.
|
||||
*/
|
||||
@SuppressWarnings("overloads")
|
||||
public static IntegrationFlowBuilder from(Publisher<? extends Message<?>> publisher) {
|
||||
FluxMessageChannel reactiveChannel = new FluxMessageChannel();
|
||||
reactiveChannel.subscribeTo(publisher);
|
||||
return from((MessageChannel) reactiveChannel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the flow with a composition from the {@link IntegrationFlow}.
|
||||
* @param other the {@link IntegrationFlow} from which to compose.
|
||||
* @return new {@link IntegrationFlowBuilder}.
|
||||
* @since 5.5.4
|
||||
*/
|
||||
@SuppressWarnings("overloads")
|
||||
public static IntegrationFlowBuilder from(IntegrationFlow other) {
|
||||
Map<Object, String> integrationComponents = other.getIntegrationComponents();
|
||||
Assert.notNull(integrationComponents, () ->
|
||||
"The provided integration flow to compose from '" + other +
|
||||
"' must be declared as a bean in the application context");
|
||||
Object lastIntegrationComponentFromOther =
|
||||
integrationComponents.keySet().stream().reduce((prev, next) -> next).orElse(null);
|
||||
if (lastIntegrationComponentFromOther instanceof MessageChannel) {
|
||||
return from((MessageChannel) lastIntegrationComponentFromOther);
|
||||
}
|
||||
else if (lastIntegrationComponentFromOther instanceof ConsumerEndpointFactoryBean) {
|
||||
MessageHandler handler = ((ConsumerEndpointFactoryBean) lastIntegrationComponentFromOther).getHandler();
|
||||
handler = extractProxyTarget(handler);
|
||||
if (handler instanceof AbstractMessageProducingHandler) {
|
||||
return buildFlowFromOutputChannel((AbstractMessageProducingHandler) handler);
|
||||
}
|
||||
lastIntegrationComponentFromOther = handler; // for the exception message below
|
||||
}
|
||||
throw new BeanCreationException("The 'IntegrationFlow' to start from must end with " +
|
||||
"a 'MessageChannel' or reply-producing endpoint to let the result from that flow to be " +
|
||||
"processed in this instance. The provided flow ends with: " + lastIntegrationComponentFromOther);
|
||||
}
|
||||
|
||||
private static IntegrationFlowBuilder buildFlowFromOutputChannel(AbstractMessageProducingHandler handler) {
|
||||
MessageChannel outputChannel = handler.getOutputChannel();
|
||||
if (outputChannel == null) {
|
||||
outputChannel = new PublishSubscribeChannel();
|
||||
handler.setOutputChannel(outputChannel);
|
||||
}
|
||||
return from(outputChannel);
|
||||
}
|
||||
|
||||
private static IntegrationFlowBuilder from(MessagingGatewaySupport inboundGateway,
|
||||
@Nullable IntegrationFlowBuilder integrationFlowBuilderArg) {
|
||||
|
||||
@@ -360,6 +407,20 @@ public final class IntegrationFlows {
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> T extractProxyTarget(T target) {
|
||||
if (!(target instanceof Advised)) {
|
||||
return target;
|
||||
}
|
||||
Advised advised = (Advised) target;
|
||||
try {
|
||||
return (T) extractProxyTarget(advised.getTargetSource().getTarget());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new BeanCreationException("Could not extract target", e);
|
||||
}
|
||||
}
|
||||
|
||||
private IntegrationFlows() {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2016-2020 the original author or authors.
|
||||
* Copyright 2016-2021 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.
|
||||
@@ -321,6 +321,7 @@ public class IntegrationFlowBeanPostProcessor
|
||||
new NameMatchMethodPointcutAdvisor(new IntegrationFlowLifecycleAdvice(target));
|
||||
integrationFlowAdvice.setMappedNames(
|
||||
"getInputChannel",
|
||||
"getIntegrationComponents",
|
||||
"start",
|
||||
"stop",
|
||||
"isRunning",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2018-2019 the original author or authors.
|
||||
* Copyright 2018-2021 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.
|
||||
@@ -75,6 +75,12 @@ class IntegrationFlowLifecycleAdvice implements MethodInterceptor {
|
||||
result = this.delegate.getInputChannel();
|
||||
}
|
||||
}
|
||||
else if ("getIntegrationComponents".equals(method)) {
|
||||
result = invocation.proceed();
|
||||
if (result == null) {
|
||||
result = this.delegate.getIntegrationComponents();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (target instanceof SmartLifecycle) {
|
||||
result = invocation.proceed();
|
||||
|
||||
Reference in New Issue
Block a user