lxw
2022-08-20 8f898dc3174a2e52d373e7486c0663b64f9abd74
物料分页模糊查询websocket
2个文件已添加
117 ■■■■■ 已修改文件
src/main/java/com/whyc/dto/MaterialSocketDto.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/MaterialSocket.java 104 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/dto/MaterialSocketDto.java
New file
@@ -0,0 +1,13 @@
package com.whyc.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class MaterialSocketDto implements Serializable {
    private String subCode;
    private String subName;
    private String subModel;
    private int pageCurr;
    private int pageSize;
}
src/main/java/com/whyc/webSocket/MaterialSocket.java
New file
@@ -0,0 +1,104 @@
package com.whyc.webSocket;
import com.whyc.config.WebSocketConfig;
import com.whyc.dto.MaterialSocketDto;
import com.whyc.dto.Response;
import com.whyc.service.MaterialService;
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 = "/material",encoders = WebSocketEncoder.class,configurator = WebSocketConfig.class)
public class MaterialSocket {
    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 MaterialService materialService;
    @Autowired
    private void setMaterialService(MaterialService materialService){
        MaterialSocket.materialService=materialService;
    }
    @OnOpen
    public void onOpen(Session session){
        this.session=session;
    }
    @OnMessage
    public void onMessage(Session session, String message){
        this.session=session;
       MaterialSocketDto materialDto= ActionUtil.getGson().fromJson(message, MaterialSocketDto.class);
        thread = new Thread("Thread_RealTime") {
            @Override
            public void run() {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Response res = materialService.getMaterialLimit(materialDto.getSubCode(),materialDto.getSubName(),materialDto.getSubModel(),materialDto.getPageCurr(),materialDto.getPageSize());
                        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());
    }
}