阻止HeartbeatServlet写入nullpointer异常以记录

时间:2017-07-21 11:57:40

标签: java logging heartbeat

我有以下代码:

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) {}
}

当用户退出浏览器而不注销时,我们会将多个心跳空指针异常写入日志。

例外:

[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();
      }
}

2 个答案:

答案 0 :(得分:0)

对于报告此错误的记录器,一个选项是set the logger levelOFF。但是,这可能会隐藏您想要查看的其他错误。

更好的选择是在报告此错误的fileappender上配置EvaluatorFilter或编写自己的过滤器。

答案 1 :(得分:-2)

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

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

如果给出nullpointer异常,这会阻止flushBuffer运行,从而阻止它写入系统日志。虽然这不是最好的解决方案,但它是防止日志文件填满的一个很好的临时解决方案。