Tomcat - do things after response
I have a servlet for the purposes of uploading my application's database in android.
The database is processed on the server side but that could take a while.
At the moment, I'm just returning back with a response that the upload has been successful using PrintWriter.write().
Is there a way after the response has been sent to the client(my android app) to start processing the file?
If I send a redirect to another servlet or forward the request using the dispatcher, I won't have a response until the servlet that carries out the processing is finished.
What's the best way, to respond to the user instantly,let the processing of the database begin and send the results back to android (note th开发者_开发百科is could take 5 minutes or 10 minutes, depending on the database - so I'm assuming I would need some sort of listener to poll on the server whether the results have been processed and ready to be shown on the client)
The backend processing can be done via a job, e.g. via Quartz. If the server could be replaced with JBoss AS, Glassfish or Resin, a simple @Asynchronous annotated method could also do the job here.
You can indeed poll the server continuously to ask if it's already done. This is what the majority of the AJAX based progress bars in a web environment also do.
Alternatively you could make use of reverse AJAX (Comet) to prevent the polling (see e.g. http://atmosphere.java.net/). In that case the server is able to push a response back to you as soon as it's done. This does necessitate some kind of eventing on your server. If you could replace the server with JBoss AS or Glassfish, the CDI event bus could be an option. Otherwise, a shared object in the HTTP session on which you call wait/notify is a crude alternative.
精彩评论