在现有的spring-boot应用程序中,在单独的线程中运行无限循环

问题描述 投票:0回答:1

我有一个具有以下主要类的现有spring-boot服务(托管许多api)。>>

@SpringBootApplication
public class BusinessRules {

    public static void main(String[] args) {
        SpringApplication.run(BusinessRules.class, args);
    }
}

作为一项新要求,我需要在同一服务的不同线程中(通过无限循环)使用来自两个不同SQS队列的SQS消息。我可以简单地使用ExecutorService添加两个新线程。像这样的东西:

@SpringBootApplication
public class BusinessRules {

    public static void main(String[] args) {
        SpringApplication.run(BusinessRules.class, args);
        //new code
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Runnable runnable1 = new SQSMessageProcessor1(); //infinite loop in run method
        Runnable runnable2 = new SQSMessageProcessor2(); //infinite loop in run method
        executor.execute(runnable1);
        executor.execute(runnable2);
    }
}

以上代码或任何其他更好的替代方案是否有问题。

我有一个具有以下主要类的现有spring-boot服务(托管许多api)。 @SpringBootApplication公共类BusinessRules {公共静态void main(String [] args){...

java multithreading spring-boot asynchronous executorservice
1个回答
1
投票

一般来说,spring提供了对异步作业的支持(完整文档here:]

© www.soinside.com 2019 - 2024. All rights reserved.