lxw
2022-09-01 a470368d78979efabbe62ae6f5b2121d6ae9d703
历史版本socket
2个文件已添加
2个文件已修改
121 ■■■■■ 已修改文件
src/main/java/com/whyc/controller/ProductController.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/dto/VersionSocketDto.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/service/ProductHistoryService.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/VersionSocket.java 102 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/controller/ProductController.java
@@ -47,8 +47,8 @@
    }
    @ApiOperation(value = "产品详情查看版本信息",notes = "8.17修改后使用")
    @GetMapping("getProductVersion")
    public Response getProductVersion( @RequestParam String parentModel, String customCode){
        return historyService.getProductVersion(parentModel,customCode);
    public Response getProductVersion( @RequestParam String parentCode, String customCode){
        return historyService.getProductVersion(parentCode,customCode);
    }
    @ApiOperation(value = "根据产品id和版本查询子件及其关联的物料信息",notes = "8.17修改后使用")
    @GetMapping("getBomAndMaterial")
src/main/java/com/whyc/dto/VersionSocketDto.java
New file
@@ -0,0 +1,11 @@
package com.whyc.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class VersionSocketDto implements Serializable {
    private String parentModel;
    private String customCode;
    private String parentCode;
}
src/main/java/com/whyc/service/ProductHistoryService.java
@@ -18,9 +18,9 @@
    //产品详情查看版本信息
    public Response getProductVersion(String parentModel,String customCode) {
    public Response getProductVersion(String parentCode,String customCode) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parent_model",parentModel);
        wrapper.eq("parent_code",parentCode);
        wrapper.eq("custom_code",customCode);
        wrapper.orderByDesc("create_time");
        List<ProductHistory> list=mapper.selectList(wrapper);
src/main/java/com/whyc/webSocket/VersionSocket.java
New file
@@ -0,0 +1,102 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.Response;
import com.whyc.dto.VersionSocketDto;
import com.whyc.service.ProductHistoryService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
@Component
@ServerEndpoint(value = "/version",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class VersionSocket {
    private Session session;
    private static final int executeTime = 2000;
    private Thread thread;
    private volatile boolean runFlag=true;
    private volatile Map<String,Thread> threadMap = new HashMap<>();
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    private static ProductHistoryService historyService;
    @Autowired
    public void setHistoryService(ProductHistoryService historyService){
        VersionSocket.historyService=historyService;
    }
    @OnOpen
    public void onOpen(Session session){
        this.session=session;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        this.session=session;
        VersionSocketDto versionDto= ActionUtil.getGson().fromJson(message, VersionSocketDto.class);
        thread = new Thread("Thread_Version") {
            @Override
            public void run() {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Response res = historyService.getProductVersion(versionDto.getParentCode(),versionDto.getCustomCode());
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(res);
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                        //} catch (IOException | InterruptedException | EncodeException e) {
                    } catch (Exception e) {
                        interrupt();
                    }
                }
            }
        };
        thread.start();
        threadFlagMap.put(thread.getId(),true);
        //停止老的socket线程
        Thread threadBefore = threadMap.get(session.getId());
        if(threadBefore !=null && threadBefore.isAlive()){
            while (threadFlagMap.get(threadBefore.getId())){
            }
            threadBefore.interrupt();
        }
        //将线程存储,便于调用定位
        threadMap.put(session.getId(), this.thread);
    }
    @OnClose
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
        if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
        threadMap.remove(session.getId());
    }
}