HttpServletResponse Writer error in LinkedBlockingQueue in separate Thread
I have a servlet that can process a request and send a response. Needing to process many simultaneous requests, I started using a LinkedBlockingQueue in a separate thread to take requests and process them. The problem comes when I have finished processing and want to send a response to the client. I get a NullPointerException when trying to .flush() or .close() the java.io.Writer associated with the HttpServletResponse.
Exception in thread"Thread-36" java.lang.NullPointerException
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:747)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:432)
at org.apache.coyote.http11.InternalOutputBuffer.flush(InternalOutputBuffer.java:305)
at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:992)
at org.apache.coyote.Response.action(Response.java:183)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:322)
at org.apache.catalina.connector.OutputBuffer.flush(OutputBuffer.java:293)
at org.apache.catalina.connector.CoyoteWriter.flush(CoyoteWriter.java:95)
at com.interrupt.bookkeeping.http.BookkeepingSystemFacade.perform(BookkeepingSystemFacade.java:343)
at com.interrupt.bookkeeping.http.BookkeepingSystemFacade.run(BookkeepingSystemFacade.java:97)
at java.lang.Thread.run(Thread.java:613)
What I do is, in the servlet, put the HttpServletRequest and HttpServletResponse into ServletInputParams, a class I made with 2 fields to hold the values (a typed bag), and add that class to the queue in the separate thread. In that separate thread, I take the ServletInputParams, get the request and response and start processing. All processing and parameter access in the request works fine, but sending a response using the HttpServletResponse's java.io.Writer gives me the error abouve. Again, this exact same processing works fine if I don't pass the HttpServletResponse between threads.
Has anyone tackled this problem before? I am using javac 1.5.0_07 and this code is running in Tomcat 5.5. Below is a snippet from the separate thread class, 'BookkeepingSystemFacade' that accepts the servlet parameters. Thanks for any help.
publicsynchronizedvoid addToQueue(ServletInputParams servlParams){
System.out.println(">> PUTTING into the queue > "+ servlParams);
try{
servletQueue.put(servlParams);
}
...
}
publicvoid run(){
System.out.println(">> BookkeepingSystemFacade is RUNNING > ");
try{
ServletInputParams siparams = (ServletInputParams)this.servletQueue.take();
while(siparams !=null){
System.out.println(">> TAKING from queue > "+ siparams);
HttpServletRequest sreq = siparams.req;
HttpServletResponse sres = siparams.res;
this.perform(sreq, sres);
siparams = (ServletInputParams)this.servletQueue.take();
}
System.out.println(">> FINISHING thread > ");
}
...
}
publicsynchronizedvoid perform(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String cmdValue = req.getParameter("cmd");
String tokenValue = req.getParameter("token");
String dataValue = req.getParameter("data");
String idValue = req.getParameter("id");
...
IResult result =new GResult();
...
//** return
resp.setContentType("text/xml");
Writer writer = resp.getWriter();
writer.write(result.toXML());
writer.flush();
writer.close();
}

