繁体   English   中英

避免日志充满 java.io.IOException: Broken pipe

[英]Avoid logs full of java.io.IOException: Broken pipe

我在一个浏览器上使用 Server-Sent 事件,并在后端使用 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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM