ソースを参照

修改 线程异步

cuimengchao 3 年 前
コミット
ff88768445

+ 2 - 0
src/main/java/com/yx/face/netty_client/NettyClient.java

@@ -9,6 +9,7 @@ import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
 import java.util.HashMap;
@@ -41,6 +42,7 @@ public class NettyClient {
      * @return
      * @throws InterruptedException
      */
+    @Async
     @SneakyThrows
     public void sendMessage(String msg) {
         if (!map.containsKey(host)) {

+ 6 - 12
src/main/java/com/yx/face/service/impl/FaceTBServiceImpl.java

@@ -64,8 +64,8 @@ public class FaceTBServiceImpl implements FaceTBService {
     @Autowired
     private NettyClient nettyClient;
 
-    @Autowired
-    private TaskPoolConfig taskPoolConfig;
+//    @Autowired
+//    private TaskPoolConfig taskPoolConfig;
 
     @Override
     public Boolean downFace(Long userId, Date startTime, Date endTime, Integer adminId) {
@@ -666,16 +666,10 @@ public class FaceTBServiceImpl implements FaceTBService {
             if (verifytype == 0 || verifytype == 2) {
                 faceLogDao.insertSelective(faceLog);
             }
-            //开启线程
-            FaceLog finalFaceLog = faceLog;
-            taskPoolConfig.executor().execute(new Runnable() {
-                @Override
-                public void run() {
-                    Map<String, String> map = new HashMap<>(1);
-                    map.put("face-log", JSONObject.toJSONString(finalFaceLog));
-                    nettyClient.sendMessage(JSON.toJSONString(map));
-                }
-            });
+            Map<String, String> maps = new HashMap<>(1);
+            maps.put("face-log", JSONObject.toJSONString(faceLog));
+            nettyClient.sendMessage(JSON.toJSONString(maps));
+
         }
         return result;
     }

+ 3 - 10
src/main/java/com/yx/face/service/impl/UserInfoServiceImpl.java

@@ -399,16 +399,9 @@ public class UserInfoServiceImpl implements UserInfoService {
             userVisitorOne = userVisitorListDao.selectByPrimaryKey(userVisitorList.getId());
         }
         //开启异步线程
-        UserVisitorList finalUserVisitorOne = userVisitorOne;
-        taskPoolConfig.executor().execute(new Runnable() {
-            @Override
-            public void run() {
-                Map<String, String> map = new HashMap<>(1);
-                map.put("user-visitor", JSONObject.toJSONString(finalUserVisitorOne));
-                nettyClient.sendMessage(JSON.toJSONString(map));
-            }
-        });
-
+        Map<String, String> maps = new HashMap<>(1);
+        maps.put("user-visitor", JSONObject.toJSONString(userVisitorOne));
+        nettyClient.sendMessage(JSON.toJSONString(maps));
     }