如何通过DSL组成Spring Integration流

时间:2018-09-25 16:02:42

标签: spring-integration spring-integration-dsl

我试图弄清楚如何将spring集成流拆分为多个子流并将它们组合在一起。最终,我将尝试设计一种模式,在该模式中,我可以创建子流模块,这些子流模块可以拼凑在一起以用于常见的集成方法。

此测试用例代表尝试(但失败)使用DSL IntegrationFlow API将子流连接在一起的最小示例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ComposedIntegrationFlowTest.class })
@SpringIntegrationTest
@EnableIntegration
public class ComposedIntegrationFlowTest {

    @Test
    public void test() {
        MessageChannel beginningChannel = MessageChannels.direct("beginning").get();

        IntegrationFlow middleFlow = f -> f
            .transform("From middleFlow: "::concat)
            .transform(String.class, String::toUpperCase);

        IntegrationFlow endFlow = f -> f
            .handle((p, h) -> "From endFlow: " + p);

        StandardIntegrationFlow composedFlow = IntegrationFlows
            .from(beginningChannel)
            .gateway(middleFlow)
            .gateway(endFlow)
            .get();

        composedFlow.start();

        beginningChannel.send(MessageBuilder.withPayload("hello!").build());
    }
}

尝试以上操作,我得到:

org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'beginning'.; nested exception is org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers, failedMessage=GenericMessage [payload=hello!, headers={id=2b1de253-a822-42ba-cd85-009b83a644eb, timestamp=1537890950879}], failedMessage=GenericMessage [payload=hello!, headers={id=2b1de253-a822-42ba-cd85-009b83a644eb, timestamp=1537890950879}]

如何将这些子流组合在一起?是否有更好的API尝试进行这种组合?集成测试的结构是否正确?

2 个答案:

答案 0 :(得分:0)

  

composedFlow.start();

那还不够;您需要向IntegrationFlowContext注册动态流,以便所有支持bean都已与应用程序上下文一起注册。

答案 1 :(得分:0)

Autowire IntegrationFlowContext ...

[2020-04-10 11:48:26] [info] Manager: list: Listing contexts for virtual host 'localhost'

[2020-04-10 11:48:26] [info] Manager: install: Installing web application '/api#v1' from 'file:/home/kos/RPServers/available_webapps/reaper-webapp.war'

[2020-04-10 11:48:26] [info] **Deploying web application archive [/var/lib/tomcat8/webapps/reaper-app/api#v1.war]**

[2020-04-10 11:48:27] [info] 2020/04/10 11:48:27.897 - #2 - 4973/4193 - MemoryShield:  totalMemory = 3871.45 [MB] resMemory = 1311.47 [MB] shrMemory = 63.6523 [MB]

[2020-04-10 11:48:28] [info] At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.

[2020-04-10 11:48:28] [info] **Deployment of web application archive [/var/lib/tomcat8/webapps/reaper-app/api#v1.war] has finished in [2,345] ms**

然后注册您的流程... ...

@Autowired
IntegrationFlowContext integrationFlowContext;