I have a spring MVC app where a user can kick off a Report generation via button click. This process could take few minutes ~ 10-20 mins. I use springs @Async annotation around the service call so that report generation happens asynchronously. While I pop a message to user indicating job is currently running. Now What I want to do is, if another user (Admin) can kick off Report generation via the button which should cancel/stop currently running @Async task and restart the new task. To do this, I call the
.. ..
future = getCurrentTask(id); // returns the current task for given report id
if (!future.isDone())
future.cancel(true);
service.generateReport(id);
How can make it so that "service.generateReport" waits while the future cancel task kills all the running threads? According to the documentation, after i call future.cancel(true), isDone will return true as well as isCancelled will return true. So there is no way of knowing the job is actually cancelled.
I can only start new report generation when old one is cancelled or completed so that it would not dirty data.