Here is the problem
A user of an enterprise web application is performing a task that results in a long (very long) database query (or other long processing intensive task)
Problems:
There are several solutions I came up with, but I'm not sure I know which is better (in all aspects, peformance, best practice, elegance and maintainability) and I would like to know what is your recommended solution, and if there is a solution that I missed? (probably yes, and many)
The bad solution: use the request thread as a worker thread,save progress state in the session, have an AJAX call check the status (in the session) in another paralel request
The compromise solution: create your own thread pool, handle a monitoring thread, a worker thread and take care of clustering by syncronizing the states in either a distrubuted transactional cache or persistant storage. this releases the request, but creats threads the application server is not aware of, and won't close in undeployment. Its up to you to shutdown threads in a clean way, and there is always a chance you will end up leaking something. This is not the J2EE way to do it either.
The J2EE solution: use JMS for the asynchronous task, this is what it's ment for
the Spring solution: use Spring batch
What would you do / did in your projects? What other solutions you know? which of the ones I noted above is the winner in your opinion?
I would do a combination of your so called "bad solution" and the "j2ee solution":
The trick is to match the request / response pair. I would do it like this:
NULL
as a value. Also pass that id to the UI.NULL
.This is clean and well abstracted from any concrete domain. A purely technical solution.
Even if you don't like polling, HTTP is stateless by design and I think this way polling only happens at well defined intervals.
Anyway, I implemented a system with exactly this pattern and it runs just fine...
The solution I have used before involves Jetty Cometd instead of AJAX. The main difference between Ajax and Cometd is that Cometd can uses more of a pub/sub model - the client (web browser in this case) subsribes to the publisher (your app) and the app pushes out updates and notifications to the web browser as apposed to an ajax model of constant polling of the server by the web browser. You can make use of the Cometd solution even if you are not using jetty - you can drop the jetty jars into the lib folder of your respective web server and you should be good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With