如何在Spring TaskExecutor中分配任务名称并检查它是否仍然存在?

发布于 2024-12-04 18:34:24 字数 1368 浏览 2 评论 0原文

以免考虑我有以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2024-12-11 18:34:24

1)不,但是..

不要使用taskExecutor.execute(),而是使用taskExecutor.submit(),它返回一个Future。将 Future 放入 HttpSession 中,当 checkStatus 请求传入时,从会话中提取 Future 并调用 isDone()就可以了。如果您需要给它一个名称,那么不要直接将 Future 放入会话中,而是在会话中使用 Map ,其中键是名称你的任务。

1) No, but..

Instead of using taskExecutor.execute() use taskExecutor.submit() which returns a Future. Put the Future in the HttpSession, and when a checkStatus request comes in, pull the Future from the session and call isDone() on it. If you need to give it a name then instead of putting the Future in the session directly have a Map<String, Future> in the session where the key is the name of your task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文