在Spring Integration DSL中将子流配置为PublishSubscribeChannel的bean

时间:2017-08-25 12:56:53

标签: spring spring-integration spring-integration-dsl

根据指南的Sub Flows support部分,人们希望能够非常简单地配置子流:使用DSL工厂或lambda。

// This is modified example from the guide.

@Bean
public IntegrationFlow subscribersFlow() {
    return flow -> flow
            .publishSubscribeChannel(Executors.newCachedThreadPool(), s -> s
                    .subscribe(f -> f
                            .<Integer>handle((p, h) -> p / 2)
                            .channel(c -> c.queue("subscriber1Results")))
                    .subscribe(
                            // this.subflow1()
                            this.subflow2()

                    ))
            .<Integer>handle((p, h) -> p * 3)
            .channel(c -> c.queue("subscriber3Results"));
}

// Attempt 1:
// Just a copy paste of the logic from above.
// Does not work, java.lang.UnsupportedOperationException

@Bean
public IntegrationFlow subflow1() {
    return f -> f
            .<Integer>handle((p, h) -> p * 2)
            .channel(c -> c.queue("subscriber2Results"));
}

// Attempt 2:
// Using DSL factories.
// Does not work, java.lang.UnsupportedOperationException

@Bean
public IntegrationFlow subflow2() {
    return IntegrationFlows.from(MessageChannels.direct())
            .<Integer>handle((p, h) -> p * 2)
            .channel(c -> c.queue("subscriber2Results"))
            .get();
}

我正在通过上面定义的第二次尝试获得此异常。

Caused by: java.lang.UnsupportedOperationException
at org.springframework.integration.dsl.StandardIntegrationFlow.configure(StandardIntegrationFlow.java:100)
at org.springframework.integration.dsl.PublishSubscribeSpec.subscribe(PublishSubscribeSpec.java:51)

我用spring-boot-starter-integration:2.0.0.M3尝试过。我错过了什么吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

子流不能是bean。你绝对可以将它们提取到自己的方法,甚至是private。但是将所有内容连接起来应该留给框架。

如果你仍然需要它们作为bean,那么通过消息通道使用流之间的连接。