6666
6767public class HttpClientJavaLib extends GXHttpClient {
6868
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 > 0 ) {
72+ return new InetAddress []{allIps [0 ]};
7973 }
80- }
74+ return allIps ;
75+ };
8176
82- private static String getGxIpResolverConfig () {
77+ private static boolean isFirstIpDnsEnabled () {
8378 String name = "GX_USE_FIRST_IP_DNS" ;
8479 String gxDns = System .getProperty (name );
8580 if (gxDns == null || gxDns .trim ().isEmpty ()) {
8681 gxDns = System .getenv (name );
8782 }
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" );
9384 }
9485
95-
9686 public HttpClientJavaLib () {
9787 getPoolInstance ();
9888 ConnectionKeepAliveStrategy myStrategy = generateKeepAliveStrategy ();
9989 HttpClientBuilder builder = HttpClients .custom ()
10090 .setConnectionManager (connManager )
10191 .setConnectionManagerShared (true )
10292 .setKeepAliveStrategy (myStrategy );
103- if (getGxIpResolverConfig () != null ) {
104- builder .setDnsResolver (new FirstIpDnsResolver () );
93+ if (isFirstIpDnsEnabled () ) {
94+ builder .setDnsResolver (FIRST_IP_DNS_RESOLVER );
10595 }
10696 httpClientBuilder = builder ;
107- cookies = new BasicCookieStore ();
97+ cookies = new BasicCookieStore ();
10898 streamsToClose = new Vector <>();
10999 }
110100
@@ -114,10 +104,11 @@ private static void getPoolInstance() {
114104 RegistryBuilder .<ConnectionSocketFactory >create ()
115105 .register ("http" , PlainConnectionSocketFactory .INSTANCE ).register ("https" , getSSLSecureInstance ())
116106 .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+ }
121112 connManager .setMaxTotal ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_SIZE" , "1000" )));
122113 connManager .setDefaultMaxPerRoute ((int ) CommonUtil .val (clientCfg .getProperty ("Client" , "HTTPCLIENT_MAX_PER_ROUTE" , "1000" )));
123114
@@ -675,7 +666,7 @@ public void execute(String method, String url) {
675666 resetStateAdapted ();
676667 }
677668 }
678-
669+
679670 private synchronized void displayHTTPConnections (){
680671 Iterator <HttpRoute > iterator = storedRoutes .iterator ();
681672 while (iterator .hasNext ()) {
0 commit comments