whyclxw
2025-01-16 6ece8bc329dd681ebf9748bc843895d6941cd9ed
首页推送
2个文件已修改
57 ■■■■ 已修改文件
src/main/java/com/whyc/factory/ThreadPoolExecutorFactory.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/webSocket/HomeSocket.java 55 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/whyc/factory/ThreadPoolExecutorFactory.java
@@ -10,7 +10,7 @@
    private static ThreadPoolExecutor init() {
        //线程池初始化
        UserThreadFactory userThreadFactory = new UserThreadFactory("fg");
        UserThreadFactory userThreadFactory = new UserThreadFactory("bl");
        ThreadPoolExecutor pool = new ThreadPoolExecutor(10, 48, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(16), userThreadFactory,new ThreadPoolExecutor.CallerRunsPolicy());
        poolExecutor = pool;
        return pool;
src/main/java/com/whyc/webSocket/HomeSocket.java
@@ -34,12 +34,7 @@
    private Thread thread;
    private HttpSession httpSession;
    private static AreaInfService ainfService;
    @Autowired
    public void setAreaInfServicee(AreaInfService ainfService) {
@@ -50,20 +45,36 @@
    @OnOpen
    public void onOpen(Session session, EndpointConfig config) {
        this.session = session;
        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
    }
    @OnMessage
    public void onMessage(Session session, String message){
        UserInf user = (UserInf) this.httpSession.getAttribute("user");
        final int userId = user.getUid();
        final int urole = user.getUrole();
        Thread thread = new Thread() {
        //HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
        //UserInf user = (UserInf) httpSession.getAttribute("user");
        //final int userId = user.getUid();
        //final int urole = user.getUrole();
        final int userId = 10001;
        final int urole = 1;
        Thread thread = new Thread("Thread_HomeSocket") {
            @Override
            public void run() {
                try {
                    Map<String, Object> res = new HashMap<>();
                    while (!currentThread().isInterrupted()) {
                        while (!currentThread().isInterrupted()) {
                            Map<String, Object> res = sendHome(userId,urole);
                            session.getBasicRemote().sendObject(new Response().set(1, res));
                            sleep(5000);
                        }
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
    }
    //推送数据
    public Map<String, Object> sendHome(int userId,int urole) {
        Map<String, Object> res = new HashMap<>();
        try {
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(5);
                        poolExecutor.execute(() -> {
@@ -96,17 +107,11 @@
                            res.put("resAllCtlLog", resAllCtlLog);
                            latch.countDown();
                        });
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(res);
                        sleep(5000);
            latch.await(4, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = thread;
        return res;
    }
    @OnClose