简体   繁体   中英

Avoid logs full of java.io.IOException: Broken pipe

I am using Server-Sent events on one browser, and a spring boot application on the back end. When I shot down the client, I get the next exception:

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

I understand this is the expected behavior; on the other hand, my application works fine, but I have awful logs full of those exceptions. I guess this is caused by Tomcat. Is there a way to catch these exceptions, or at least to prevent Tomcat from writing this exception stack trace to the log? I mean, without modifying Tomcat's code.

To prevent this exception in logs you can try some changes in your code that performs push on client. Here is my example. I listen to api called and then it called I push socket to the client. I think you could understand the code:

 @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);
        }

    }

In this code to prevent broken pipe I add annotation @ExceptionHandler(IOException.class) and check if exception contains broken pipe then nothing else send message to client.

I see that this question is quite old, but in case someone is still looking for an answer, there are a few blog posts on how to mute ClientAbortException so it doesn't flood your logs.

https://tutorial-academy.com/jersey-workaround-clientabortexception-ioexception/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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