whyclxw
2024-08-16 e1c7a6792d7f67c42be2084af0131949f8476e4f
实时推送
1个文件已修改
36 ■■■■ 已修改文件
src/main/java/com/whyc/webSocket/BattSocket.java 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/BattSocket.java
@@ -6,6 +6,7 @@
import com.whyc.pojo.RtState;
import com.whyc.pojo.StationInf;
import com.whyc.service.*;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -51,6 +52,8 @@
    private static RtEnvirmentService rtEnvirmentService;
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
    @Autowired
    public void setRtStateService(RtStateService rtStateService) {
@@ -80,12 +83,14 @@
    @OnMessage
    public void onMessage(Session session, String message) {
        int binfId=Integer.valueOf(message);
        Thread thread = new Thread() {
        thread = new Thread("Thread_RealTime") {
            @Override
            public void run() {
                try {
                while (runFlag && !isInterrupted()) {
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    Map<String, Object> res = new HashMap<>();
                    while (!currentThread().isInterrupted()) {
                    try {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(4);
                        poolExecutor.execute(() -> {
@@ -109,20 +114,35 @@
                            latch.countDown();
                        });
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, res));
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                session.getBasicRemote().sendObject(new Response().set(1, res));
                            }
                            threadFlagMap.put(thread.getId(), false);
                        }
                        sleep(executeTime);
                    } catch (Exception e) {
                        interrupt();
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
        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) {
    public void onClose(CloseReason closeReason){
        System.err.println("closeReason = " + closeReason);
        runFlag = false;
        if (thread != null && thread.isAlive()) {