1

I have a Java HttpServlet. This servlet contains a set of objects that make use of the observer pattern in order to return data through the servlet's Response object. Here is a simplified version of my doGet() method in the HttpServlet:

protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
    MyProcess process = new MyProcess();
    // This following method spawns a few threads, so I use a listener to receive a completion event.
    process.performAsynchronousMethod(request, new MyListener() {
        public void processComplete(data) {
            response.getWriter().print(data.toString());
        }
    }
}

As the example shows, I have a process that I execute, which spawns a variety of threads in order to produce a final dataset. This process can take anywhere from seconds to a minute. My problem is, it appears that as the doGet() method completes, the response object becomes null. When processComplete() is called, the response object will be null - thus preventing me from writing any data out.

It appears as if the servlet is closing the connection as soon as the asynchronous method is called.

Is there a better way to implement this type of servlet when using the observer pattern for asynchronous tasks? Should I do this in another way?

Patrick D
  • 6,659
  • 3
  • 43
  • 55

2 Answers2

3

The servlet response will be sent back to the client when the doGet method terminates, it won't wait for your asynchronous call to finish as well. You will need to find a way to block until all your asynchronous tasks have completed, and only then allow the doGet() method to return.

The answers to this question should point you in the right direction.

Something else to watch out for is that you have no guarantee that the threads will write to the response writer in series, you may find that the various print operations overlap and the output will be garbled (this may not matter to you, depending on what the data is, and how it will be used)

Community
  • 1
  • 1
codebox
  • 19,927
  • 9
  • 63
  • 81
  • This worked for me. Thanks! I just pass the latch in as a parameter to my `performAsynchronousMethod()` and countDown whenever it completes. – Patrick D Apr 05 '13 at 13:11
1

You could try asynchronous servlets available in spec version 3.0, not all web servers support it, only some modern. But it means that server will hold socket connection for this amount of time. So, you should know how many clients could be connected simultaneously, not all hardware/operation system could handle a lot of open connections.

And web client will wait, and could have a timeout. You should also consider a situation that socket connection could be disconnected and client will never get result (e.g. some proxy servers break long running connections). So you should allow "resume" operation.

kan
  • 28,279
  • 7
  • 71
  • 101
  • I don't think this is an option for me, with my current web server. If it supported it, I would definitely look into this option. It seems like the more elegant way to do things than holding a latch. Thanks! – Patrick D Apr 05 '13 at 13:13