lxw
2023-09-05 5b398a743e06f47381bf33f580491d6f68e2d85c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.whyc.webSocket;
 
import com.google.gson.reflect.TypeToken;
import com.whyc.dto.Login;
import com.whyc.factory.ThreadPoolExecutorFactory;
import com.whyc.pojo.Response;
import com.whyc.service.CarCameraService;
import com.whyc.util.ActionUtil;
import com.whyc.util.WebSocketEncoderUtil;
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.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
@Component
@ServerEndpoint(value = "/carCameraSocket",encoders = WebSocketEncoderUtil.class)
public class CarCameraWebSocket {
    private volatile Thread thread;
 
    private static final int executeTime = 5000;
 
    private volatile boolean runFlag=true;
 
    private volatile Map<String,Thread> threadMap = new HashMap<>();
 
    private volatile Map<Long,Boolean> threadFlagMap = new HashMap<>();
 
    private static CarCameraService service;
 
    private Session session;
 
    @Autowired
    public void setService(CarCameraService service){
        CarCameraWebSocket.service = service;
    }
 
    @OnOpen
    public void onOpen(Session session){
        this.session=session;
    }
 
    @OnMessage
    public void onMessage(Session session, String message){
        List<Login> list= ActionUtil.getGson().fromJson(message,new TypeToken<List<Login>>(){}.getType());
        thread = new Thread("Thread_airSocket") {
            @Override
            public void run() {
                try {
                    Map<String, Object> map = new HashMap<>();
                    while (!currentThread().isInterrupted()) {
                        Thread thread = currentThread();
                        threadFlagMap.put(thread.getId(), true);
                        ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                        CountDownLatch latch = new CountDownLatch(list.size());
                        for (Login login:list) {
                            poolExecutor.execute(() -> {
                                Response res=service.getCarCamera(login);
                                map.put(login.getIp(),res);
                                latch.countDown();
                            });
                        }
                        latch.await(10, TimeUnit.MINUTES);
                        session.getBasicRemote().sendObject(new Response().set(1, map));
                        sleep(5000);
                    }
                } catch (Exception e) {
                    this.interrupt();
                }
            }
        };
        thread.start();
        this.thread = 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());
    }
}