如何在 spring 集成 DSL 中为通道设置多个消息处理程序?
How to set several message handlers for channel in spring integration DSL?
我编写了我的第一个 spring 集成应用程序,它从 spring RSS 读取数据并将其记录到控制台:
@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;
}
}
但我不知道如何添加一个额外的处理程序来将结果写入文件。
如何实现?
补充问题:
元数据键的含义是什么?
有一个 publishSubscribeChannel()
可以放在流中,您可以在其中为多个子流添加 subscribe()
。他们每个人都将获得相同的消息来处理。如果您还向配置添加 Executor
,则该过程将并行发生:
.publishSubscribeChannel(s -> s
.applySequence(true)
.subscribe(f -> f
.handle((p, h) -> "Hello"))
.subscribe(f -> f
.handle((p, h) -> "World!"))
);
我编写了我的第一个 spring 集成应用程序,它从 spring RSS 读取数据并将其记录到控制台:
@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;
}
}
但我不知道如何添加一个额外的处理程序来将结果写入文件。
如何实现?
补充问题:
元数据键的含义是什么?
有一个 publishSubscribeChannel()
可以放在流中,您可以在其中为多个子流添加 subscribe()
。他们每个人都将获得相同的消息来处理。如果您还向配置添加 Executor
,则该过程将并行发生:
.publishSubscribeChannel(s -> s
.applySequence(true)
.subscribe(f -> f
.handle((p, h) -> "Hello"))
.subscribe(f -> f
.handle((p, h) -> "World!"))
);