Ror*_*oro 6 spring multithreading threadpoolexecutor spring-boot
我想使用 TaskExecutor 使用 Spring Boot 运行几个长任务。我想让多个 rfid 阅读器连续运行并获取项目标签。然后我需要使用这些标签来更新数据库。到目前为止,我还没有能够使用 Spring Boot 使任务运行一次以上。这可能吗?
我正在创建一个@Component 阅读器类。创建时,我启动侦听器服务和侦听器以跟踪标签。我有 startMessageService 和 setReaderConfig 方法。第一种方法我启动消息侦听器服务以在阅读器进入时接收带有标签信息的消息。第二种方法设置阅读器自主读取,因此当标签经过阅读器时,阅读器将消息发送到听众和我收到消息。
run 方法基本上让读者在消息进来时继续阅读。但由于某种原因,代码没有按照我想要的方式工作。只要读者有标签,我就会收到一条消息,而我没有。
底部是我的 threadpooltaskexecutor bean。
我不确定我错过了什么。
@Component
@Scope("prototype")
public class AlienReader extends AlienClass1Reader implements 
TagTableListener, MessageListener, Runnable{
 private String ipaddress;
 private int port;
 private String username;
 private String password;
 int serviceport;
public AlienReader(String ipaddress, int port, String username, String pwd, 
int serviceport) throws UnknownHostException, AlienReaderException, 
InterruptedException{
    super(ipaddress, port);
    this.ipaddress=ipaddress;
    this.port=port;
    this.username=username;
    this.password=pwd;
    this.serviceport=serviceport;
    startMessageService();
    setReaderConfig();
}
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(15);
    executor.setMaxPoolSize(42);
    executor.setQueueCapacity(11);
    executor.setThreadNamePrefix("threadPoolExecutor-");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
}
Spring Boot 没有强加任何限制;如果你可以用 Java 或其他语言来完成,你应该可以用 Java + Spring Boot 来完成。
什么是 Spring Boot
需要明确的是,Spring Boot 基本上只是一个构建 Spring 项目的框架,它在历史上非常复杂,并且有很多样板代码。可以制作spring boot命令行应用或者web应用(比较正常)。我相信存在更多选择,并且也会出现。
因此,Spring Boot 将通过轻松公开 applicaiton.properties 文件中的配置、自动连接所有 spring bean、设置控制器的连接等方式为您提供一个良好的开端。但它不会限制您编写的代码;它只是减轻了它。
你的问题
您可以使用执行程序服务来运行任意数量的长时间运行的任务,只要您的 PC/服务器可以处理它们。
这是一个工作 spring boot 示例,它并行或一分钟运行两个不同的任务。它们可以永远持续下去并做任何事情,您可以轻松地将其扩展到数百个任务。
import org.apache.catalina.core.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
        App app = run.getBean(App.class);
        app.run();
    }
    private void run() {
        Runnable r1 = () -> {
            for(int i = 0; i < 30; ++i) {
                System.out.println("task 1");
                try {Thread.sleep(1000);} catch(Exception ignored) {}
            }
        };
        Runnable r2 = () -> {
            for(int i = 0; i < 30; ++i) {
                System.out.println("task 2");
                try {Thread.sleep(1000);} catch(Exception ignored) {}
            }
        };
        //Create an executor service with 2 threads (it can be like 50
        //if you need it to be).  Submit our two tasks to it and they'll
        //both run to completion (or forever if they don't end).
        ExecutorService service = Executors.newFixedThreadPool(2);
        service.submit(r1);
        service.submit(r2);
        //Wait or completion of tasks (or forever).
        service.shutdown();
        try { service.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS); }
        catch (InterruptedException e) { e.printStackTrace(); }
    }
}
更多信息
| 归档时间: | 
 | 
| 查看次数: | 6323 次 | 
| 最近记录: |