TcpServiceRunner.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.warewms.hailiang.init;
  2. import cn.hutool.core.util.ClassUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.warewms.common.exception.ServiceException;
  5. import com.warewms.hailiang.connect.base.TCPConnectBase;
  6. import com.warewms.hailiang.service.DeviceService;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.CommandLineRunner;
  10. import org.springframework.core.annotation.Order;
  11. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  12. import org.springframework.stereotype.Component;
  13. import javax.annotation.Resource;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16. import java.util.Set;
  17. /**
  18. * Created with IntelliJ IDEA.
  19. *
  20. * @author: liuzhifei
  21. * Date: 2023/8/9
  22. * Time: 14:44
  23. * To change this template use File | Settings | File Templates.
  24. * Description:tcp服务连接启动类
  25. **/
  26. @Component
  27. @Order(2)
  28. @Slf4j
  29. public class TcpServiceRunner implements CommandLineRunner {
  30. @Autowired
  31. ThreadPoolTaskExecutor threadPoolTaskExecutor;
  32. @Resource
  33. DeviceService deviceService;
  34. private HashMap<String,TCPConnectBase> TCPInstanceList = new HashMap<>();
  35. @Override
  36. public void run(String... args) throws Exception {
  37. Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.warewms.hailiang.connect", TCPConnectBase.class);
  38. for (Class<?> aClass : classes) {
  39. Object o = aClass.newInstance();
  40. if (o instanceof TCPConnectBase) {
  41. TCPConnectBase nodeModel = (TCPConnectBase) o;
  42. // threadPoolTaskExecutor.execute(() -> {
  43. // try {
  44. // nodeModel.init();
  45. // TCPInstanceList.put(nodeModel.getDeviceName(),nodeModel);
  46. // } catch (IOException e) {
  47. // nodeModel.close();
  48. // throw new RuntimeException(e);
  49. // } catch (InterruptedException e) {
  50. // throw new RuntimeException(e);
  51. // }
  52. // });
  53. }
  54. }
  55. }
  56. public TCPConnectBase getTCPInstanceList(String key){
  57. TCPConnectBase tcpConnectBase = TCPInstanceList.get(key);
  58. if(ObjectUtil.isNull(tcpConnectBase)){
  59. throw new ServiceException("设备未连接");
  60. }
  61. if (!deviceService.IsConnect(key)){
  62. throw new ServiceException("设备连接中断");
  63. }
  64. return TCPInstanceList.get(key);
  65. }
  66. }