/*
 * Copyright (c) 2018 Redlink GmbH.
 */
package com.rbmhtechnology.vind.monitoring.utils;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * Created on 01.03.18.
 */
public class ElasticSearchClientBuilder {

    private static final Logger log = LoggerFactory.getLogger(ElasticSearchClientBuilder.class);

    public static JestClient build(String host, String port) {
        final String conn = String.format("http://%s:%s", host, port);
        log.info("Creating ElasticSearch REST Client over {}..,", conn);
        final JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig.Builder(conn)
                .multiThreaded(true)
                .discoveryEnabled(true)
                .discoveryFrequency(100l, TimeUnit.MILLISECONDS)
                .maxConnectionIdleTime(1500L, TimeUnit.MILLISECONDS)
                .maxTotalConnection(75)
                .defaultMaxTotalConnectionPerRoute(75)
                .build());
        return factory.getObject();
    }

    public static JestClient build(String host, int port) {
        return build(host, Integer.toString(port));
    }
}

@@ -494,16 +494,59 @@
 lass);%0A%0A
+    private static JestClient jestClient;%0A%0A
     publ
@@ -592,32 +592,69 @@
  String port) %7B%0A
+        if(jestClient == null) %7B%0A    
         final St
@@ -708,16 +708,20 @@
  port);%0A
+    
         
@@ -789,24 +789,28 @@
 n);%0A        
+   
+ 
 final JestCl
@@ -852,24 +852,28 @@
 tFactory();%0A
+    
         fact
@@ -947,16 +947,20 @@
         
+ 
+   
 .multiTh
@@ -980,32 +980,36 @@
                 
+  
+  
 .discoveryEnable
@@ -1008,32 +1008,36 @@
 ryEnabled(true)%0A
+    
                 
@@ -1097,24 +1097,28 @@
             
+    
 .maxConnecti
@@ -1170,24 +1170,28 @@
             
+ 
+   
 .maxTotalCon
@@ -1198,24 +1198,28 @@
 nection(75)%0A
+    
             
@@ -1277,16 +1277,20 @@
         
+    
 .build()
@@ -1300,22 +1300,32 @@
         
-return
+    jestClient =
  factory
@@ -1334,24 +1334,61 @@
 etObject();%0A
+        %7D%0A        return jestClient;%0A
     %7D%0A%0A    p

create ES Client only once