如何捕获和忽略 org.apache.catalina.connector.ClientAbortException
Posted
技术标签:
【中文标题】如何捕获和忽略 org.apache.catalina.connector.ClientAbortException【英文标题】:How to catch and Ignore org.apache.catalina.connector.ClientAbortException 【发布时间】:2013-12-28 21:34:59 【问题描述】:我想捕获并忽略以下tomcat ClientAbortException
。
因为我的程序没有必要关注这一点。
知道如何以及在哪里可以捕获此异常吗? 以下是例外:
14:46:43.920 [ajp-bio-8029-exec-538] ERROR com.sok.runway.URLFilter - Exception in URLFilter
org.apache.catalina.connector.ClientAbortException: null
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388) ~[catalina.jar:7.0.26]
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462) ~[tomcat-coyote.jar:7.0.26]
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366) ~[tomcat-coyote.jar:7.0.26]
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:413) ~[catalina.jar:7.0.26]
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:401) ~[catalina.jar:7.0.26]
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91) ~[catalina.jar:7.0.26]
at org.apache.catalina.servlets.DefaultServlet.copyRange(DefaultServlet.java:2031) ~[catalina.jar:7.0.26]
at org.apache.catalina.servlets.DefaultServlet.copy(DefaultServlet.java:1870) ~[catalina.jar:7.0.26]
at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:958) ~[catalina.jar:7.0.26]
at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:411) ~[catalina.jar:7.0.26]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) ~[servlet-api.jar:na]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.26]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.26]
at com.sok.runway.URLFilter.doFilter(URLFilter.java:130) ~[runway-dev.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.26]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.26]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) [catalina.jar:7.0.26]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) [catalina.jar:7.0.26]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.26]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.26]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [catalina.jar:7.0.26]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) [catalina.jar:7.0.26]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.26]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.26]
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200) [tomcat-coyote.jar:7.0.26]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) [tomcat-coyote.jar:7.0.26]
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) [tomcat-coyote.jar:7.0.26]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
Caused by: java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113) ~[na:1.7.0_03]
at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.7.0_03]
at org.apache.coyote.ajp.AjpProcessor.output(AjpProcessor.java:295) [tomcat-coyote.jar:7.0.26]
at org.apache.coyote.ajp.AbstractAjpProcessor$SocketOutputBuffer.doWrite(AbstractAjpProcessor.java:1082) ~[tomcat-coyote.jar:7.0.26]
at org.apache.coyote.Response.doWrite(Response.java:533) ~[tomcat-coyote.jar:7.0.26]
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:383) ~[catalina.jar:7.0.26]
【问题讨论】:
【参考方案1】:由于您可能不希望依赖于 Catalina,因此您可以像这样忽略异常(我假设 ClientAbortException 是原因):
String simpleName = e.getCause().getClass().getSimpleName();
if (simpleName.equals("ClientAbortException"))
// ignore
else
// do whatever you do
【讨论】:
【参考方案2】:在 Spring 4 中,您将把它放在一个带有 @ControllerAdvice 注释的类中。诀窍是只抑制ClientAbortException,同时允许所有其他IOException 被处理为错误或记录,而不引入对容器API 的编译/运行时依赖:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
//import org.apache.catalina.connector.ClientAbortException; // We don't want this dependency.
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
@ControllerAdvice
public final class ErrorController
@ExceptionHandler(NoHandlerFoundException.class)
public String handleNoHandlerFoundException(final NoHandlerFoundException ex)
return "notFoundPage";
@ExceptionHandler(IOException.class)
public String handleAbortedConnection(final IOException ex)
// avoids compile/runtime dependency by using class name
if (ex.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException"))
return null;
// log and return error page for any other IOExceptions
if (LOGGER.isLoggable(Level.WARNING))
LOGGER.log(Level.WARNING, "IO Error", ex);
return "errorPage";
@ExceptionHandler(Exception.class)
public String handleException(final Exception ex)
if (LOGGER.isLoggable(Level.SEVERE))
LOGGER.log(Level.SEVERE, "Server Error", ex);
return "errorPage";
感谢这个答案提供了我们return null for the view name to avoid throwing a SocketException on the closed OutputStream 的缺失部分。
【讨论】:
【参考方案3】:ClientAbortException
扩展 IOException
。您可以尝试下面的代码来捕获异常:
try
// codes
out.write("xyz"); // throws IOException
catch (org.apache.catalina.connector.ClientAbortException ca)
System.out.println("ClientAbortException caught");
catch (IOException ioe)
System.out.println("i/o exception raised. abrorting.");
【讨论】:
但我的问题更像是我应该在代码中的哪里捕获这个异常?我需要在哪个类中添加这个 catch 块? 因为你需要发布你的代码,并尝试调试你的代码,这样你就可以确切地知道你在什么时候得到了异常,然后试着把这些代码放在 try catch 中。 如果这样做,你会遇到编译/运行时依赖问题。以上是关于如何捕获和忽略 org.apache.catalina.connector.ClientAbortException的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中捕获或覆盖“onCellLocationChanged():得到不是 gsm 的单元格位置,忽略”事件