package com.warewms.hailiang.init; import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.ObjectUtil; import com.warewms.common.exception.ServiceException; import com.warewms.hailiang.connect.base.TCPConnectBase; import com.warewms.hailiang.service.DeviceService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.IOException; import java.util.HashMap; import java.util.Set; /** * Created with IntelliJ IDEA. * * @author: liuzhifei * Date: 2023/8/9 * Time: 14:44 * To change this template use File | Settings | File Templates. * Description:tcp服务连接启动类 **/ @Component @Order(2) @Slf4j public class TcpServiceRunner implements CommandLineRunner { @Autowired ThreadPoolTaskExecutor threadPoolTaskExecutor; @Resource DeviceService deviceService; private HashMap TCPInstanceList = new HashMap<>(); @Override public void run(String... args) throws Exception { Set> classes = ClassUtil.scanPackageBySuper("com.warewms.hailiang.connect", TCPConnectBase.class); for (Class aClass : classes) { Object o = aClass.newInstance(); if (o instanceof TCPConnectBase) { TCPConnectBase nodeModel = (TCPConnectBase) o; // threadPoolTaskExecutor.execute(() -> { // try { // nodeModel.init(); // TCPInstanceList.put(nodeModel.getDeviceName(),nodeModel); // } catch (IOException e) { // nodeModel.close(); // throw new RuntimeException(e); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // }); } } } public TCPConnectBase getTCPInstanceList(String key){ TCPConnectBase tcpConnectBase = TCPInstanceList.get(key); if(ObjectUtil.isNull(tcpConnectBase)){ throw new ServiceException("设备未连接"); } if (!deviceService.IsConnect(key)){ throw new ServiceException("设备连接中断"); } return TCPInstanceList.get(key); } }