Releasing elasticsearch connection

Hi,

I am deploying a webapp, which internally queries elasticsearch. I am
creating only one connection, and I want to close it when the webapp shuts
down. I am using singleton pattern for creating the ES client and how do I
destroy/close ES client? When the application shuts down, will the client
get closed or I have to specify it/ where should I have the code to close
the connection?

This is my code:

public class ESClientManager {

    public static Client client = null;
    private static Settings settings = null;
    private static Object mutex = new Object();
    private static final String CONFIG_CLUSTER_NAME = "cluster.name";
    private static final String CLUSTER_NAME = "qatool_es";
    private static final String[] transportAddress = {
           "127.0.0.1"
    };

    private static final int transportPort = 9300;

    private ESClientManager() {
    }

    public static Client getClient() {
        if (client == null) {
            synchronized (mutex) {
                settings = ImmutableSettings.settingsBuilder()
                        .put(CONFIG_CLUSTER_NAME, CLUSTER_NAME).build();

                client = new TransportClient(settings);
                for (int i = 0; i < transportAddress.length - 1; i++) {
                    ((TransportClient) client).addTransportAddress(new 

InetSocketTransportAddress(transportAddress[i], transportPort));
}
logger.info("Elastic search client initialized");
}
}
logger.info("Returning the existing client");
return client;
}
}

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Have you try using client.close() ?

Jason

On Sun, Nov 30, 2014 at 8:23 PM, prachicsa@gmail.com wrote:

Hi,

I am deploying a webapp, which internally queries elasticsearch. I am
creating only one connection, and I want to close it when the webapp shuts
down. I am using singleton pattern for creating the ES client and how do I
destroy/close ES client? When the application shuts down, will the client
get closed or I have to specify it/ where should I have the code to close
the connection?

This is my code:

public class ESClientManager {

    public static Client client = null;
    private static Settings settings = null;
    private static Object mutex = new Object();
    private static final String CONFIG_CLUSTER_NAME = "cluster.name";
    private static final String CLUSTER_NAME = "qatool_es";
    private static final String[] transportAddress = {
           "127.0.0.1"
    };

    private static final int transportPort = 9300;

    private ESClientManager() {
    }

    public static Client getClient() {
        if (client == null) {
            synchronized (mutex) {
                settings = ImmutableSettings.settingsBuilder()
                        .put(CONFIG_CLUSTER_NAME,

CLUSTER_NAME).build();

                client = new TransportClient(settings);
                for (int i = 0; i < transportAddress.length - 1; i++) {
                    ((TransportClient) client).addTransportAddress(new

InetSocketTransportAddress(transportAddress[i], transportPort));
}
logger.info("Elastic search client initialized");
}
}
logger.info("Returning the existing client");
return client;
}
}

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com
https://groups.google.com/d/msgid/elasticsearch/a25c2c6e-82d3-4d7b-a9f0-2717dbc59225%40googlegroups.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAHO4itwhO%3D3QvnZd1%2BgqdXmHS%3DokjBL0mUqQ3LA-aO9B-nT%3D9w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.