Spring Integration poll聚合器以编程方式

时间:2016-11-22 14:27:23

标签: spring spring-boot spring-integration

我一直在使用Spring启动并删除项目中的所有XML个文件。 不幸的是,它也使用了Spring集成,根据我的经验,它基于XML。{/ p>

我有一个场景要求我有一个聚合器,并且每隔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(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;
}

但是,只有在与此处理程序关联的canRelease()中收到新消息时,才会触发ReleaseStrategy的{​​{1}}方法,而不会以固定的时间间隔触发inboundChannel期望的结果。 我希望将超过一分钟的所有组重定向到输出通道。 我的问题是 - 有没有办法以编程方式附加轮询器,例如XML定义中的轮询器?

1 个答案:

答案 0 :(得分:1)

对于Java&amp;注释配置看一看herehere

Aggregator组件具有AggregatorFactoryBean,以便于Java配置。

无论如何,您必须注意在该处理程序定义上有@ServiceActivator注释和@Bean。确切地说,@ServiceActivator具有poller属性。

还要注意Spring Integration有一个Java DSL

你问题的另一部分有点混乱。 poller完全与发布策略无关。在这种情况下,它有责任接收来自PollableChannel logEntryChannel的消息。只有在那之后,已经轮询的消息才会被放置到聚合器以获得它的相关性和释放逻辑。

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