【发布时间】:2018-08-30 20:43:55
【问题描述】:
我想通过 Spring Boot 使用 TaskExecutor 运行几个长任务。我想让多个 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 multithreading spring-boot threadpoolexecutor