66
66
67
67
public class HttpClientJavaLib extends GXHttpClient {
68
68
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 ;
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 ]};
79
73
}
80
- }
74
+ return allIps ;
75
+ };
81
76
82
- private static String getGxIpResolverConfig () {
77
+ private static boolean isFirstIpDnsEnabled () {
83
78
String name = "GX_USE_FIRST_IP_DNS" ;
84
79
String gxDns = System .getProperty (name );
85
80
if (gxDns == null || gxDns .trim ().isEmpty ()) {
86
81
gxDns = System .getenv (name );
87
82
}
88
- if (gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" )) {
89
- return gxDns .trim ();
90
- } else {
91
- return null ;
92
- }
83
+ return gxDns != null && gxDns .trim ().equalsIgnoreCase ("true" );
93
84
}
94
85
95
-
96
86
public HttpClientJavaLib () {
97
87
getPoolInstance ();
98
88
ConnectionKeepAliveStrategy myStrategy = generateKeepAliveStrategy ();
99
89
HttpClientBuilder builder = HttpClients .custom ()
100
90
.setConnectionManager (connManager )
101
91
.setConnectionManagerShared (true )
102
92
.setKeepAliveStrategy (myStrategy );
103
- if (getGxIpResolverConfig () != null ) {
104
- builder .setDnsResolver (new FirstIpDnsResolver () );
93
+ if (isFirstIpDnsEnabled () ) {
94
+ builder .setDnsResolver (FIRST_IP_DNS_RESOLVER );
105
95
}
106
96
httpClientBuilder = builder ;
107
- cookies = new BasicCookieStore ();
97
+ cookies = new BasicCookieStore ();
108
98
streamsToClose = new Vector <>();
109
99
}
110
100
@@ -114,10 +104,11 @@ private static void getPoolInstance() {
114
104
RegistryBuilder .<ConnectionSocketFactory >create ()
115
105
.register ("http" , PlainConnectionSocketFactory .INSTANCE ).register ("https" , getSSLSecureInstance ())
116
106
.build ();
117
- boolean useCustomDnsResolver = getGxIpResolverConfig () != null ;
118
- PoolingHttpClientConnectionManager connManager = useCustomDnsResolver
119
- ? new PoolingHttpClientConnectionManager (socketFactoryRegistry , new FirstIpDnsResolver ())
120
- : new PoolingHttpClientConnectionManager (socketFactoryRegistry );
107
+ if (isFirstIpDnsEnabled ()) {
108
+ connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry , FIRST_IP_DNS_RESOLVER );
109
+ } else {
110
+ connManager = new PoolingHttpClientConnectionManager (socketFactoryRegistry );
111
+ }
121
112
connManager .setMaxTotal ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_SIZE" , "1000" )));
122
113
connManager .setDefaultMaxPerRoute ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_PER_ROUTE" , "1000" )));
123
114
@@ -675,7 +666,7 @@ public void execute(String method, String url) {
675
666
resetStateAdapted ();
676
667
}
677
668
}
678
-
669
+
679
670
private synchronized void displayHTTPConnections (){
680
671
Iterator <HttpRoute > iterator = storedRoutes .iterator ();
681
672
while (iterator .hasNext ()) {
0 commit comments