Spring Integration Aggregator - 丢失的消息

时间:2014-06-10 18:54:57

标签: java spring spring-integration

我想收集一些消息(比方说10)并将它们作为列表传递给服务激活器,而不是逐个传递它们。

背景信息:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=...>

    <int:channel id="ch.http.in"/>
    <int:channel id="ch.http.trans"/>
    <int:channel id="ch.http.aggr"/>
    <int-http:inbound-channel-adapter path="test" channel="ch.http.in"/>

    <int:map-to-object-transformer input-channel="ch.http.in" output-channel="ch.http.trans" type="demo.Req"/>
    <int:aggregator 
        input-channel="ch.http.trans" 
        output-channel="ch.http.aggr"
        release-strategy-expression="size() == 10"
        correlation-strategy-expression="headers['id']"
        ref="aggr" method="add"/>
    <int:service-activator ref="srv" method="httpTest" input-channel="ch.http.aggr"/>

    <bean id="srv" class="demo.IntService"/>
    <bean id="aggr" class="demo.HttpAggregator"/>
</beans>

聚合传播者:

public class HttpAggregator{
    public List<Req> add(List<Req> reqs) {
        System.out.println(reqs);
        return reqs;
      }
}

服务:

public class IntService {
    public void httpTest(Req msg){
        System.out.println(msg);
    }
}

Req只是一个POJO。

问题是永远不会调用聚合器方法。如果没有聚合器,消息将毫无问题地传递给服务激活器。 使用Spring Integration 3.0.2.RELEASE(Spring Boot 1.0.2.RELEASE)

编辑: 当我将correlation-strategy-expression="headers['id']"更改为correlation-strategy-expression="payload.id"(Req对象具有属性ID)时,当我为每个块传递不同的id时(例如,前10个为id = 1;下10个为2时为2)... )看起来这就是相关策略的工作原理。我怎么能通过呢?我只想限制聚合列表的大小。

1 个答案:

答案 0 :(得分:4)

右;你必须关联某事;使用标题['id']最终将会有大量的1个项目,这些项目永远不会满足发布策略。

对于像你这样的简单用例,关联文字 - 例如correlation-expression="'foo'"并设置expire-groups-on-completion="true"。这会在发布后重置该组,因此可以在下一条消息上开始一个新的(具有相同的相关ID)。

如果要在超时后释放部分组,则需要MessageGroupStoreReaper。或者,如果您可以升级到4.0.x,则聚合器现在具有group-timeout(或group-timeout-expression)。

相关问题