Kaynağa Gözat

线程采集

985653680@qq.com 1 yıl önce
ebeveyn
işleme
b8aa4d19e9

+ 3 - 5
src/main/java/com/gzlh/api/OpenApi.java

@@ -18,11 +18,9 @@ public class OpenApi {
 
     @RequestMapping("test")
     public ResultJson test(){
-        eventBus.start("LED_SHOW_CONTENT");
-        eventBus.start("LED_SHOW_CONTENT");
-        eventBus.start("LED_SHOW_CONTENT");
-        eventBus.start("LED_SHOW_CONTENT");
-        eventBus.start("LED_SHOW_CONTENT");
+        eventBus.start("PLC.LED_READ");
+        eventBus.start("LED.LED_READ");
+
         return ResultJson.success();
     }
 }

+ 28 - 1
src/main/java/com/gzlh/event/EventBus.java

@@ -1,15 +1,42 @@
 package com.gzlh.event;
 
 import com.gzlh.dto.EventDTO;
+import com.gzlh.infrared.config.RedPropertiesConfig;
+import com.gzlh.infrared.factory.RedFactory;
+import com.gzlh.led.factory.LedFactory;
+import com.gzlh.led.properties.LedPropertiesConfig;
+import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig;
+import com.gzlh.weighbridge.factory.WeighbridgeFactory;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 
+import javax.annotation.Resource;
 import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 
 @Component
 @Slf4j
 public class EventBus {
     private List<EventDTO> eventDTOList;
+    @Resource
+    private LedFactory ledFactory;
+    @Resource
+    private RedFactory redFactory;
+    @Resource
+    WeighbridgeFactory weighbridgeFactory;
+
+    @Resource
+    private WeighbridgePropertiesConfig weighbridgePropertiesConfig;
+    @Resource
+    private RedPropertiesConfig redPropertiesConfig;
+    @Resource
+    private LedPropertiesConfig ledPropertiesConfig;
+
+    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
+            10L, TimeUnit.SECONDS,
+            new LinkedBlockingQueue(100));
 
     public void setEventDTOList(List<EventDTO> eventDTOList) {
         this.eventDTOList = eventDTOList;
@@ -18,6 +45,6 @@ public class EventBus {
     public void start(String eventName){
         EventThread thread = new EventThread(eventName);
         thread.setEventDTOList(eventDTOList);
-        thread.start();
+        threadPoolExecutor.execute(thread);
     }
 }

+ 10 - 18
src/main/java/com/gzlh/event/EventThread.java

@@ -2,8 +2,8 @@ package com.gzlh.event;
 
 import cn.hutool.core.thread.ThreadUtil;
 import cn.hutool.core.util.NumberUtil;
-import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
 import com.gzlh.config.ModuleEnum;
 import com.gzlh.dto.EventDTO;
 import com.gzlh.infrared.config.RedPropertiesConfig;
@@ -13,34 +13,25 @@ import com.gzlh.led.properties.LedPropertiesConfig;
 import com.gzlh.weighbridge.config.WeighbridgePropertiesConfig;
 import com.gzlh.weighbridge.factory.WeighbridgeFactory;
 import lombok.extern.slf4j.Slf4j;
-
-import javax.annotation.Resource;
-import java.util.Date;
 import java.util.List;
 
 @Slf4j
-public class EventThread extends Thread{
+public class EventThread implements Runnable{
     private List<EventDTO> eventDTOList;
     private String eventName;
-    @Resource
-    private LedFactory ledFactory;
-    @Resource
-    private RedFactory redFactory;
-    @Resource
-    WeighbridgeFactory weighbridgeFactory;
 
-    @Resource
-    private WeighbridgePropertiesConfig weighbridgePropertiesConfig;
-    @Resource
-    private RedPropertiesConfig redPropertiesConfig;
-    @Resource
-    private LedPropertiesConfig ledPropertiesConfig;
+    private LedFactory ledFactory = SpringUtil.getBean(LedFactory.class);
+    private RedFactory redFactory = SpringUtil.getBean(RedFactory.class);
+    private WeighbridgeFactory weighbridgeFactory = SpringUtil.getBean(WeighbridgeFactory.class);
+
+    private WeighbridgePropertiesConfig weighbridgePropertiesConfig = SpringUtil.getBean(WeighbridgePropertiesConfig.class);
+    private RedPropertiesConfig redPropertiesConfig = SpringUtil.getBean(RedPropertiesConfig.class);
+    private LedPropertiesConfig ledPropertiesConfig = SpringUtil.getBean(LedPropertiesConfig.class);
 
     public EventThread() {
     }
 
     public EventThread(String eventName) {
-        this.setName(eventName);
         this.eventName=eventName;
     }
 
@@ -105,6 +96,7 @@ public class EventThread extends Thread{
         }
     }
 
+    @Override
     public void run(){
         handlerEvent(eventName);
     }