zj
2025-06-10 879a75e26e94d766e893e47d65a0b239e04ce94a
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package project.data.websocket.utils;
 
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
 
import okhttp3.ConnectionPool;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import project.data.websocket.exception.SDKException;
 
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class ConnectionFactory {
 
    private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE;
 
    private static LinkedBlockingQueue<NetworkLatency> LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>();
 
    private static ConnectionPool connectionPool = new ConnectionPool(20, 300, TimeUnit.SECONDS);
 
    private static final OkHttpClient client = new OkHttpClient.Builder().followSslRedirects(false)
            .followRedirects(false).connectTimeout(5000, TimeUnit.MILLISECONDS).readTimeout(5000, TimeUnit.MILLISECONDS)
            .writeTimeout(5000, TimeUnit.MILLISECONDS).connectionPool(connectionPool)
            .addNetworkInterceptor(new Interceptor() {
                @NotNull
                @Override
                public Response intercept(@NotNull Chain chain) throws IOException {
                    Request request = chain.request();
 
                    Long startNano = System.nanoTime();
 
                    Response response = chain.proceed(request);
 
                    Long endNano = System.nanoTime();
 
                    if (LATENCY_DEBUG_SWATCH) {
                        LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano));
                    }
 
                    return response;
                }
            }).build();
 
    private static final Logger log = LoggerFactory.getLogger(ConnectionFactory.class);
 
    public static String execute(Request request) {
 
        Response response = null;
        String str = null;
        try {
            log.debug("[Request URL]{}", request.url());
            response = client.newCall(request).execute();
            if (response.code() != 200) {
                throw new SDKException(SDKException.EXEC_ERROR,
                        "[Execute] Response Status Error : " + response.code() + " message:" + response.message());
            }
            if (response != null && response.body() != null) {
                str = response.body().string();
                response.close();
            } else {
                throw new SDKException(SDKException.ENV_ERROR, "[Execute] Cannot get the response from server");
            }
            log.debug("[Response]{}", str);
            return str;
        } catch (IOException e) {
            e.printStackTrace();
            throw new SDKException(SDKException.RUNTIME_ERROR, "[Execute] Cannot get the response from server");
        }
 
    }
 
    public static WebSocket createWebSocket(Request request, WebSocketListener listener) {
        return client.newWebSocket(request, listener);
    }
 
    public static void setLatencyDebug() {
        LATENCY_DEBUG_SWATCH = Boolean.TRUE;
    }
 
    public static LinkedBlockingQueue<NetworkLatency> getLatencyDebugQueue() {
        return LATENCY_DEBUG_QUEUE;
    }
 
    public static void clearLatencyDebugQueue() {
        LATENCY_DEBUG_QUEUE.clear();
    }
 
    public static class NetworkLatency {
 
        private String path;
 
        private Long startNanoTime;
 
        private Long endNanoTime;
 
        public NetworkLatency(String path, Long startNanoTime, Long endNanoTime) {
            this.path = path;
            this.startNanoTime = startNanoTime;
            this.endNanoTime = endNanoTime;
        }
 
        public String getPath() {
            return path;
        }
 
        public void setPath(String path) {
            this.path = path;
        }
 
        public Long getStartNanoTime() {
            return startNanoTime;
        }
 
        public void setStartNanoTime(Long startNanoTime) {
            this.startNanoTime = startNanoTime;
        }
 
        public Long getEndNanoTime() {
            return endNanoTime;
        }
 
        public void setEndNanoTime(Long endNanoTime) {
            this.endNanoTime = endNanoTime;
        }
 
    }
}