java Spring Boot 线程管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33703520/
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-11-02 22:01:09  来源:igfitidea点击:

Spring Boot Thread management

javamultithreadingspringspring-mvcspring-boot

提问by rogerpt

I am barely new with Spring framework and i would like to ask opinion of the experts.

我对 Spring 框架几乎不陌生,我想请教专家的意见。

It is a spring boot application with a rest end point, which everytime is called it will put an action on a queue that will be consumed by a thread.

它是一个带有休息端点的 Spring Boot 应用程序,每次调用该端点时,它都会将一个操作放在一个队列上,该队列将被线程使用。

The way I am organizing my code is:

我组织代码的方式是:

Application class A runnable class. A component class.

应用程序类 一个可运行的类。一个组件类。

The component class has the annotatiion @Component and it only contains an instance for the thread.

组件类有注解@Component,它只包含线程的一个实例。

@Component
public class ComponenteExample {
    @Autowired
    Runnable runnableImpl;
    Thread thread;

    @PostConstruct
    private void init(){
       thread = new thread(runnableImpl);
       thread.start();
    }

What I would like to ask if there is a better/elegant way to manage this thread. By this I mean if it could be Spring container to manage it?

我想问一下是否有更好/优雅的方式来管理这个线程。我的意思是,它是否可以由 Spring 容器来管理?

回答by bsmk

For asynchronous call you can use https://spring.io/guides/gs/async-method/

对于异步调用,您可以使用https://spring.io/guides/gs/async-method/

However if you want to use queues you should look at https://spring.io/guides/gs/messaging-jms

但是,如果您想使用队列,您应该查看https://spring.io/guides/gs/messaging-jms

And for event-driven application there is https://spring.io/guides/gs/messaging-reactor/

对于事件驱动的应用程序,有https://spring.io/guides/gs/messaging-reactor/