Java 如何在spring webapp中创建后台进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:37:53  来源:igfitidea点击:

How to create background process in spring webapp?

javamultithreadingspringbackground-process

提问by Vladimir

I want to run background process in parallel with my spring-mvc web-application. I need a way to start in automatically on context loading. Background process is a class that implements Runnable. Is spring-mvc has some facilities for that?

我想与我的 spring-mvc web 应用程序并行运行后台进程。我需要一种在上下文加载时自动启动的方法。后台进程是一个实现Runnable. spring-mvc 是否有一些功能?

采纳答案by skaffman

Spring has a comprehensive task execution framework. See the relevant part of the docs.

Spring 有一个全面的任务执行框架。请参阅文档相关部分

I suggest having a Spring bean in your context, which, when initialized, submits your background Runnableto a SimpleAsyncTaskExecutorbean. That's the simplest approach, which you can make more complex and capable as you see fit.

我建议在您的上下文中使用 Spring bean,它在初始化时会将您的背景提交RunnableSimpleAsyncTaskExecutorbean。这是最简单的方法,您可以根据需要将其变得更复杂和更强大。

回答by washley

I would go ahead and look at the task scheduling documentation linked by skaffman, but there's also a simpler way if all you really want to do is fire up a background thread at context initialization time.

我会继续查看 skaffman 链接的任务调度文档,但如果您真正想做的只是在上下文初始化时启动后台线程,还有一种更简单的方法。

<bean id="myRunnableThingy">
  ...
</bean>

<bean id="thingyThread" class="java.lang.Thread" init-method="start">
  <constructor-arg ref="myRunnableThingy"/>
</bean>

回答by Lucas Holt

As another option, one can now use Spring's scheduling capabilities. With Spring 3 or higher, it has a cron like annotation that allows you to schedule tasks to run with a simple annotation of a method. It's also friendly with autowiring.

作为另一种选择,现在可以使用 Spring 的调度功能。在 Spring 3 或更高版本中,它有一个类似 cron 的注释,允许您使用简单的方法注释来安排任务运行。它对自动装配也很友好。

This example schedules a task for every 2 minutes with an initial wait (on startup) of 30 seconds. The next task will run 2 minutes after the method completes! If you want it to run every 2 minutes exactly, use fixedInterval instead.

此示例每 2 分钟安排一个任务,初始等待(启动时)为 30 秒。下一个任务将在方法完成后 2 分钟运行!如果您希望它恰好每 2 分钟运行一次,请改用 fixedInterval。

@Service
public class Cron {
private static Logger log = LoggerFactory.getLogger(Cron.class);

@Autowired
private PageService pageService;

@Scheduled(initialDelay = 30000, fixedDelay=120000)  // 2 minutes
public void cacheRefresh() {
    log.info("Running cache invalidation task");
    try {

        pageService.evict();
    } catch (Exception e) {
        log.error("cacheRefresh failed: " + e.getMessage());
    }
}

}

Be sure to also add @EnableAsync @EnableScheduling to your Application class to enable this feature.

确保还将 @EnableAsync @EnableScheduling 添加到您的 Application 类以启用此功能。