Example Configuration

// Build the settings for our transport client.
Settings settings = ImmutableSettings.settingsBuilder()
    // Setting "transport.type" enables this module:
    .put("transport.type", "no.found.elasticsearch.transport.netty.FoundNettyTransportModule")
    // Create an api key via the console and add it here:
    .put("transport.found.api-key", "YOUR_API_KEY")

    .put("cluster.name", "YOUR_CLUSTER_ID")

    .put("client.transport.ignore_cluster_name", false)
    .put("client.transport.nodes_sampler_interval", "30s")
    .put("client.transport.ping_timeout", "30s")

    .build();

// Instantiate a TransportClient and add Elastic Cloud to the list of addresses to connect to.
// Only port 9343 (SSL-encrypted) is currently supported.
Client client = new TransportClient(settings)
    .addTransportAddress(new InetSocketTransportAddress("YOUR_CLUSTER_ID-REGION.foundcluster.com", 9343));

Example Usage

while(true) {
    try {
        System.out.print("Getting cluster health... "); System.out.flush();
        ActionFuture<ClusterHealthResponse> healthFuture = client.admin().cluster().health(Requests.clusterHealthRequest());
        ClusterHealthResponse healthResponse = healthFuture.get(5, TimeUnit.SECONDS);
        System.out.println("Got response: " + healthResponse.getStatus());
    } catch(Throwable t) {
        System.out.println("Error: " + t);
    }
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ie) { ie.printStackTrace(); }
}

DNS-caching

Note that the JVM will cache DNS lookups infinitely, by default. Since this client is connecting to a load balancer which can change IPs, it’s important to disable this functionality.

This can be done by setting the security property networkaddress.cache.ttl in $JAVA_HOME/lib/security/java.security. You can also do it programmatically, e.g. java.security.Security.setProperty("networkaddress.cache.ttl" , "60").

Note that you can not configure it as a JVM-property with, for example, -Dnetworkaddress.cache.ttl=60.

Complete Example

A complete, runnable example can be found at https://github.com/foundit/elasticsearch-transport-module-example

Contributing

Report issues in the issue tracker.