繁体   English   中英

Spring Integration轮询聚合器以编程方式

[英]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时才触发ReleaseStrategycanRelease()方法,而不是在不希望的固定时间间隔上inboundChannel 我希望所有早于一分钟的组都重定向到输出通道。 我的问题是-有没有办法以编程方式附加XML定义中的轮询器?

对于Java和注释配置,请在此处此处查看

Aggregator组件具有AggregatorFactoryBean ,可简化Java配置。

无论如何,您必须注意在该处理程序定义上有一个@ServiceActivator批注以及一个@Bean 正是@ServiceActivator具有poller属性。

还请注意,存在用于Spring Integration的Java DSL

您问题的另一部分是混乱。 poller与释放策略完全无关。 在这种情况下,它的责任是从PollableChannel接收消息,即logEntryChannel 并且只有在此之后,已经轮询的消息才被放入聚合器以进行关联和释放逻辑。

在该示例中完成的是完全不同的故事,我们可以在单独的SO线程中进行讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM