如何在 spring 集成 DSL 中为通道设置多个消息处理程序?
我编写了我的第一个 spring 集成应用程序,它从 spring RSS 读取数据并将其记录到控制台:
I wrote my first spring integration application which reads data from spring RSS and logs it into console:
@Configuration
@EnableIntegration
@IntegrationComponentScan
public class DslConfig {
@Bean
public IntegrationFlow feedFlow() throws MalformedURLException {
return IntegrationFlows.from(inBoundFeedDataAdapter(), configurer -> configurer.poller(Pollers.fixedDelay(1000)))
.channel(newsChannel())
.transform(source -> {
SyndEntry e = ((SyndEntry) source);
return e.getTitle() + " " + e.getLink();
})
.handle(messageHandler())
.get();
}
@Bean
public FeedEntryMessageSourceSpec inBoundFeedDataAdapter() throws MalformedURLException {
return Feed.inboundAdapter(new URL("https://spring.io/blog.atom"), "some_key");
}
@Bean
public MessageChannel newsChannel() {
return new DirectChannel();
}
@Bean
public MessageHandler messageHandler() {
return System.out::println;
}
}
但我不知道如何添加一个额外的处理程序来将结果写入文件.
But I have no idea how can I add one additional handler for writing result into file.
我怎样才能实现它?
其他问题:
元数据键是什么意思?
有一个 publishSubscribeChannel()
放置在流程中,你可以在那里添加 subscribe()
对于几个子流.他们每个人都将获得相同的消息进行处理.如果您还在配置中添加了 Executor
,则该过程将并行发生:
There is a publishSubscribeChannel()
to place in the flow and there you can add subscribe()
for several sub-flows. Each of them is going to get the same message to process. If you also add an Executor
to the configuration, the process is going to happen in parallel:
.publishSubscribeChannel(s -> s
.applySequence(true)
.subscribe(f -> f
.handle((p, h) -> "Hello"))
.subscribe(f -> f
.handle((p, h) -> "World!"))
);
See more info in Docs: https://docs.spring.io/spring-integration/docs/5.2.0.BUILD-SNAPSHOT/reference/html/dsl.html#java-dsl-subflows