如何使用spring集成聚合两个Web服务的响应

时间:2014-12-17 10:29:34

标签: soap spring-integration soap-client

我使用spring-integration调用两个webservices,如下所示:

应用context.xml中

<int:chain input-channel="requestChannelForHolidayService"
    output-channel="outputChannelForHolidayService">
    <int-ws:outbound-gateway
        uri="http://localhost:8080/holidayService/holidayService" marshaller="marshaller"
        unmarshaller="marshaller" />
</int:chain>

<int:chain input-channel="requestChannelForAccount"
    output-channel="outputChannelForAccount">
    <int-ws:outbound-gateway
        uri="http://localhost:8080/spring-webservices-sample/endpoints"
        marshaller="marshaller1" unmarshaller="marshaller1" />
</int:chain>

Testrunner.java

ApplicationContext context =  new ClassPathXmlApplicationContext("application-context.xml");  
    MessageChannel channel=(MessageChannel) context.getBean("requestChannelForHolidayService",MessageChannel.class);  
    HolidayRequest request=new HolidayRequest();
    BigInteger b1=new BigInteger("1");
    BigInteger b2=new BigInteger("50");
    request.setEmpId(b1);
    request.setDays(b2);
    System.out.println("sending request");
    System.out.println("request sent");
    channel.send(MessageBuilder.withPayload(request).build());

    ApplicationContext context1 =  new ClassPathXmlApplicationContext("application-context.xml");  
    MessageChannel channel1=(MessageChannel) context1.getBean("requestChannelForAccount",MessageChannel.class);  
    AccountDetailsRequest request2=new AccountDetailsRequest();
    request2.setAccountNumber("12345");
    System.out.println("sending request2");
    System.out.println("request sent2");
    channel1.send(MessageBuilder.withPayload(request2).build());

现在我需要结合输出&#39; outputChannelForHolidayService&#39;和&#39; outputChannelForAccount&#39;。任何人都可以帮助实现这一目标。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你不应该使用两个应用程序上下文;把它们放在同一个环境中。在消息上设置correlationId标头;使用release-strategy-expression="size == 2"将结果发送给聚合器。

考虑使用Messaging Gateway而不是发送到频道。类似的东西:

Collection<Object> process(@Payload Object[] requests, @Header("correlationId"), String correlation);

然后在上下文中,有......

gateway->splitter->payload-type-router->
    request1Channel->ws->toAgg
    request2Channel->ws->toAgg

toAggChannel->aggregator

如果您从聚合器中省略output-channel,结果将返回到网关。