Can anyone teach me or direct to a working example to satisfy this requirement.
Scenario:
- List item My Web App is using spring mvc.
- One of the services it provides is that when the user clicks on a button a long running process will occur on the server. (Query database, write files, write logs, etc…) this process can take a few seconds or a few minutes.
-
*Problem***
How can I implement the service to update the client of its progress.
-
The service returns true or false if the process was successful.
Thanks for your replies. A code snippet or a complete tutorial will be most helpful.
There are a good number of ways to handle a scenario like this. One way is to model the work in terms of a “Process”, which contains a “status”, including a percentage completion.
If you imagine what this might look like on a website, clicking the button to start the process would submit a form that begins the process and assigns some sort of identity to the process, almost like if you were creating any other sort of object. It would then redirect you to a “process status” page.
The process status page would query for the status of the process and display it. It’d probably have a URL parameter for the process’s ID. It would perhaps update itself using an AJAX call to return a progress percentage.
On the backend, you now need to solve a couple of problems: finding out the current status of process N, and updating the status of process N. You could accomplish this in a number of ways, including storing the progress in the database or having some sort of in-memory table of running jobs. You could also use some sort of heuristic to estimate a percent. For example, if it’s a “register new user” job, maybe it’s 20% done if the user’s table has an email address, 40% done if the user avatar table has data in it for this user, etc. I don’t recommend this as much.