# Elasticsearch TransportClient singleton instance

**URL:** <https://discuss.elastic.co/t/elasticsearch-transportclient-singleton-instance/16887>\
**Category:** Elasticsearch\
**Created:** [April 9, 2014, 8:14am UTC](https://discuss.elastic.co/t/elasticsearch-transportclient-singleton-instance/16887 "2014-04-09T08:14:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bagui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bagui/32/960_2.png) [@bagui](https://discuss.elastic.co/u/bagui)\
**Post date:** [April 9, 2014, 8:14am UTC](https://discuss.elastic.co/t/elasticsearch-transportclient-singleton-instance/16887/1 "2014-04-09T08:14:33Z")

</div>

Hi,

I'm trying to use a singleton instance of client for multiple index  
creation. Below is the code for the same. But every time I'm getting  
instance as null and it's creating a new instance. Please let me know what  
I'm doing wrong

_singleton instance_ :

public class ESClientSingleton {

public static Client instance ;

private ESClientSingleton()  
{}  
public static Client getInstance()  
{  
if (instance == null)  
{  
System.out.println("the instance is null...");  
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();  
settings.put("node.client", true);  
settings.put("node.data", false);  
settings.put("node.name", "node-client");  
settings.put("cluster.name", "elasticsearch");  
settings.build();  
instance = new TransportClient(settings)  
.addTransportAddress(new  
InetSocketTransportAddress("10.203.251.142", 9300));  
//instance = client;  
return instance;  
}  
return instance;  
}  
}

_calling method_ :

public static IndexResponse insertESDocument(String nodeName, String json)  
{  
Client client = ESClientSingleton.getInstance();  
logger.debug("calling the es client");  
logger.debug("the json received as == "+json);  
IndexResponse response =  
client.prepareIndex("aricloud-nodes","node-entry",nodeName )  
.setSource(json)  
.execute()  
.actionGet();  
logger.debug("the document is successfully indexed...");  
System.out.println("the document is indexed...");  
[//client.close](https://client.close)();  
return response;  
}

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [April 9, 2014, 9:26pm UTC](https://discuss.elastic.co/t/elasticsearch-transportclient-singleton-instance/16887/2 "2014-04-09T21:26:06Z")

</div>

Nothing sticks out, but I would synchronize the call so that different  
threads do not end up creating an instance. Enforce the singleton pattern  
by making the static variable private and have only one return in your  
method. My code looks something like

public class ClientFactory {  
private static TransportClient client;

public static Client getClient() {  
if (client == null) {  
synchronized (ClientFactory.class) {  
...  
Settings settings = ImmutableSettings.settingsBuilder()  
.put("cluster.name", cluster)  
.put("node.client", true)  
.put("client.transport.sniff", "true")  
.build();  
client = new TransportClient(settings, false);  
for (String server: servers) {  
client.addTransportAddress(new InetSocketTransportAddress(server, ES\_PORT));  
}  
}  
}  
return client;  
}  
}

On Wed, Apr 9, 2014 at 1:14 AM, Subhadip Bagui [i.bagui@gmail.com](mailto:i.bagui@gmail.com) wrote:

> Hi,
> 
> I'm trying to use a singleton instance of client for multiple index  
> creation. Below is the code for the same. But every time I'm getting  
> instance as null and it's creating a new instance. Please let me know what  
> I'm doing wrong
> 
> _singleton instance_ :
> 
> public class ESClientSingleton {
> 
> public static Client instance ;
> 
> private ESClientSingleton()  
> {}  
> public static Client getInstance()  
> {  
> if (instance == null)  
> {  
> System.out.println("the instance is null...");  
> ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder();  
> settings.put("node.client", true);  
> settings.put("node.data", false);  
> settings.put("node.name", "node-client");  
> settings.put("cluster.name", "elasticsearch");  
> settings.build();  
> instance = new TransportClient(settings)  
> .addTransportAddress(new  
> InetSocketTransportAddress("10.203.251.142", 9300));  
> //instance = client;  
> return instance;  
> }  
> return instance;  
> }  
> }
> 
> _calling method_ :
> 
> public static IndexResponse insertESDocument(String nodeName, String json)  
> {  
> Client client = ESClientSingleton.getInstance();  
> logger.debug("calling the es client");  
> logger.debug("the json received as == "+json);  
> IndexResponse response =  
> client.prepareIndex("aricloud-nodes","node-entry",nodeName )  
> .setSource(json)  
> .execute()  
> .actionGet();  
> logger.debug("the document is successfully indexed...");  
> System.out.println("the document is indexed...");  
> [//client.close](https://client.close)();  
> return response;  
> }
> 
> --  
> 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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/36c95b86-5fa3-4af0-998a-a1b15b0bd1ab%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDO10-3PEyPrrnNc0jDA481AwYZcEtmb8uv3SXPa803rA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDO10-3PEyPrrnNc0jDA481AwYZcEtmb8uv3SXPa803rA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 1:37am UTC](https://discuss.elastic.co/t/elasticsearch-transportclient-singleton-instance/16887/3 "2017-07-06T01:37:06Z")

</div>


