Java Servlet - Observer Pattern causing null Response object -


i have java httpservlet. servlet contains set of objects make use of observer pattern in order return data through servlet's response object. here simplified version of doget() method in httpservlet:

protected void doget(final httpservletrequest request, final httpservletresponse response)     myprocess process = new myprocess();     // following method spawns few threads, use listener receive completion event.     process.performasynchronousmethod(request, new mylistener() {         public void processcomplete(data) {             response.getwriter().print(data.tostring());         }     } } 

as example shows, have process execute, spawns variety of threads in order produce final dataset. process can take anywhere seconds minute. problem is, appears doget() method completes, response object becomes null. when processcomplete() called, response object null - preventing me writing data out.

it appears if servlet closing connection asynchronous method called.

is there better way implement type of servlet when using observer pattern asynchronous tasks? should in way?

the servlet response sent client when doget method terminates, won't wait asynchronous call finish well. need find way block until asynchronous tasks have completed, , allow doget() method return.

the answers this question should point in right direction.

something else watch out have no guarantee threads write response writer in series, may find various print operations overlap , output garbled (this may not matter you, depending on data is, , how used)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -