lxw
2023-05-18 0f9060e0d97582f2e6794addc5fd9c8dbba32594
src/main/java/com/whyc/webSocket/TaskManageAdminWebsocket.java
@@ -4,7 +4,6 @@
import com.whyc.dto.Response;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.FaultUpload;
import com.whyc.pojo.UserInf;
import com.whyc.service.FaultUploadService;
import com.whyc.util.ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;
@@ -51,9 +50,9 @@
    @OnMessage
    public void onMessage(Session session, String message) {
        UserInf user = (UserInf) this.httpSession.getAttribute("user");
        final int userId = user.getUId().intValue();
        //final int userId = 1041;
        /*UserInf user = (UserInf) this.httpSession.getAttribute("user");
        final int userId = user.getUId().intValue();*/
        final int userId = 1041;
        FaultUpload upload = ActionUtil.getGson().fromJson(message, FaultUpload.class);
        thread = new Thread("Thread_RealTime") {
            @Override
@@ -62,12 +61,38 @@
                    Thread thread = currentThread();
                    threadFlagMap.put(thread.getId(), true);
                    try {
                        Map<String, Response> map = getTaskMAdmin(userId, upload);
                        if (session.isOpen()) {
                            //推送信息
                            synchronized (session) {
                                try {
                                session.getBasicRemote().sendObject(new Response().set(1, map));
                            }
                            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);
    }
    //获取管理员的数据
    public Map getTaskMAdmin(int userId, FaultUpload upload) {
                                    Map<String, Object> res = new HashMap<>();
                                    while (!currentThread().isInterrupted()) {
        try {
                                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                                        CountDownLatch latch = new CountDownLatch(5);
                                        poolExecutor.execute(() -> {
@@ -101,35 +126,11 @@
                                            latch.countDown();
                                        });
                                        latch.await(10, TimeUnit.MINUTES);
                                        session.getBasicRemote().sendObject(new Response().set(1, res));
                                        sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
                                    }
                                } catch (Exception e) {
                                    this.interrupt();
        return 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) {