使用Scatter-Gather模式忽略Spring Integration的一个错误

时间:2017-01-15 12:17:06

标签: java spring spring-integration

我正在编写一个使用Scatter-Gather模式的Spring Integration应用程序。 我的服务器代码使用Pippo来提供如下请求:

public static void main(String[] args) {
    Pippo pippo = new Pippo();
    pippo.GET("/one", routeContext -> {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        routeContext.send("One Hello World!");
    });
    pippo.GET("/two", routeContext -> routeContext.send("Two Hello World!"));
    pippo.GET("/three", routeContext -> {
        throw new RuntimeException("err");
    });
    pippo.start();
}

在第三个请求中,服务器失败。 我的弹簧配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/integration
    http://www.springframework.org/schema/integration/spring-integration.xsd
    http://www.springframework.org/schema/integration/http
    http://www.springframework.org/schema/integration/http/spring-integration-http.xsd">

<int:gateway service-interface="ro.oss.thirdparty.PippoGateway" />

<int:channel id="inputDistribution" />
<int:channel id="gatherChannel" />
<int:channel id="distribution1Channel" />
<int:channel id="distribution2Channel" />
<int:channel id="distribution3Channel" />

<int:scatter-gather input-channel="inputDistribution"
    gather-channel="gatherChannel">
    <int:scatterer apply-sequence="true">
        <int:recipient channel="distribution1Channel" />
        <int:recipient channel="distribution2Channel" />
        <int:recipient channel="distribution3Channel" />
    </int:scatterer>
</int:scatter-gather>

<int-http:outbound-gateway request-channel="distribution1Channel"
    url="http://localhost:8338/one" http-method="GET" expected-response-type="java.lang.String" />
<int-http:outbound-gateway request-channel="distribution2Channel"
    url="http://localhost:8338/two" http-method="GET" expected-response-type="java.lang.String" />
<int-http:outbound-gateway request-channel="distribution3Channel"
    url="http://localhost:8338/three" http-method="GET" expected-response-type="java.lang.String" />

有没有办法忽略那些给我错误的频道并从好的频道收集?现在我在控制台中得到这样的东西:

Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:628)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:549)
at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:382)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.router.AbstractMessageRouter.handleMessageInternal(AbstractMessageRouter.java:194)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.channel.FixedSubscriberChannel.send(FixedSubscriberChannel.java:70)
at org.springframework.integration.channel.FixedSubscriberChannel.send(FixedSubscriberChannel.java:64)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.

谢谢

1 个答案:

答案 0 :(得分:1)

聚合器(收集器)需要3个回复。

您可以为每个网关添加ExpressionEvaluatingRequestHandlerAdvicefailureExpression并将returnFailureExpressionResult设置为true;发生异常时,将计算表达式,并返回其结果以与其他结果聚合。

请参阅Adding Behavior to Endpoints