Spring 集成:使用 oubound 网关处理 http 错误
Spring integration: handle http error with oubound gateway
如何处理 http 出站网关中的异常?
当我收到状态代码 500 或 400... 时,会显示异常。那么我应该怎么做才能使用 spring 集成来处理 http 错误。
我的配置是这样的:
<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
因为将异常处理为,呃,异常是很自然的。
在 Spring 集成中有(至少)两种处理异常的方法。
- 在启动流程的任何地方(例如网关)添加
error-channel
和相关流程。错误通道得到一个 ErrorMessage
和 MessagingException
有效负载;该异常有两个属性 - failedMessage
和 cause
.
- 向网关添加
ExpressionEvaluatingRequestHandlerAdvice
(或自定义建议);参见 Adding Behavior to Endpoints。
如果响应状态码在HTTP系列CLIENT_ERROR或SERVER_ERROR,HttpClientErrorException 或 HttpServerErrorException 分别抛出。因此响应不会进入回复频道
Refer DefaultResponseErrorHandler
Methods: hasError and handleError
要处理这些异常,请创建您自己的 CustomResponseErrorHandler 并覆盖 hasError 和 handlerError 方法的内容。
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 */
}
}
并将 error-handler 添加到您的 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>
我也遇到过同样的情况 problem.I 抛出了我自己定制的异常和错误消息,但总是收到“500 内部服务器错误”。这是因为没有回复消息,并且 "reply" 超时throw Exception.So 我通过订阅error-channel处理异常,然后自己回复
Message<?> failedMessage = exception.getFailedMessage();
Object replyChannel = new MessageHeaderAccessor(failedMessage).getReplyChannel();
if (replyChannel != null) {
((MessageChannel) replyChannel).send(MessageFactory.createDataExchangeFailMessage(exception));
}
如何处理 http 出站网关中的异常? 当我收到状态代码 500 或 400... 时,会显示异常。那么我应该怎么做才能使用 spring 集成来处理 http 错误。
我的配置是这样的:
<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
因为将异常处理为,呃,异常是很自然的。
在 Spring 集成中有(至少)两种处理异常的方法。
- 在启动流程的任何地方(例如网关)添加
error-channel
和相关流程。错误通道得到一个ErrorMessage
和MessagingException
有效负载;该异常有两个属性 -failedMessage
和cause
. - 向网关添加
ExpressionEvaluatingRequestHandlerAdvice
(或自定义建议);参见 Adding Behavior to Endpoints。
如果响应状态码在HTTP系列CLIENT_ERROR或SERVER_ERROR,HttpClientErrorException 或 HttpServerErrorException 分别抛出。因此响应不会进入回复频道
Refer DefaultResponseErrorHandler
Methods: hasError and handleError
要处理这些异常,请创建您自己的 CustomResponseErrorHandler 并覆盖 hasError 和 handlerError 方法的内容。
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 */
}
}
并将 error-handler 添加到您的 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>
我也遇到过同样的情况 problem.I 抛出了我自己定制的异常和错误消息,但总是收到“500 内部服务器错误”。这是因为没有回复消息,并且 "reply" 超时throw Exception.So 我通过订阅error-channel处理异常,然后自己回复
Message<?> failedMessage = exception.getFailedMessage();
Object replyChannel = new MessageHeaderAccessor(failedMessage).getReplyChannel();
if (replyChannel != null) {
((MessageChannel) replyChannel).send(MessageFactory.createDataExchangeFailMessage(exception));
}