【问题标题】:Spring integration http outbound gateway logging errorsSpring集成http出站网关日志记录错误
【发布时间】:2014-12-10 17:17:09
【问题描述】:

我有一个集成流,我从 jms 通道读取消息并使用 http-outbound-gateway 通过 REST api 调用发送它们。 现在,当我收到 5xx Http 服务器错误时,我想记录服务的 URL 以及响应消息。

我看到 RestTemplate 实例已经记录了 URL 并调用了 ResponseErrorHandler,这可以是自定义实现。

我的问题是:ResponseErrorHandler 仅接收 ClientHttpResponse 作为参数,我无法知道该实例的 URL。我需要的是记录服务器错误,例如:'错误 URL - 响应 ...'

我该怎么做?我无法覆盖RestTemplate 的错误处理并且ResponseErrorHandler 没有足够的信息。

非常感谢。

【问题讨论】:

  • 在你写的时候,RestTemplate 已经记录了 URL,那么问题出在哪里?
  • 我知道,但有两个问题:首先我需要它是 ERROR 级别(它是 WARN,否则我无法设置它),其次在相同的日志行。

标签: spring spring-integration


【解决方案1】:

嗯,我在那里看到的。

HttpRequestExecutingMessageHandler (<int-http:outbound-gateway>) 捕获所有底层异常:

catch (Exception e) {
        throw new MessageHandlingException(requestMessage, "HTTP request execution failed for URI ["
                + (realUri == null ? uri : realUri.toString())
                + "]", e);
    }

包括来自RestTemplateDefaultResponseErrorHandler 的结果。 如您所见,我们在这里使用 MessageHandlingException urirequestMessage

此外,此异常将委托给 jms channel

为什么这对你来说还不够?

从另一端,您可以将 <request-handler-advice-chain> 添加到 <int-http:outbound-gateway>ExpressionEvaluatingRequestHandlerAdvice 以处理 MessageHandlingException 就位:http://docs.spring.io/spring-integration/docs/latest-ga/reference/html/messaging-endpoints-chapter.html#expression-advice

【讨论】:

  • 您好,感谢您的帮助。问题是 RestTemplate 捕获异常并将其记录在级别警告中(RestTemplate 第 591 行),然后 HttpRequestExecutingMessageHandler 没有捕获异常。
  • 对。但最后RestTemplate#handleResponseError() 调用getErrorHandler().handleError(response);,其中DefaultResponseErrorHandler 抛出适当的RestClientException。如果您不覆盖默认值,则该异常将传播到HttpRequestExecutingMessageHandler
  • 好的,我知道了。我可以为 jms 通道设置一个错误处理程序,然后 IP 位于MessageHandlingException 内的嵌套异常中。不确定这是否是最简单的方法,但对我有用。感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
相关资源
最近更新 更多