Browse Source

增加生产封面图socket

xiewenjie 3 years ago
parent
commit
b5cb494768

+ 15 - 2
sxz-core/src/main/java/com/fdkk/sxz/other/listener/RunBuild.java

@@ -1424,12 +1424,15 @@ public class RunBuild {
 
         switch (uploadType) {
             case "1":
+                redisUtil.setEx("createImgStatus:id:" + modelId, "biz", 5, TimeUnit.MINUTES);
                 endProductHandler(fileId, userId, modelId, dataJson, taskType, uploadType, filePath);
                 break;
             case "2":
+                redisUtil.setEx("createImgStatus:id:" + modelId, "biz", 5, TimeUnit.MINUTES);
                 customHandler(fileId, userId, modelId, dataJson, taskType, uploadType, filePath);
                 break;
             case "3":
+                redisUtil.setEx("componentCreateImgStatus:id:" + modelId, "biz", 5, TimeUnit.MINUTES);
                 componentHandler(fileId, userId, modelId, dataJson, taskType, uploadType, filePath);
                 break;
             default:
@@ -1484,9 +1487,11 @@ public class RunBuild {
             componentModelUploadEntity.setThumStatus(1);
             componentModelUploadEntity.setProgress(100);
             componentModelUploadService.updateById(componentModelUploadEntity);
+            redisUtil.setEx("componentCreateImgStatus:id:" + modelId, "done", 5, TimeUnit.MINUTES);
 
         } catch (Exception e) {
             RunBuild.log.error("生成封面失败-{}", e);
+            redisUtil.setEx("componentCreateImgStatus:id:" + modelId, "error", 5, TimeUnit.MINUTES);
             componentModelUploadEntity = new ComponentModelUploadEntity();
             componentModelUploadEntity.setId(Long.valueOf(modelId));
             componentModelUploadEntity.setThumStatus(-1);
@@ -1526,6 +1531,7 @@ public class RunBuild {
             modelUploadEntity.setThumStatus(1);
             modelUploadEntity.setProgress(100);
             modelUploadService.updateById(modelUploadEntity);
+            redisUtil.setEx("createImgStatus:id:" + modelId, "done", 5, TimeUnit.MINUTES);
 
 
         } catch (Exception e) {
@@ -1533,7 +1539,10 @@ public class RunBuild {
             modelUploadEntity = new ModelUploadEntity();
             modelUploadEntity.setId(Long.valueOf(modelId));
             modelUploadEntity.setThumStatus(-1);
+            modelUploadEntity.setThumProgress(0);
             modelUploadService.updateById(modelUploadEntity);
+            redisUtil.setEx("createImgStatus:id:" + modelId, "error", 5, TimeUnit.MINUTES);
+
         }
     }
 
@@ -1560,6 +1569,7 @@ public class RunBuild {
             modelUploadEntity.setThumStatus(1);
             modelUploadEntity.setProgress(100);
             modelUploadService.updateById(modelUploadEntity);
+            redisUtil.setEx("createImgStatus:id:" + modelId, "done", 5, TimeUnit.MINUTES);
 
 
         } catch (Exception e) {
@@ -1567,7 +1577,10 @@ public class RunBuild {
             modelUploadEntity = new ModelUploadEntity();
             modelUploadEntity.setId(Long.valueOf(modelId));
             modelUploadEntity.setThumStatus(-1);
+            modelUploadEntity.setThumProgress(0);
             modelUploadService.updateById(modelUploadEntity);
+            redisUtil.setEx("createImgStatus:id:" + modelId, "error", 5, TimeUnit.MINUTES);
+
         }
     }
 
@@ -1621,12 +1634,12 @@ public class RunBuild {
                     if (uploadType.equals("3")) {
                         ComponentModelUploadEntity componentModelUploadEntity = new ComponentModelUploadEntity();
                         componentModelUploadEntity.setId(Long.valueOf(modelId));
-                        componentModelUploadEntity.setProgress(objectJson.getInteger("progress"));
+                        componentModelUploadEntity.setThumProgress(objectJson.getInteger("progress"));
                         componentModelUploadService.updateById(componentModelUploadEntity);
                     } else {
                         ModelUploadEntity modelUploadEntity = new ModelUploadEntity();
                         modelUploadEntity.setId(Long.valueOf(modelId));
-                        modelUploadEntity.setProgress(objectJson.getInteger("progress"));
+                        modelUploadEntity.setThumProgress(objectJson.getInteger("progress"));
                         modelUploadService.updateById(modelUploadEntity);
                     }
                 }

+ 209 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/ComponentCreateImgSocket.java

@@ -0,0 +1,209 @@
+package com.fdkk.sxz.webApi.socket;
+
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.fdkk.sxz.entity.ComponentModelUploadEntity;
+import com.fdkk.sxz.util.RedisUtil;
+import com.fdkk.sxz.webApi.service.IComponentModelUploadService;
+import io.netty.handler.codec.http.HttpHeaders;
+import io.netty.handler.timeout.IdleStateEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.yeauty.annotation.*;
+import org.yeauty.pojo.Session;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author XieWj
+ */
+@ServerEndpoint(path = "/wss/componentCreateImgWSS/", port = "${ws.port}")
+@Slf4j
+public class ComponentCreateImgSocket {
+
+    @Value("${main.url}")
+    private String mainUrl;
+
+
+    private static Map<String, ComponentCreateImgSocket> componentSocketMap = new ConcurrentHashMap<>();
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    @Autowired
+    private IComponentModelUploadService componentModelUploadService;
+
+    Thread thread = null;
+
+    static boolean isStop = true;
+
+    private String region = "customWSS";
+
+
+    @BeforeHandshake
+    public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap) {
+        String num = pathMap.get("num");
+        String token = pathMap.get("token");
+        Map<String, String> paramsMap = new HashMap();
+        paramsMap.put("num", num);
+        Map<String, String> postHeaders = new HashMap();
+        postHeaders.put("token", token);
+        session.setSubprotocols("stomp");
+//        JSONObject req=OkHttpUtils.httpPostFormReturnJson(mainUrl + "api/scene/isLogin", paramsMap, postHeaders);
+        if (1 == 0) {
+            ComponentCreateImgSocket.log.info("Authentication failed!");
+            session.close();
+        }
+    }
+
+    @OnOpen
+    public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
+        ComponentCreateImgSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
+        session.sendText("onOpen" + session.id().toString());
+    }
+
+    @OnClose
+    public void onClose(Session session) throws IOException, InterruptedException {
+        ComponentCreateImgSocket.log.info("one connection closed");
+        if (thread != null) {
+            Thread.sleep(1000);
+            thread.interrupt();
+        }
+        session.sendText("onClose" + session.id().toString());
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) {
+        throwable.printStackTrace();
+        if (thread != null) {
+            thread.interrupt();
+        }
+    }
+
+    public void toDo(Session session, String message) throws InterruptedException {
+        while (true) {
+            JSONObject data = JSONUtil.parseObj(message);
+            Long id = data.getLong("id");
+            Thread.sleep(3000);
+            ComponentCreateImgSocket.log.warn("查询" + session.id() + "|----------" + id);
+            if (session.isOpen()) {
+                String status = data.getStr("status");
+                if (StrUtil.equals(status, "query")) {
+                    if (redisUtil.hasKey("componentCreateImgStatus:id:" + id)) {
+                        String ObjStatus = redisUtil.get("componentCreateImgStatus:id:" + id);
+                        if (StrUtil.equals(ObjStatus, "done") || StrUtil.equals(ObjStatus, "error")) {
+                            extracted(session, data, id, ObjStatus);
+                            return;
+                        }
+                    } else {
+                        ComponentModelUploadEntity entity = componentModelUploadService.findById(id);
+                        Thread.sleep(2000);
+                        if (ObjectUtil.isNotNull(entity)) {
+                            if (entity.getThumStatus() == 1) {
+                                extracted(session, data, id, "done");
+                                redisUtil.setEx("componentCreateImgStatus:id:" + id, "done", 5, TimeUnit.MINUTES);
+                                return;
+                            } else if (entity.getStatus() == -1) {
+                                extracted(session, data, id, "error");
+                                redisUtil.delete("componentCreateImgStatus:id:" + id);
+                                return;
+                            }
+                        } else {
+                            return;
+                        }
+                    }
+
+                    if (Thread.currentThread().isInterrupted()) {
+                        ComponentCreateImgSocket.log.info("i has isInterrupted");
+                        return;
+                    }
+                }
+            } else {
+                //连接关闭直接return
+                ComponentCreateImgSocket.isStop = false;
+                ComponentCreateImgSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+                return;
+            }
+        }
+    }
+
+    private void extracted(Session session, JSONObject data, Long id, String status) {
+        JSONObject res = JSONUtil.createObj();
+        JSONObject resData = JSONUtil.createObj();
+        resData.set("id", id);
+        resData.set("status", status);
+        res.set("msg", "done");
+        res.set("data", resData);
+        res.set("code", 0);
+        session.sendText(res.toString());
+        //连接关闭直接return
+        ComponentCreateImgSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+        Thread.yield();
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message) throws InterruptedException {
+        ComponentCreateImgSocket.log.info(message);
+        JSONObject res = JSONUtil.createObj();
+        res.set("code", 0);
+        //业务处理 调用异步线程,处理成功返回消息,连接断开,销毁线程
+        if (JSONUtil.isJson(message)) {
+            JSONObject data = JSONUtil.parseObj(message);
+            res.set("msg", "is Json");
+            ComponentCreateImgSocket.isStop = true;
+            if (!ComponentCreateImgSocket.componentSocketMap.containsKey("id" + data.getInt("id") + "session" + session.id())) {
+                ComponentCreateImgSocket.componentSocketMap.put("id" + data.getInt("id") + "session" + session.id(), this);
+                thread = new Thread(() -> {
+                    try {
+                        toDo(session, message);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+
+                });
+                thread.start();
+            }
+        } else {
+            res.set("code", 400);
+            res.set("msg", "Not for Json");
+        }
+        session.sendText(res.toString());
+        session.sendText(session.id().toString());
+    }
+
+
+    @OnBinary
+    public void onBinary(Session session, byte[] bytes) {
+        for (byte b : bytes) {
+            System.out.println(b);
+        }
+        session.sendBinary(bytes);
+    }
+
+    @OnEvent
+    public void onEvent(Session session, Object evt) {
+        if (evt instanceof IdleStateEvent) {
+            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
+            switch (idleStateEvent.state()) {
+                case READER_IDLE:
+                    ComponentCreateImgSocket.log.info("read idle");
+                    break;
+                case WRITER_IDLE:
+                    ComponentCreateImgSocket.log.info("write idle");
+                    break;
+                case ALL_IDLE:
+                    ComponentCreateImgSocket.log.info("all idle");
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}

+ 209 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/CreateImgSocket.java

@@ -0,0 +1,209 @@
+package com.fdkk.sxz.webApi.socket;
+
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.fdkk.sxz.entity.ModelUploadEntity;
+import com.fdkk.sxz.util.RedisUtil;
+import com.fdkk.sxz.webApi.service.IModelUploadService;
+import io.netty.handler.codec.http.HttpHeaders;
+import io.netty.handler.timeout.IdleStateEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.yeauty.annotation.*;
+import org.yeauty.pojo.Session;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author XieWj
+ */
+@ServerEndpoint(path = "/wss/createImgWSS/", port = "${ws.port}")
+@Slf4j
+public class CreateImgSocket {
+
+    @Value("${main.url}")
+    private String mainUrl;
+
+
+    private static Map<String, CreateImgSocket> componentSocketMap = new ConcurrentHashMap<>();
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    @Autowired
+    private IModelUploadService modelUploadService;
+
+    Thread thread = null;
+
+    static boolean isStop = true;
+
+    private String region = "customWSS";
+
+
+    @BeforeHandshake
+    public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap) {
+        String num = pathMap.get("num");
+        String token = pathMap.get("token");
+        Map<String, String> paramsMap = new HashMap();
+        paramsMap.put("num", num);
+        Map<String, String> postHeaders = new HashMap();
+        postHeaders.put("token", token);
+        session.setSubprotocols("stomp");
+//        JSONObject req=OkHttpUtils.httpPostFormReturnJson(mainUrl + "api/scene/isLogin", paramsMap, postHeaders);
+        if (1 == 0) {
+            CreateImgSocket.log.info("Authentication failed!");
+            session.close();
+        }
+    }
+
+    @OnOpen
+    public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
+        CreateImgSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
+        session.sendText("onOpen" + session.id().toString());
+    }
+
+    @OnClose
+    public void onClose(Session session) throws IOException, InterruptedException {
+        CreateImgSocket.log.info("one connection closed");
+        if (thread != null) {
+            Thread.sleep(1000);
+            thread.interrupt();
+        }
+        session.sendText("onClose" + session.id().toString());
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) {
+        throwable.printStackTrace();
+        if (thread != null) {
+            thread.interrupt();
+        }
+    }
+
+    public void toDo(Session session, String message) throws InterruptedException {
+        while (true) {
+            JSONObject data = JSONUtil.parseObj(message);
+            Long id = data.getLong("id");
+            Thread.sleep(3000);
+            CreateImgSocket.log.warn("查询" + session.id() + "|----------" + id);
+            if (session.isOpen()) {
+                String status = data.getStr("status");
+                if (StrUtil.equals(status, "query")) {
+                    if (redisUtil.hasKey("createImgStatus:id:" + id)) {
+                        String ObjStatus = redisUtil.get("createImgStatus:id:" + id);
+                        if (StrUtil.equals(ObjStatus, "done") || StrUtil.equals(ObjStatus, "error")) {
+                            extracted(session, data, id, ObjStatus);
+                            return;
+                        }
+                    } else {
+                        ModelUploadEntity entity = modelUploadService.findById(id);
+                        Thread.sleep(2000);
+                        if (ObjectUtil.isNotNull(entity)) {
+                            if (entity.getThumStatus() == 1) {
+                                extracted(session, data, id, "done");
+                                redisUtil.setEx("createImgStatus:id:" + id, "done", 5, TimeUnit.MINUTES);
+                                return;
+                            } else if (entity.getStatus() == -1) {
+                                extracted(session, data, id, "error");
+                                redisUtil.delete("createImgStatus:id:" + id);
+                                return;
+                            }
+                        } else {
+                            return;
+                        }
+                    }
+
+                    if (Thread.currentThread().isInterrupted()) {
+                        CreateImgSocket.log.info("i has isInterrupted");
+                        return;
+                    }
+                }
+            } else {
+                //连接关闭直接return
+                CreateImgSocket.isStop = false;
+                CreateImgSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+                return;
+            }
+        }
+    }
+
+    private void extracted(Session session, JSONObject data, Long id, String status) {
+        JSONObject res = JSONUtil.createObj();
+        JSONObject resData = JSONUtil.createObj();
+        resData.set("id", id);
+        resData.set("status", status);
+        res.set("msg", "done");
+        res.set("data", resData);
+        res.set("code", 0);
+        session.sendText(res.toString());
+        //连接关闭直接return
+        CreateImgSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+        Thread.yield();
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message) throws InterruptedException {
+        CreateImgSocket.log.info(message);
+        JSONObject res = JSONUtil.createObj();
+        res.set("code", 0);
+        //业务处理 调用异步线程,处理成功返回消息,连接断开,销毁线程
+        if (JSONUtil.isJson(message)) {
+            JSONObject data = JSONUtil.parseObj(message);
+            res.set("msg", "is Json");
+            CreateImgSocket.isStop = true;
+            if (!CreateImgSocket.componentSocketMap.containsKey("id" + data.getInt("id") + "session" + session.id())) {
+                CreateImgSocket.componentSocketMap.put("id" + data.getInt("id") + "session" + session.id(), this);
+                thread = new Thread(() -> {
+                    try {
+                        toDo(session, message);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+
+                });
+                thread.start();
+            }
+        } else {
+            res.set("code", 400);
+            res.set("msg", "Not for Json");
+        }
+        session.sendText(res.toString());
+        session.sendText(session.id().toString());
+    }
+
+
+    @OnBinary
+    public void onBinary(Session session, byte[] bytes) {
+        for (byte b : bytes) {
+            System.out.println(b);
+        }
+        session.sendBinary(bytes);
+    }
+
+    @OnEvent
+    public void onEvent(Session session, Object evt) {
+        if (evt instanceof IdleStateEvent) {
+            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
+            switch (idleStateEvent.state()) {
+                case READER_IDLE:
+                    CreateImgSocket.log.info("read idle");
+                    break;
+                case WRITER_IDLE:
+                    CreateImgSocket.log.info("write idle");
+                    break;
+                case ALL_IDLE:
+                    CreateImgSocket.log.info("all idle");
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}