升级到Spring-Integration 4后,聚合器失败

时间:2017-05-22 13:23:21

标签: spring-integration

我们在项目中使用Spring-Integration。我们正在尝试从spring-integration-core:jar:3.0.1.RELEASE迁移到spring-integration-core:jar:4.3.2.RELEASEjava 8spring 4。我们遇到了aggregator的问题。奇怪的是,聚合器的方法在执行期间不会被调用。配置如下所示:

        <!-- Store the original payload in header for future purpose -->
    <int:header-enricher default-overwrite="true"  should-skip-nulls="true"  >
        <int:header name="${headerNames.originalPayload}" expression="payload" />
    </int:header-enricher>  

    <!-- split the issues-->
    <int-xml:xpath-splitter  >
    <int-xml:xpath-expression expression="//transaction"/>
    </int-xml:xpath-splitter>

    <int:service-activator  ref="httpOutboundGatewayHandler" method="buildHttpOutboundGatewayRequest" />

    <int:header-filter  header-names="accept-encoding"/>    

    <int-http:outbound-gateway  url-expression="headers.restResourceUrl"
                                http-method-expression="headers.httpMethod"
                                extract-request-payload="true"
                                expected-response-type="java.lang.String">
    </int-http:outbound-gateway>

    <int:service-activator ref="msgHandler" method="buildMessageFromExtSysResponse" />   


    <int-xml:xslt-transformer xsl-resource="${stylesheet.PQGetWorklist-Response-MoveSources}"  />
</int:chain>    

    <int:aggregator input-channel="PQGetWorklist-Aggregate-sources" output-channel="PQGetWorklist-MoveSourcesUnderIssues"  
                    ref="xmlAggregator"  method="aggregateSources">      
    </int:aggregator>   

在上面的代码中,<int-http:outbound-gateway正在执行,但由于未知原因未调用XmlAggregator.aggregateSources。我可以看到邮件已发送到频道PQGetWorklist-Aggregate-sources。但是从那里调用aggregator方法aggregateSources。结果,我们得到No reply received within timeout。请记住,相同的配置与spring-integration-core:jar:3.0.1.RELEASE一起正常运行。仅当我们将其升级到spring-integration-core:jar:4.3.2.RELEASE时才会出现此问题。 这是我的XmlAggregator.java

public class XmlAggregator {

    private static final Logger logger = Logger.getLogger(XmlAggregator.class);
    public Message aggregateSources(List < Message > messages) throws DocumentException {
     Document mainDom = XmlParserUtil.convertString2Document("<Results> </Results>");
     Document splitMessageDom = XmlParserUtil.convertString2Document(messages.get(0).getPayload().toString());
     Document IssuesDom = XmlParserUtil.convertString2Document("<Issues> </Issues>");
     Document sourcesDom = XmlParserUtil.convertString2Document("<RetrievedSources> </RetrievedSources>");
     if(messages.get(0).getHeaders().get("jobDesignerJobName").equals("PQIssueInquiry")){
     //extract callerType node
     Element callerType = XmlParserUtil.getXmlElements(XmlParserUtil.convertString2Document(messages.get(0).getPayload().toString()), "//callerType").get(0);
     //add callerType to root node
     mainDom.getRootElement().content().add(callerType);
     }
     //extract sort node
     Element sort = XmlParserUtil.getXmlElements(XmlParserUtil.convertString2Document(messages.get(0).getPayload().toString()), "//sort").get(0);
     //add sort to root node
     mainDom.getRootElement().content().add(sort);

     //get all the issues 
     List < Element > transactionElements = XmlParserUtil.getXmlElements(splitMessageDom, "//transaction");
     for (Element issue: transactionElements) {
      // add all the issues to the IssuesDom
      IssuesDom.getRootElement().content().add(issue);
     }
     //add all the issues to the root node
     XmlParserUtil.appendChild(mainDom, IssuesDom, null);
     for (Message source: messages) {

      Document sourcesTempDom = XmlParserUtil.convertString2Document(source.getPayload().toString());
      Reader xml = new StringReader((String) source.getPayload());
      SAXReader reader = new SAXReader();
      Document document = reader.read(xml);
      //get all the sources
      List < Element > sourceElements = XmlParserUtil.getXmlElements(sourcesTempDom, "//sources");

      for (Element sources: sourceElements) {
       //add all the sources to sourcesDom 
       sourcesDom.getRootElement().content().add(sources);
      }

     }

     // add all the sources to the root node
     XmlParserUtil.appendChild(mainDom, sourcesDom, null);

     MessageBuilder < ? > msgBuilder = MessageBuilder.withPayload(mainDom.asXML());
     Message message = msgBuilder.build();
     logger.debug("aggregateSources Results after aggregation " + mainDom.asXML());
     return message;
    }

}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

从版本4.2开始,XPathMessageSplitter默认情况下基于iterator功能:

<xsd:attribute name="iterator" default="true">
    <xsd:annotation>
        <xsd:documentation>
            The iterator mode: 'true' (default) to return an 'java.util.Iterator'
            for splitting 'payload', 'false to return a 'java.util.List'.
            Note: the 'list' contains transformed nodes whereas with the
            'iterator' each node is transformed while iterating.
        </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
        <xsd:union memberTypes="xsd:boolean xsd:string" />
    </xsd:simpleType>
</xsd:attribute>

不要以size兴趣开销内存,并根据需要从目标源中提取数据。

可以通过iterator="false"选项关闭该功能。