From 4fa37207178491d3180373cf7d9a2982f077599a Mon Sep 17 00:00:00 2001 From: Andrei Kaplanov Date: Fri, 16 Jun 2023 18:43:39 +0500 Subject: [PATCH 1/2] Add OkHttpClientBuilder --- .../client/utils/httpclient/HttpClientSingleton.java | 6 +++--- .../client/utils/httpclient/OkHttpClientBuilder.java | 9 +++++++++ .../httpclient/WebSocketApiHttpClientSingleton.java | 2 +- .../httpclient/WebSocketStreamHttpClientSingleton.java | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java index c784a0d0..99191b9a 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java @@ -12,12 +12,12 @@ private HttpClientSingleton() { private static void createHttpClient(ProxyAuth proxy) { if (proxy == null) { - httpClient = new OkHttpClient(); + httpClient = new OkHttpClient(OkHttpClientBuilder.builder); } else { if (proxy.getAuth() == null) { - httpClient = new OkHttpClient.Builder().proxy(proxy.getProxy()).build(); + httpClient = OkHttpClientBuilder.builder.proxy(proxy.getProxy()).build(); } else { - httpClient = new OkHttpClient.Builder().proxy(proxy.getProxy()).proxyAuthenticator(proxy.getAuth()).build(); + httpClient = OkHttpClientBuilder.builder.proxy(proxy.getProxy()).proxyAuthenticator(proxy.getAuth()).build(); } } } diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java b/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java new file mode 100644 index 00000000..c1062162 --- /dev/null +++ b/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java @@ -0,0 +1,9 @@ +package com.binance.connector.client.utils.httpclient; + +import okhttp3.OkHttpClient; + +import java.time.Duration; + +public class OkHttpClientBuilder { + public static OkHttpClient.Builder builder = new OkHttpClient.Builder().pingInterval(Duration.ofSeconds(10)); +} diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java index 672fb005..f50eb478 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java @@ -3,7 +3,7 @@ import okhttp3.OkHttpClient; public final class WebSocketApiHttpClientSingleton { - private static final OkHttpClient httpClient = new OkHttpClient(); + private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.builder); private WebSocketApiHttpClientSingleton() { } diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java index d2462927..053ad03c 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java @@ -3,7 +3,7 @@ import okhttp3.OkHttpClient; public final class WebSocketStreamHttpClientSingleton { - private static final OkHttpClient httpClient = new OkHttpClient(); + private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.builder); private WebSocketStreamHttpClientSingleton() { } From 39f6adf6a73d16d9cf364ec0eb2359765ac8b711 Mon Sep 17 00:00:00 2001 From: Andrei Kaplanov Date: Tue, 25 Jul 2023 15:51:57 +0500 Subject: [PATCH 2/2] Static initialization of OkHttpClientBuilder --- .../utils/httpclient/HttpClientSingleton.java | 6 +++--- .../utils/httpclient/OkHttpClientBuilder.java | 15 ++++++++++++--- .../WebSocketApiHttpClientSingleton.java | 2 +- .../WebSocketStreamHttpClientSingleton.java | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java index 99191b9a..8b0a5c2b 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/HttpClientSingleton.java @@ -12,12 +12,12 @@ private HttpClientSingleton() { private static void createHttpClient(ProxyAuth proxy) { if (proxy == null) { - httpClient = new OkHttpClient(OkHttpClientBuilder.builder); + httpClient = new OkHttpClient(OkHttpClientBuilder.getBuilder()); } else { if (proxy.getAuth() == null) { - httpClient = OkHttpClientBuilder.builder.proxy(proxy.getProxy()).build(); + httpClient = OkHttpClientBuilder.getBuilder().proxy(proxy.getProxy()).build(); } else { - httpClient = OkHttpClientBuilder.builder.proxy(proxy.getProxy()).proxyAuthenticator(proxy.getAuth()).build(); + httpClient = OkHttpClientBuilder.getBuilder().proxy(proxy.getProxy()).proxyAuthenticator(proxy.getAuth()).build(); } } } diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java b/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java index c1062162..f3fcb0ca 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/OkHttpClientBuilder.java @@ -2,8 +2,17 @@ import okhttp3.OkHttpClient; -import java.time.Duration; +public final class OkHttpClientBuilder { + private static OkHttpClient.Builder builder = new OkHttpClient.Builder(); -public class OkHttpClientBuilder { - public static OkHttpClient.Builder builder = new OkHttpClient.Builder().pingInterval(Duration.ofSeconds(10)); + private OkHttpClientBuilder() { + } + + public static void setBuilder(OkHttpClient.Builder builder) { + OkHttpClientBuilder.builder = builder; + } + + public static OkHttpClient.Builder getBuilder() { + return builder; + } } diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java index f50eb478..065107f6 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketApiHttpClientSingleton.java @@ -3,7 +3,7 @@ import okhttp3.OkHttpClient; public final class WebSocketApiHttpClientSingleton { - private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.builder); + private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.getBuilder()); private WebSocketApiHttpClientSingleton() { } diff --git a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java index 053ad03c..d6008970 100644 --- a/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java +++ b/src/main/java/com/binance/connector/client/utils/httpclient/WebSocketStreamHttpClientSingleton.java @@ -3,7 +3,7 @@ import okhttp3.OkHttpClient; public final class WebSocketStreamHttpClientSingleton { - private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.builder); + private static final OkHttpClient httpClient = new OkHttpClient(OkHttpClientBuilder.getBuilder()); private WebSocketStreamHttpClientSingleton() { }