如何在Spring TaskExecutor中分配任务名称并检查它是否仍然存在?
以免考虑我有以下内容:
public class MyRunnable implements Runnable {
public void run() {
//do something expensive
}
}
public class ThreadExecutor {
private TaskExecutor taskExecutor;
public ThreadExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void fireThread(){
taskExecutor.execute(new MyRunnable());
}
}
我的 xml 如下:
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
<bean id="threadExecutor" class="com.vanilla.threads.controllers.ThreadExecutor">
<constructor-arg ref="taskExecutor" />
</bean>
在我的 Spring MVC 控制器中,我启动任务:
@RequestMapping(value="/startTask.html", method=RequestMethod.GET)
public ModelAndView indexView(){
ModelAndView mv = new ModelAndView("index");
threadExecutor.fireThread();
return mv;
}
现在让我们考虑我想创建另一个请求(@RequestMapping(value="/checkStatus.html"
)这将告诉我之前的请求中启动的任务是否已经完成
所以我的问题很简单:
1)我可以为TaskExecutor中的任务分配名称吗?如果可以,我该怎么做?
2)如何检查具有该特定名称的任务是否已完成?
Lest's consider that I have the following:
public class MyRunnable implements Runnable {
public void run() {
//do something expensive
}
}
public class ThreadExecutor {
private TaskExecutor taskExecutor;
public ThreadExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void fireThread(){
taskExecutor.execute(new MyRunnable());
}
}
my xml is the following:
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
<bean id="threadExecutor" class="com.vanilla.threads.controllers.ThreadExecutor">
<constructor-arg ref="taskExecutor" />
</bean>
in my Spring MVC controller I start the task:
@RequestMapping(value="/startTask.html", method=RequestMethod.GET)
public ModelAndView indexView(){
ModelAndView mv = new ModelAndView("index");
threadExecutor.fireThread();
return mv;
}
Now let's consider that I would like to create another request(@RequestMapping(value="/checkStatus.html"
) which will tell if the the task started in my previous Request has been finished.
So my questions are simple:
1) Can I assign name to the task in TaskExecutor and if yes, how can I do it?
2) How can I check that the task with that specific name has been done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)不,但是..
不要使用
taskExecutor.execute()
,而是使用taskExecutor.submit()
,它返回一个Future
。将Future
放入HttpSession
中,当 checkStatus 请求传入时,从会话中提取Future
并调用isDone()就可以了。如果您需要给它一个名称,那么不要直接将
Future
放入会话中,而是在会话中使用Map
,其中键是名称你的任务。1) No, but..
Instead of using
taskExecutor.execute()
usetaskExecutor.submit()
which returns aFuture
. Put theFuture
in theHttpSession
, and when a checkStatus request comes in, pull theFuture
from the session and callisDone()
on it. If you need to give it a name then instead of putting theFuture
in the session directly have aMap<String, Future>
in the session where the key is the name of your task.