ClientAbortException when using Jersey 2.13

后端 未结 4 1313
悲&欢浪女
悲&欢浪女 2020-12-29 10:10

I\'m using Jersey 2.13 in my web application for retrieving data async. There are some cases where requests take some time (i.E. when executing complex reports) until their

4条回答
  •  无人及你
    2020-12-29 10:36

    I've encountered this as well and finally found a guide to "solve" this.

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

    There are two options where the first is the one that is currently the accepted answer. The other, preferred way, is to add a WriterInterceptor to drop the ClientAbortException. My personal twist is to WARN log this occurrence instead.

    In case the URL is unreachable I add my implementation here. Don't forget to to register it in your Jersey context.

    import org.apache.catalina.connector.ClientAbortException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import javax.annotation.Priority;
    import javax.ws.rs.ext.Provider;
    import javax.ws.rs.ext.WriterInterceptor;
    import javax.ws.rs.ext.WriterInterceptorContext;
    import java.io.IOException;
    
    
    @Provider
    @Priority(1)
    public class ClientAbortExceptionWriterInterceptor implements WriterInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(ClientAbortExceptionWriterInterceptor.class);
    
    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException {
        try {
            context.proceed();
        } catch (Throwable t) {
            for (Throwable cause = t; cause != null; cause = cause.getCause()) {
                if (cause instanceof ClientAbortException) {
                    logger.warn("Client aborted request.", cause);
                    return;
                }
            }
            throw t;
        }
    }
    

    }

提交回复
热议问题