66
66
67
67
public class HttpClientJavaLib extends GXHttpClient {
68
68
69
- private static final DnsResolver FIRST_IP_DNS_RESOLVER = host -> {
70
- InetAddress [] allIps = SystemDefaultDnsResolver .INSTANCE .resolve (host );
71
- if (allIps != null && allIps .length > 1 ) {
72
- return new InetAddress []{allIps [0 ]};
69
+ private static class FirstIpDnsResolver implements DnsResolver {
70
+ private final DnsResolver defaultDnsResolver = new SystemDefaultDnsResolver ();
71
+
72
+ @ Override
73
+ public InetAddress [] resolve (final String host ) throws UnknownHostException {
74
+ InetAddress [] allIps = defaultDnsResolver .resolve (host );
75
+ if (allIps != null && allIps .length > 0 ) {
76
+ return new InetAddress []{allIps [0 ]};
77
+ }
78
+ return allIps ;
73
79
}
74
- return allIps ;
75
- };
80
+ }
76
81
77
- private static boolean isFirstIpDnsEnabled () {
82
+ private static String getGxIpResolverConfig () {
78
83
String name = "GX_USE_FIRST_IP_DNS" ;
79
84
String gxDns = System .getProperty (name );
80
85
if (gxDns == null || gxDns .trim ().isEmpty ()) {
81
86
gxDns = System .getenv (name );
82
87
}
83
- return gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" );
88
+ if (gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" )) {
89
+ return gxDns .trim ();
90
+ } else {
91
+ return null ;
92
+ }
84
93
}
85
94
95
+
86
96
public HttpClientJavaLib () {
87
97
getPoolInstance ();
88
98
ConnectionKeepAliveStrategy myStrategy = generateKeepAliveStrategy ();
89
99
HttpClientBuilder builder = HttpClients .custom ()
90
100
.setConnectionManager (connManager )
91
101
.setConnectionManagerShared (true )
92
102
.setKeepAliveStrategy (myStrategy );
93
- if (isFirstIpDnsEnabled () ) {
94
- builder .setDnsResolver (FIRST_IP_DNS_RESOLVER );
103
+ if (getGxIpResolverConfig () != null ) {
104
+ builder .setDnsResolver (new FirstIpDnsResolver () );
95
105
}
96
106
httpClientBuilder = builder ;
97
- cookies = new BasicCookieStore ();
107
+ cookies = new BasicCookieStore ();
98
108
streamsToClose = new Vector <>();
99
109
}
100
110
@@ -104,11 +114,10 @@ private static void getPoolInstance() {
104
114
RegistryBuilder .<ConnectionSocketFactory >create ()
105
115
.register ("http" , PlainConnectionSocketFactory .INSTANCE ).register ("https" ,getSSLSecureInstance ())
106
116
.build ();
107
- if (isFirstIpDnsEnabled ()) {
108
- connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry , FIRST_IP_DNS_RESOLVER );
109
- } else {
110
- connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry );
111
- }
117
+ boolean useCustomDnsResolver = getGxIpResolverConfig () != null ;
118
+ PoolingHttpClientConnectionManager connManager = useCustomDnsResolver
119
+ ? new PoolingHttpClientConnectionManager (socketFactoryRegistry , new FirstIpDnsResolver ())
120
+ : new PoolingHttpClientConnectionManager (socketFactoryRegistry );
112
121
connManager .setMaxTotal ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_SIZE" , "1000" )));
113
122
connManager .setDefaultMaxPerRoute ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_PER_ROUTE" , "1000" )));
114
123
@@ -650,7 +659,7 @@ public void execute(String method, String url) {
650
659
resetStateAdapted ();
651
660
}
652
661
}
653
-
662
+
654
663
private synchronized void displayHTTPConnections (){
655
664
Iterator <HttpRoute > iterator = storedRoutes .iterator ();
656
665
while (iterator .hasNext ()) {
0 commit comments