| | |
| | | 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; |
| | | |
| | |
| | | |
| | | private static RtEnvirmentService rtEnvirmentService; |
| | | |
| | | private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>(); |
| | | |
| | | |
| | | @Autowired |
| | | public void setRtStateService(RtStateService rtStateService) { |
| | |
| | | @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(() -> { |
| | |
| | | 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()) { |