Wednesday, 14 June 2017

Elasticsearch REST Java Connection Client

For Elasticsearch connection, Elasticsearch REST Java API provide RestClient and RestClient.build() to get connection.

Below connection client class covers all the ways of connectivity to elastic search depend of elastic servers configuration and  accessibility accordingly you can uncomment below methods in customize Http Client.  I made this class as Singleton because Elasticsearch client keep connection persistent.

public class ElasticSearchConnectionRestTest {
    private RestClient client = null;
    private static ElasticSearchConnectionRestTest esc = null;
    private ElasticSearchConnectionRestTest() {
    }

    public static synchronized ElasticSearchConnectionRestTest getInstance() {
        if (esc == null) {
            esc = new ElasticSearchConnectionRestTest();
        }
        return esc;
    }

    public RestClient getClient() {
        if (client == null) {
            getElasticSearchClient();
        }
        return client;
    }

private RestClient getElasticSearchClient() {

//Basic credential settings
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("usrid", "password"));

client = RestClient.builder(new HttpHost("elasticserchhost1",
Integer.parseInt("elasticsearchport1")),
new HttpHost("elasticserchhost2",
Integer.parseInt("elasticsearchport2"))))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
 //Security Settings
@override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    return httpClientBuilder
//disable preemptive authentication so that same request done when next
//request
.disableAuthCaching()

//Credentials
.setDefaultCredentialsProvider(credentialsProvider)

//Proxy server settings
.setProxy(new HttpHost("one.proxy.att.com", 8080))

//setting for key store for JKS SSL
//.setSSLContext(sslcontext)

//Number of threads will execute
//.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build())
//connection timeout
//.setConnectTimeout(5000)
//socket connection timeout
//.setSocketTimeout(60000)
                                ;
}
)
//Max retry timeout
.setMaxRetryTimeoutMillis(60000).build();
return client;
}

private void closeConnnection()
{
try
{
client.close();
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}

Details of API’s used by Elasticsearch REST Connection

setHttpClientConfigCallback


This callback method allows to modify the http client configuration like  encrypted ,proxy, communication over ssl, socket timeout etc.  By using customizeHttpClient we can configure all these values.

setDefaultHeaders


We can set default header if need to sent some value with in  every request.

setMaxRetryTimeoutMillis


The timeout value if request do multiple attempts for same request.

setFailureLister


This is listener to get notification whenever get any node fails and need to get any action for same.

Methods of CustomizeHttpClient Builder


Timeout Configuration


connectTimeout: Default value is 1 seconds

socketTimeout: Default 30 seconds

maxRetryTimeoutMilliseconds: Default 30 seconds

Thread Configuration


IoThreadCount:  Client start with default one thread and a number of worker threads used by connection manager, as many as the number of locally detected processors.

Authentication Configuration


setDefaultCredentialsProvider :  This method require basic credentials for authentication.

httpClientBuilder.disableAuthCaching(): We can disable authentication caching and sent in  every request headers to elasticsearch  if it will accepted and,  if it get failed by receiving a HTTP 401 response message, it will resend the exact same request again with the basic authentication header.

Encrypted Communication


setSSLContext:  Set this value for SSL Context for encrypted communication.