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 <[email protected]> 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();
>          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 [email protected].
> 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?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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDO10-3PEyPrrnNc0jDA481AwYZcEtmb8uv3SXPa803rA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to