Spring integration: handle http error with oubound gateway

微笑、不失礼 提交于 2019-12-30 13:30:38

问题


How can I handle exceptions in an http outbound gateway? When i receive status code 500 or 400..., an exception is shown. So What should I do to handle http error using spring integration.

My configuration is like:

<int:inbound-channel-adapter channel="quakeinfotrigger.channel"
    expression="''">
    <int:poller fixed-delay="60000"></int:poller>
</int:inbound-channel-adapter>


<int:channel id="quakeinfo.channel">
    <int:queue capacity="10" />
</int:channel>

<int:channel id="quakeinfotrigger.channel"></int:channel>

<int:channel id="error.channel">
<int:queue capacity="10" />
</int:channel>

<int:service-activator input-channel="error.channel"
    ref="httpResponseErrorHandler" method="handleMessage">
    <int:poller fixed-delay="5000"></int:poller>
</int:service-activator>

<int:service-activator input-channel="quakeinfo.channel"
    ref="httpResponseMessageHandler" method="handleMessage">
    <int:poller fixed-delay="5000"></int:poller>
</int:service-activator>

<int:gateway id="requestGateway" service-interface="standalone.HttpRequestGateway"
    default-request-channel="quakeinfotrigger.channel" error-channel="error.channel" />

<int-http:outbound-gateway id="quakerHttpGateway"
    request-channel="quakeinfotrigger.channel" url="http://fooo/mmmm/rest/put/44545454"
    http-method="PUT" expected-response-type="java.lang.String" charset="UTF-8"
    reply-timeout="5000" reply-channel="quakeinfo.channel">
</int-http:outbound-gateway>

<bean id="httpResponseMessageHandler" class="standalone.HttpResponseMessageHandler" />
<bean id="httpResponseErrorHandler" class="standalone.HttpResponseErrorHandler" />

I would like to know why exception does'nt go to reply-channel


回答1:


I would like to know why exception does'nt go to reply-channel

Because it's natural to handle exceptions as, er, Exceptions.

There are (at least) two ways to handle exceptions in Spring Integration.

  1. Add an error-channel and associated flow on whatever starts your flow (e.g. a gateway). The error channel gets an ErrorMessage with a MessagingException payload; the exception has two properties - the failedMessage and the cause.
  2. Add a ExpressionEvaluatingRequestHandlerAdvice (or a custom advice) to the gateway; see Adding Behavior to Endpoints.



回答2:


If the response status code is in the HTTP series CLIENT_ERROR or SERVER_ERROR, HttpClientErrorException or HttpServerErrorException are thrown respectively. Hence the response doesn't go to reply channel

Refer DefaultResponseErrorHandler
Methods: hasError and handleError

To Handle these exceptions, create your own CustomResponseErrorHandler and override contents of hasError and handlerError methods.

public class CustomResponseErrorHandler extends DefaultResponseErrorHandler {

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        return hasError(getHttpStatusCode(response));
    }

    protected boolean hasError(HttpStatus statusCode) {
        return /*Status Code to be considered as Error*/ ;
    }

    @Override
    public void handleError(ClientHttpResponse response) throws IOException { /* Handle Exceptions */
    }
}  

And add error-handler to your http-outbound-gateway

<int-http:outbound-gateway id="quakerHttpGateway"
    request-channel="quakeinfotrigger.channel" url="http://fooo/mmmm/rest/put/44545454"
    http-method="PUT" expected-response-type="java.lang.String" charset="UTF-8"
    reply-timeout="5000" reply-channel="quakeinfo.channel" error-handler="customResponseErrorHandler">
</int-http:outbound-gateway>



回答3:


I had occurred the same problem.I threw my own customized Exception and error message but always got "500 Internal server error".It's because there will be no reply message and "reply" is timeout when throw Exception.So I handle the exception by subscribing error-channel and then reply by myself.

Message<?> failedMessage = exception.getFailedMessage();
Object replyChannel = new MessageHeaderAccessor(failedMessage).getReplyChannel();
if (replyChannel != null) {
    ((MessageChannel) replyChannel).send(MessageFactory.createDataExchangeFailMessage(exception));
}


来源:https://stackoverflow.com/questions/32945231/spring-integration-handle-http-error-with-oubound-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!