防止 HeartbeatServlet 将空指针异常写入日志



我有以下代码:

HeartbeatServlet.java:

public final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
if (session != null) {
compAjaxResponse ajaxResponse = new compAjaxResponse();
ajaxResponse.addAction(new JavascriptAction("sendNextBeat", "setTimeout("sendHeartBeat()"," + MILLIS_PER_SECOND * HEARTBEAT_INTERVAL + ")", true));
if (CSGNotificationTracker.hasNotificationChanged()) {
ajaxResponse.addAction(new InnerHtmlAjaxAction("importantMessage", CSGNotificationTracker.getNotificationHtml()));
}
response.getWriter().write(ajaxResponse.getResponse(request));
}
return;
}

GZIPResponseWrapper.java:

public class GZIPResponseWrapper extends HttpServletResponseWrapper {
protected HttpServletResponse origResponse = null;
protected ServletOutputStream stream = null;
protected PrintWriter writer = null;
public GZIPResponseWrapper(HttpServletResponse response) {
super(response);
origResponse = response;
}
public ServletOutputStream createOutputStream() throws IOException {
return (new GZIPResponseStream(origResponse));
}
public void finishResponse() {
try {
if (writer != null) {
writer.close();
} else {
if (stream != null) {
stream.close();
}
}
} catch (IOException e) {}
}
public void flushBuffer() throws IOException {
stream.flush();
}
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException("getWriter() has already been 
called!");
}
if (stream == null)
stream = createOutputStream();
return (stream);
}
public PrintWriter getWriter() throws IOException {
if (writer != null) {
return (writer);
}
if (stream != null) {
throw new IllegalStateException("getOutputStream() has already been 
called!");
}
stream = createOutputStream();
writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
return (writer);
}
public void setContentLength(int length) {}
}

当用户退出浏览器而不注销时,我们会在日志中写入多个检测信号 nullpointer 异常。

例外:

[7/14/17 9:20:46:326 EDT] 000001e8 webapp        E 
com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Servlet 
Error]-[compHeartbeatServlet]: java.lang.NullPointerException
2017-07-14 11:03:09.340 |   at 
com.csg.service.performance.gzip.GZIPResponseWrapper.flushBuffer 
(GZIPResponseWrapper.java:39)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest 
(ServletWrapper.java:816)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest 
(ServletWrapper.java:480)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest 
(ServletWrapperImpl.java:178)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget 
(WebAppFilterChain.java:136)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:97)
2017-07-14 11:03:09.340 |   at 
com.comp.compservice.security.xss.XssFilter.doFilter(XssFilter.java:33)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter 
(FilterInstanceWrapper.java:195)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:91)
2017-07-14 11:03:09.340 |   at 
com.csg.service.performance.gzip.GZIPFilter.doFilter(GZIPFilter.java:32)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter 
(FilterInstanceWrapper.java:195)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter 
(WebAppFilterChain.java:91)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter 
(WebAppFilterManager.java:967)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters 
(WebAppFilterManager.java:1107)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest 
(CacheServletWrapper.java:87)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:940)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.WSWebContainer.handleRequest 
(WSWebContainer.java:1817)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.webcontainer.channel.WCChannelLink.ready   
(WCChannelLink.java:200)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination 
(HttpInboundLink.java:463)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest 
(HttpInboundLink.java:530)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest 
(HttpInboundLink.java:316)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete 
(HttpICLReadCallback.java:88)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.ssl.channel.impl.SSLReadServiceContext   
$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1820)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted 
(AioReadCompletionListener.java:175)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AbstractAsyncFuture.invokeCallback 
(AbstractAsyncFuture.java:217)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AsyncChannelFuture.fireCompletionActions 
(AsyncChannelFuture.java:161)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler.runEventProcessingLoop 
(ResultHandler.java:775)
2017-07-14 11:03:09.340 |   at 
com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
2017-07-14 11:03:09.340 |   at 
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881)

如果我们得到它们很好,我只是不希望它们写入日志,因为它会导致日志文件比它们应该大得多。我想知道是否有人有有效的方法来解决这个问题,因为我似乎被卡住了。提前谢谢。

编辑: 你在想这样的事情@jmehrens?

public void flushBuffer() throws IOException {
if (stream != null) {
stream.flush();
}
}

一种选择是将报告此错误的记录器的记录器级别设置为OFF。 但是,这可能会隐藏要查看的其他错误。

更好的选择是在报告此错误的文件追加器上配置 EvaluatorFilter 或编写自己的筛选器。

使用给我的jmehrens的建议,我将以下代码添加到GZIPResponseWrapper.java类中:

public void flushBuffer() throws IOException {
if (stream != null) {
stream.flush();
}
}

这会防止 flushBuffer 在给定空指针异常时运行,这反过来会阻止它写入系统日志。虽然这不是最好的解决方案,但它是一个很好的临时解决方案,可以防止日志文件填满。

相关内容

  • 没有找到相关文章

最新更新