package com.whyc.webSocket; import com.whyc.dto.Response; import com.whyc.factory.ThreadPoolExecutorFactory; import com.whyc.service.*; 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; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * packageName com.whyc.websocket * * @author lxw * @version JDK 8 * @className BattSocket (此处以class为例) * @date 2024/6/15 * @description */ @Component @ServerEndpoint(value = "/batt", encoders = WebSocketEncoder.class) public class BattSocket { private Session session; private Thread thread; private volatile boolean runFlag = true; private volatile Map threadMap = new HashMap<>(); private static final int executeTime = 5000; private static RtStateService rtStateService; private static RtDataService rtDataService; private static BattAlarmService battAlarmService; private static RtEnvirmentService rtEnvirmentService; private volatile Map threadFlagMap = new HashMap<>(); @Autowired public void setRtStateService(RtStateService rtStateService) { BattSocket.rtStateService = rtStateService; } @Autowired public void setRtDataService(RtDataService rtDataService) { BattSocket.rtDataService = rtDataService; } @Autowired public void setBattAlarmService(BattAlarmService battAlarmService) { BattSocket.battAlarmService = battAlarmService; } @Autowired public void setRtEnvirmentService(RtEnvirmentService rtEnvirmentService) { BattSocket.rtEnvirmentService = rtEnvirmentService; } @OnOpen public void onOpen(Session session) { this.session = session; } @OnMessage public void onMessage(Session session, String message) { int binfId=Integer.valueOf(message); thread = new Thread("Thread_BattSocket") { @Override public void run() { Map res = new HashMap<>(); while (runFlag && !isInterrupted()) { Thread thread = currentThread(); threadFlagMap.put(thread.getId(), true); try { ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor(); CountDownLatch latch = new CountDownLatch(4); poolExecutor.execute(() -> { Response resRtState = rtStateService.getResRtState(binfId); res.put("resRtState", resRtState); latch.countDown(); }); poolExecutor.execute(() -> { Response resRtData = rtDataService.getResRtData(binfId); res.put("resRtData", resRtData); latch.countDown(); }); poolExecutor.execute(() -> { Response resBattAlm = battAlarmService.getResBattAlm(binfId); res.put("resBattAlm", resBattAlm); latch.countDown(); }); poolExecutor.execute(() -> { Response resRtEnvir = rtEnvirmentService.getResRtEnvir(); res.put("resRtEnvir", resRtEnvir); latch.countDown(); }); latch.await(10, TimeUnit.MINUTES); if (session.isOpen()) { //推送信息 synchronized (session) { session.getBasicRemote().sendObject(new Response().set(1, res)); } threadFlagMap.put(thread.getId(), false); } sleep(executeTime); } 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()); } }