如何使聚合器等待publish-subsribe通道中所有方法的响应?

时间:2017-05-19 12:02:39

标签: spring-integration

我使用下面的spring配置来组合使用聚合器的发布订阅频道的结果。但聚合器仅从发布订阅通道中的第一个服务激活器填充响应,并且它不等待来自其他服务激活器的响应。我应该如何修改配置以使聚合器等待来自所有4个服务激活器的响应?

<int:bridge id="ValidationsBridge" input-channel="RequestChannel" output-channel="bridgeOutputChannel"></int:bridge>

    <int:publish-subscribe-channel id="bridgeOutputChannel"  apply-sequence="true" />
        <int:service-activator input-channel="bridgeOutputChannel" output-channel="aggregatorInput"
        method="populateResponse1" ref="WebServiceImpl" >
    </int:service-activator>
    <int:service-activator input-channel="bridgeOutputChannel" method="populateResponse2" ref="WebServiceImpl" output-channel="aggregatorInput"
    >
    </int:service-activator>
    <int:service-activator input-channel="bridgeOutputChannel" method="populateResponse3" ref="WebServiceImpl" output-channel="aggregatorInput"
    >
    </int:service-activator>
    <int:service-activator input-channel="bridgeOutputChannel" method="populateResponse4" ref="WebServiceImpl" output-channel="aggregatorInput"
    >
    </int:service-activator>

    <task:executor id="executor" pool-size="4" keep-alive="20"/> 

    <int:aggregator input-channel="aggregatorInput" output-channel="aggregatorOutput" ref="vehicleAggregator"   method="populateResponse"
     ></int:aggregator>


    <int:service-activator id="processorServiceActivator" input-channel="aggregatorOutput" ref="Processor" method="mapResponse"  output-channel="ResponseChannel"/> 


     <int:channel id="bridgeOutputChannel" />
     <int:channel id="aggregatorInput" />
    <int:channel id="aggregatorOutput" /> 

  </beans>

以下是我的聚合器

的摘录
public Message<?> populateResponse(Collection<Message<?>> message){
        MessageBuilder<?> MsgBuilder =null;
        MsgBuilder=MessageBuilder.withPayload(message.iterator().next().getPayload());
        for (Message<?> message2 : message) {

            if(null!=message2.getHeaders().get(Constants.RESPONSE1)){
                MsgBuilder.setHeader(Constants.RESPONSE1, message2.getHeaders().get(Constants.RESPONSE1));
            }
            if(null!=message2.getHeaders().get(Constants.RESPONSE2)){
                MsgBuilder.setHeader(Constants.RESPONSE2, message2.getHeaders().get(Constants.RESPONSE2));
            }
                    }
        return (Message<?>) MsgBuilder.build();
    }

1 个答案:

答案 0 :(得分:0)

您应该在该发布 - 订阅频道上使用apply-sequence="true"(默认情况下应该在那里),并且不要在聚合器上使用任何correlation选项 - 只需依赖默认{{1由correlationKey填充的标题。

根据消息ID(您的代码)建立关联策略,使聚合器为每条消息构建新的组,因为消息ID始终是唯一的。

顺便说一句,您也不需要发布策略。聚合器可以通过填充apply-sequence标头轻松完成。

而且我不确定你是否也需要sequenceNumber

换句话说,您的用例所需要的只是依靠开箱即用的序列详细信息功能:

http://docs.spring.io/spring-integration/docs/4.3.9.RELEASE/reference/html/messaging-channels-section.html#channel-configuration-pubsubchannel http://docs.spring.io/spring-integration/docs/4.3.9.RELEASE/reference/html/messaging-routing-chapter.html#aggregator