后台工作者的Spring Boot应用程序 [英] Spring boot application for background workers
问题描述
我已经定义了以下Spring Boot应用程序:
I have defined the following Spring boot application:
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
}
public void run(String... args) throws Exception {
Thread.currentThread().join();
}
}
我还有一组工作程序(即可以是实现Runnable
的类).它们应该无限期地运行.
I also have a package of workers (i.e. could be classes which implements Runnable
). They are supposed to run indefinitely.
运行它们的春季方式"是什么? (并且自动执行,而无需明确知道它们)
What is the "Spring way" to run them? (and doing so automatically, without explicitly knowing them)
谢谢
推荐答案
您可以(1)用@Component注释实现Runnable的类,以便Spring可以找到它们.然后,您可以(2)用@Service编写带有@Autowired列表的WorkManager,该列表将由(1)中所有类的列表实例进行初始化.并且可以(3)在WorkManager中编写一个@PostConstruct方法,该方法将遍历该Runnable列表,然后将每个传递给TaskExecutor来运行...
You could (1) have your classes that implement Runnable be annotated with @Component, so that Spring can find them. You can then (2) write a WorkManager, annotated with @Service, with an @Autowired List - which Spring would initialize with a list instances of all your classes from (1). And could (3) write a @PostConstruct method in your WorkManager that would iterate over that list of Runnables, and pass each one to a TaskExecutor to run...
这篇关于后台工作者的Spring Boot应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!