[英]Spring Integration poll aggregator programmatically
我一直在使用Spring boot并摆脱项目中的所有XML
文件。 不幸的是,它也使用Spring集成,根据我的经验,该集成非常基于XML
。
我有一个场景,要求我有一个聚合器,并让该聚合器每x
秒钟轮询一次。
可以像这样使用XML来完成此操作(示例取自先前的SO问题):
<!--
the poller will process 100 messages every minute
if the size of the group is 100 (the poll reached the max messages) or 60 seconds time out (poll has less than 100 messages) then the payload with the list of messages is passed to defined output channel
-->
<int:aggregator input-channel="logEntryChannel" output-channel="logEntryAggrChannel"
send-partial-result-on-expiry="true"
group-timeout="60000"
correlation-strategy-expression="T(Thread).currentThread().id"
release-strategy-expression="size() == 100">
<int:poller max-messages-per-poll="100" fixed-rate="60000"/>
</int:aggregator>
我设法找到了一个可以解决问题的类,它的bean定义是:
@Bean(name = "aggregatingMessageHandler")
public AggregatingMessageHandler aggregatingMessageHandler() {
AggregatingMessageHandler aggregatingMessageHandler =
new AggregatingMessageHandler(messageGroupProcessorBean(),
new SimpleMessageStore(10));
aggregatingMessageHandler.setCorrelationStrategy(customCorrelationStrategyBean());
aggregatingMessageHandler.setReleaseStrategy(
new TimeoutCountSequenceSizeReleaseStrategy(3,
TimeoutCountSequenceSizeReleaseStrategy.DEFAULT_TIMEOUT));
aggregatingMessageHandler.setExpireGroupsUponCompletion(true);
aggregatingMessageHandler.setOutputChannel(outputAggregatedChannelBean());
return aggregatingMessageHandler;
}
但是,仅当与此处理程序相关联的inboundChannel
中接收到inboundChannel
时才触发ReleaseStrategy
的canRelease()
方法,而不是在不希望的固定时间间隔上inboundChannel
。 我希望所有早于一分钟的组都重定向到输出通道。 我的问题是-有没有办法以编程方式附加XML定义中的轮询器?
Aggregator
组件具有AggregatorFactoryBean
,可简化Java配置。
无论如何,您必须注意在该处理程序定义上有一个@ServiceActivator
批注以及一个@Bean
。 正是@ServiceActivator
具有poller
属性。
还请注意,存在用于Spring Integration的Java DSL 。
您问题的另一部分是混乱。 poller
与释放策略完全无关。 在这种情况下,它的责任是从PollableChannel
接收消息,即logEntryChannel
。 并且只有在此之后,已经轮询的消息才被放入聚合器以进行关联和释放逻辑。
在该示例中完成的是完全不同的故事,我们可以在单独的SO线程中进行讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.