ty130316261 лет назад: 3
Родитель
Сommit
920e6b6f26

+ 6 - 4
zd-modules/zd-netty/src/main/java/com/zd/netty/init/NettyStartListener.java

@@ -3,6 +3,7 @@ package com.zd.netty.init;
 import com.zd.common.core.constant.HttpStatus;
 import com.zd.common.core.domain.R;
 import com.zd.common.core.enums.HardwareOperate;
+import com.zd.common.core.utils.SpringUtils;
 import com.zd.netty.service.IFridService;
 import com.zd.netty.thread.ThreadPoolTaskConfig;
 import com.zd.system.api.laboratory.RemoteLaboratoryService;
@@ -11,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
@@ -29,16 +31,16 @@ public class NettyStartListener implements ApplicationRunner {
     private IFridService fridService;
     @Resource
     private RemoteLaboratoryService laboratoryService;
-    @Autowired
-    private SocketServer socketServer;
     @Resource
-    private ThreadPoolTaskConfig threadPoolTaskConfig;
+    private SocketServer socketServer;
+
+    private final ThreadPoolTaskExecutor taskExecutor= SpringUtils.getBean("taskExecutor");
 
     @Override
     public void run(ApplicationArguments args) {
         socketServer.start();
 
-        threadPoolTaskConfig.getAsyncExecutor().execute(() -> {
+        taskExecutor.execute(() -> {
             synchronized (this) {
                 R<List<RemoteLabHardware>> listStart = laboratoryService.listStart();
                 while (listStart.getCode() != HttpStatus.SUCCESS) {

+ 46 - 52
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDService.java

@@ -5,6 +5,7 @@ import com.gg.reader.api.dal.HandlerTagEpcLog;
 import com.gg.reader.api.dal.HandlerTagEpcOver;
 import com.gg.reader.api.protocol.gx.*;
 import com.zd.common.core.exception.ServiceException;
+import com.zd.common.core.utils.SpringUtils;
 import com.zd.netty.service.ISendService;
 import com.zd.netty.service.IService;
 import com.zd.netty.thread.ThreadPoolTaskConfig;
@@ -12,13 +13,15 @@ import com.zd.system.api.domain.InventoryTag;
 import com.zd.system.api.laboratory.domain.RemoteLabHardware;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.Hashtable;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 @Slf4j
 @Service
@@ -29,8 +32,7 @@ public class DeJuRFIDService implements IService {
 
     private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
 
-    @Resource
-    private ThreadPoolTaskConfig threadPoolTaskConfig;
+    private final ScheduledExecutorService scheduledExecutorService= SpringUtils.getBean("scheduledExecutorService");
 
     @Override
     public void start(RemoteLabHardware hardware) {
@@ -66,10 +68,14 @@ public class DeJuRFIDService implements IService {
                         log.info("Power configuration successful.");
                     } else {
                         log.info("Power configuration error.");
+                        disconnect(hardware);
+                        start(hardware);
                     }
                 }
             } else {
                 log.info("Power configuration error.");
+                disconnect(hardware);
+                start(hardware);
             }
 
             //蜂鸣器设置
@@ -107,6 +113,8 @@ public class DeJuRFIDService implements IService {
                 log.info("Inventory epc successful.");
             } else {
                 log.info("Inventory epc error.");
+                disconnect(hardware);
+                start(hardware);
             }
         } else {
             throw new ServiceException("Connect failure.");
@@ -118,22 +126,7 @@ public class DeJuRFIDService implements IService {
         String ipAddress = hardware.getIpAddress();
         if (clientMap.containsKey(ipAddress)) {
             GClient client = clientMap.get(ipAddress);
-            MsgAppSetGpo msgAppSetGpo=new MsgAppSetGpo();
-            msgAppSetGpo.setGpo1(0);
-            msgAppSetGpo.setGpo2(0);
-            msgAppSetGpo.setGpo3(0);
-            msgAppSetGpo.setGpo4(0);
-            msgAppSetGpo.setGpo5(0);
-            msgAppSetGpo.setGpo6(0);
-            msgAppSetGpo.setGpo7(0);
-            msgAppSetGpo.setGpo8(0);
-
-            client.sendSynMsg(msgAppSetGpo);
-            if (0 == msgAppSetGpo.getRtCode()) {
-                log.info("Gpo epc stop successful.");
-            } else {
-                log.error("Gpo epc stop error.");
-            }
+            changeGpo(0, client);
             MsgBaseStop msg = new MsgBaseStop();
             // 停止读卡,空闲态
             client.sendSynMsg(msg);
@@ -148,6 +141,36 @@ public class DeJuRFIDService implements IService {
         }
     }
 
+    private void changeGpo(int state, GClient client) {
+        MsgAppSetGpo msgAppSetGpo=new MsgAppSetGpo();
+        msgAppSetGpo.setGpo1(state);
+        msgAppSetGpo.setGpo2(state);
+        msgAppSetGpo.setGpo3(state);
+        msgAppSetGpo.setGpo4(state);
+        msgAppSetGpo.setGpo5(state);
+        msgAppSetGpo.setGpo6(state);
+        msgAppSetGpo.setGpo7(state);
+        msgAppSetGpo.setGpo8(state);
+
+        client.sendSynMsg(msgAppSetGpo);
+        String status = state == 1 ? "start" : "stop";
+        if (0 == msgAppSetGpo.getRtCode()) {
+            log.info("Gpo epc {} successful.",status);
+        } else {
+            log.error("Gpo epc {} error.",status);
+        }
+    }
+
+    @Async
+    public void stopGpo(GClient client){
+        scheduledExecutorService.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                changeGpo(0, client);
+            }
+        },10, TimeUnit.SECONDS);
+    }
+
     /**
      * 订阅6c标签信息上报
      *
@@ -158,37 +181,8 @@ public class DeJuRFIDService implements IService {
             if (logBaseEpcInfo.getResult() == 0) {
                 log.info("===========》{}",logBaseEpcInfo.getbEpc());
                 //灯带设置
-                MsgAppSetGpo msgAppSetGpo=new MsgAppSetGpo();
-                msgAppSetGpo.setGpo1(1);
-                msgAppSetGpo.setGpo2(1);
-                msgAppSetGpo.setGpo3(1);
-                msgAppSetGpo.setGpo4(1);
-                msgAppSetGpo.setGpo5(1);
-                msgAppSetGpo.setGpo6(1);
-                msgAppSetGpo.setGpo7(1);
-                msgAppSetGpo.setGpo8(1);
-                client.sendSynMsg(msgAppSetGpo);
-                if (0 == msgAppSetGpo.getRtCode()) {
-                    log.info("Gpo epc successful.");
-                } else {
-                    log.error("Gpo epc error.");
-                }
-                threadPoolTaskConfig.getAsyncExecutor().execute(() -> {
-                    msgAppSetGpo.setGpo1(0);
-                    msgAppSetGpo.setGpo2(0);
-                    msgAppSetGpo.setGpo3(0);
-                    msgAppSetGpo.setGpo4(0);
-                    msgAppSetGpo.setGpo5(0);
-                    msgAppSetGpo.setGpo6(0);
-                    msgAppSetGpo.setGpo7(0);
-                    msgAppSetGpo.setGpo8(0);
-                    client.sendSynMsg(msgAppSetGpo);
-                    if (0 == msgAppSetGpo.getRtCode()) {
-                        log.info("Gpo epc stop successful.");
-                    } else {
-                        log.error("Gpo epc stop error.");
-                    }
-                },8*1000);
+                changeGpo(1, client);
+                stopGpo(client);
                 InventoryTag tag = new InventoryTag();
                 BeanUtils.copyProperties(logBaseEpcInfo, tag);
                 sendService.send(tag);

+ 43 - 2
zd-modules/zd-netty/src/main/java/com/zd/netty/thread/ThreadPoolTaskConfig.java

@@ -1,14 +1,16 @@
 package com.zd.netty.thread;
 
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 
-import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.*;
 
 @Configuration
-@EnableAsync
+@Slf4j
 public class ThreadPoolTaskConfig {
 
     private static final int corePoolSize = 10;       		// 核心线程数(默认线程数)
@@ -32,4 +34,43 @@ public class ThreadPoolTaskConfig {
         executor.initialize();
         return executor;
     }
+
+    /**
+     * 执行定时任务
+     */
+    @Bean(name = "scheduledExecutorService")
+    protected ScheduledExecutorService scheduledExecutorService() {
+        return new ScheduledThreadPoolExecutor(corePoolSize,
+                new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build()) {
+            @Override
+            protected void afterExecute(Runnable r, Throwable t) {
+                super.afterExecute(r, t);
+                printException(r, t);
+            }
+        };
+    }
+
+
+    /**
+     * 打印线程异常信息
+     */
+    public static void printException(Runnable r, Throwable t) {
+        if (t == null && r instanceof Future<?>) {
+            try {
+                Future<?> future = (Future<?>) r;
+                if (future.isDone()) {
+                    future.get();
+                }
+            } catch (CancellationException ce) {
+                t = ce;
+            } catch (ExecutionException ee) {
+                t = ee.getCause();
+            } catch (InterruptedException ie) {
+                Thread.currentThread().interrupt();
+            }
+        }
+        if (t != null) {
+            log.error(t.getMessage(), t);
+        }
+    }
 }