我在一个浏览器上使用服务器发送事件,并在后端使用 Spring Boot 应用程序。当我击落客户端时,我得到了下一个异常:
14:35:09.458 [http-nio-8084-exec-25] ERROR o.a.c.c.C.[Tomcat].[localhost] - Exception Processing ErrorPage[errorCode=0, location=/error]
org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe
我理解这是预期的行为;另一方面,我的应用程序运行良好,但我的日志充满了这些异常。我猜这是Tomcat造成的。有没有办法捕获这些异常,或者至少阻止 Tomcat 将此异常堆栈跟踪写入日志?我的意思是,不修改 Tomcat 的代码。
为了防止日志中出现此异常,您可以尝试对在客户端上执行推送的代码进行一些更改。这是我的例子。我监听调用的 api,然后它调用我将套接字推送到客户端。我想你可以理解代码:
@GetMapping("/api/status/{groupId}/{groupstatusId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ExceptionHandler(IOException.class)
public void listenToNewStatus(@PathVariable Long groupId, @PathVariable String groupstatusId, IOException e) {
Group group = groupDTOService.findById(groupId);
if (group != null) {
if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) {
logger.info("broken pipe");
} else {
template.convertAndSend("/topic/callstatus/" + group.getUser().getId(), groupstatusId);
}
}
在这段代码中,为了防止管道损坏,我添加注释 @ExceptionHandler(IOException.class) 并检查异常是否包含损坏的管道,然后没有其他内容向客户端发送消息。
我发现这个问题已经很老了,但如果有人仍在寻找答案,有一些关于如何静音 ClientAbortException 的博客文章,这样它就不会淹没您的日志。
https://tutorial-academy.com/jersey-workaround-clientabortexception-ioexception/