Spring @Async取消并开始吗? [英] Spring @Async cancel and start?
问题描述
我有一个spring MVC应用程序,用户可以在其中通过单击按钮开始生成报告.此过程可能需要几分钟〜10-20分钟.我在服务调用周围使用springs @Async批注,以便异步生成报告.当我向用户弹出一条消息时,指示作业当前正在运行.现在,我想做的是,如果另一个用户(管理员)可以通过按钮启动报告生成,该按钮应取消/停止当前运行的@Async任务并重新启动新任务.为此,我致电
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);
如何使其能够在将来的取消任务杀死所有正在运行的线程的同时让"service.generateReport"等待?根据文档,在我调用future.cancel(true)之后,isDone将返回true,而isCancelled将返回true.因此,无法知道该工作实际上已被取消.
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.
推荐答案
来自 如果此方法返回true,则随后对isCancelled()的调用将始终返回true
尝试一下.
future = getCurrentTask(id); // returns the current task for given report id
if (!future.isDone()){
boolean terminatedImmediately=future.cancel(true);
if(terminatedImmediately)
service.generateReport(id);
else
//Inform user existing job couldn't be stopped.And to try again later
}
这篇关于Spring @Async取消并开始吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!