zouyu
9 天以前 bf95078dab81dcd0639fdb1a41e998b31c940fb1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.chinaztt.mes.docx.config;
 
import com.chinaztt.mes.docx.handler.SerialPortListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
/**
 * 项目启动完成后执行串口监听
 */
@Slf4j
@Component
public class SerialPortStartupRunner implements ApplicationRunner {
 
    @Value("${serialPort.enable}")
    public Boolean enableSerialPort;
 
    @Resource
    private SerialPortListener serialPortListenerService;
 
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
    @Override
    public void run(ApplicationArguments args) {
        log.info("===== SpringBoot 项目启动完成,开始初始化串口监听 =====");
        // 启动串口监听(核心调用)
        if(enableSerialPort)threadPoolTaskExecutor.execute(() -> serialPortListenerService.serialPortDataListener());
    }
}