【问题标题】:Ignore one error for Spring Integration with Scatter-Gather pattern忽略 Spring Integration with Scatter-Gather 模式的一个错误
【发布时间】:2017-01-15 12:17:05
【问题描述】:

我正在编写一个使用 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();
}

第三次请求服务器失败。 我的spring配置是这样的:

<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.

谢谢

【问题讨论】:

    标签: java spring spring-integration


    【解决方案1】:

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

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

    Adding Behavior to Endpoints

    【讨论】:

    • 谢谢你,成功了。我想问一些新问题,我不知道我是否应该提出一个新问题。如果我想过滤掉其中一个分布式通道上的一些消息,它会阻止所有内容,因为我没有来自其中一个通道的响应。这也是配置吗?谢谢
    • 您通常应该提出一个新问题(必要时参考这个问题);这里的管理员不喜欢扩展的 cmets。您可以使用自定义发布策略docs herehere 自定义收集器。但是,您的发布策略将需要一些机制来知道该组何时完成。路由到不同的回复可能比过滤更容易。
    猜你喜欢
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    相关资源
    最近更新 更多