views:

96

answers:

4

Good evening,

I am in the process of writing a Java Servlet (Struts 2, Tomcat, JSP etc) which is capable of doing some fairly complex simulations. These can take up to 2 minutes to complete on the and will return a graph of the results. It is trivial to calculate the percentage of the simulation completed because the process works by repeating the same calculations 1000s of times.

I would be interested to know if anyone has ever tried to use client side technology to provide any estimate of the percentage complete. I.e query the servlet processing to get the number of cycles completed at various point throughout the simulation. This could then be displayed as a bar in the client browser.

Any thoughts, advice, resources would be much appreciated.

Thanks,

Alex

A: 

AJAX is the way to go here.

duffymo
+4  A: 

In your database, have a table to maintain a list of simulations along with their server-calculated progress.

In your web-application, use AJAX to query the server every few seconds (1-20 depending on load is what I'd go with) and update a graphical progress bar. Most javascript libraries have simple timer-based AJAX functions to do exactly this sort of thing.

There's a few details to figure out, such as whether or not completed simulations remain in the DB (could be useful logging info), but overall, this should be fairly simple.

Ben S
Thank you for your response. There is no database backing this application (yet!). The calculation is done entirely in memory. For now I intend to return an XY plot to show the results. Eventually I will provide some way for a user to download the result data.
Alex
+4  A: 

You could encapsulate your response in a mime/multipart message and sends your updates until you have a full response done.

Bugzilla uses this in their search to show "Searching ..."-screen until the searchresult is done.

ZeissS
+1 this has the benefit of almost real-time progress updates (considering a decent connection), however, the down-side is if the site has high traffic, each request will sit on the resources for 2 minutes and scalability could be an issue.
Anurag
Another downside is, that this does now work with AJAX since ajax is only notified when the request is completed.
ZeissS
+2  A: 

If you want to use plain Struts2, you should take a look at the ExecuteAndWait Interceptor.

It works by the old refresh-with-timeout method. Sure, it has lower coolness factor than some AJAX thing, but it's simple and works (I've used it).

Struts2 takes care (by using this interceptor) of executing the action method (which would typically take a long time) in a separate thread, and it returns a special result wait until the work is completed. You just have to code a jsp that shows some "waiting..." message for this result, and make it refresh to the same action, repeatedly, with (say) two or three seconds of timeout. The jsp has access to the action properties, of course, hence you can code some getProgress() method to show a progress message or bar.

leonbloy