gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:46:43 24 4
gpt4 key购买 nike

我在一个浏览器上使用 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) 并检查异常是否包含破损的管道,然后没有其他任何东西向客户端发送消息。

关于java - 避免充满 java.io.IOException : Broken pipe 的日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48564452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com