Spring Boot线程管理
问题描述:
我几乎不熟悉Spring框架,我想询问专家的意见。Spring Boot线程管理
这是一个带有休息终点的弹簧启动应用程序,每调用一次,它都会将一个操作放入一个线程将要消耗的队列中。
我安排我的代码的方式是:
应用类 可运行的类。 一个组件类。
组件类有annotatiion @Component,它只包含一个线程实例。
@Component
public class ComponenteExample {
@Autowired
Runnable runnableImpl;
Thread thread;
@PostConstruct
private void init(){
thread = new thread(runnableImpl);
thread.start();
}
我想什么询问是否有管理这个线程更好的/优雅的方式。我的意思是,如果它可能是Spring容器来管理它?
答
对于异步调用,您可以使用https://spring.io/guides/gs/async-method/
但是,如果你想使用的队列,你应该看看https://spring.io/guides/gs/messaging-jms
而对于事件驱动的应用程序有https://spring.io/guides/gs/messaging-reactor/
使用的执行? (并使用构造函数注入来代替字段注入;它更易于管理。) – chrylis
如果此线程/ runnable构造函数的总体用途是什么?如果你想要一个异步工作者,你真的可以在'void'(或'Future')方法上使用'@ Async'注解,并且会弹出一个神奇的功能,包括线程和队列。在bsmk的答案中也有http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async。 [反应堆](https://spring.io/blog/2013/05/13/reactor-a-foundation-for-asynchronous-applications-on-the-jvm)将是精心制作的异步处理系统的框架。 – zapl
感谢您的回复。其目的是根据请求处理XML,但处理过程应该是异步的,并且由于内存限制,一次只能处理一个XML(如果有新的请求到达,则应该放在队列中)。 – rogerpt