# Obtaining an TransportClient via Spring Inject?

**URL:** https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159
**Category:** Elasticsearch
**Created:** [July 28, 2010, 12:18am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159 "2010-07-28T00:18:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Franz\_Allan\_Valencia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/franz_allan_valencia/32/3326_2.png) [@Franz\_Allan\_Valencia](https://discuss.elastic.co/u/Franz_Allan_Valencia)
#### Post date: [July 28, 2010, 12:18am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159/1 "2010-07-28T00:18:38Z")

</div>

Good day,

I am currently instantiating a Client similar to  
[http://www.elasticsearch.com/docs/elasticsearch/java\_api/client/#TransportClientusing](http://www.elasticsearch.com/docs/elasticsearch/java_api/client/#TransportClientusing)  
spring, which implies that I only instantiate this once and use it  
everywhere. Is that the proper way to use it?

I am not sure when the 'sniffing' occurs. Does it occur during client  
creation or for every client#get(..) invocation?

## Thanks,

Franz Allan Valencia See | Java Software Engineer  
[franz.see@gmail.com](mailto:franz.see@gmail.com)  
LinkedIn: [http://www.linkedin.com/in/franzsee](http://www.linkedin.com/in/franzsee)  
Twitter: [http://www.twitter.com/franz\_see](http://www.twitter.com/franz_see)

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [July 28, 2010, 6:14am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159/2 "2010-07-28T06:14:36Z")

</div>

Yes, you should shard the client as a "singleton". The sniffing happens in  
the background, based on the list of addresses provided.

-shay.banon

On Wed, Jul 28, 2010 at 3:18 AM, Franz Allan Valencia See \<  
[franz.see@gmail.com](mailto:franz.see@gmail.com)\> wrote:

> Good day,
> 
> I am currently instantiating a Client similar to  
> [http://www.elasticsearch.com/docs/elasticsearch/java\_api/client/#TransportClientusing](http://www.elasticsearch.com/docs/elasticsearch/java_api/client/#TransportClientusing) spring, which implies that I only instantiate this once and use it  
> everywhere. Is that the proper way to use it?
> 
> I am not sure when the 'sniffing' occurs. Does it occur during client  
> creation or for every client#get(..) invocation?
> 
> ## Thanks,
> 
> Franz Allan Valencia See | Java Software Engineer  
> [franz.see@gmail.com](mailto:franz.see@gmail.com)  
> LinkedIn: [http://www.linkedin.com/in/franzsee](http://www.linkedin.com/in/franzsee)  
> Twitter: [http://www.twitter.com/franz\_see](http://www.twitter.com/franz_see)

---

<div class="post-metadata">

### Author: ![erez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erez/32/3182_2.png) [@erez](https://discuss.elastic.co/u/erez)
#### Post date: [July 10, 2011, 10:57am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159/3 "2011-07-10T10:57:06Z")

</div>

Take a look at this post:

[http://techo-ecco.com/blog/elasticsearch-with-spring/](http://techo-ecco.com/blog/elasticsearch-with-spring/)

I have a TransportClientFactoryBean example here:

> <https://github.com/ezbz/projectx/blob/master/org.projectx.elasticsearch/src/main/java/org/projectx/elasticsearch/ElasticsearchTransportClientFactoryBean.java>

---

<div class="post-metadata">

### Author: ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)
#### Post date: [July 13, 2011, 11:15am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159/4 "2011-07-13T11:15:53Z")

</div>

I'm using guice and essentially this 4 lines in Module.configure:

Settings s = ImmutableSettings.settingsBuilder().put("cluster.name",  
cluster).build();  
TransportClient tmp = new TransportClient(s);  
tmp.addTransportAddress(new InetSocketTransportAddress(url, port));  
bind(MyCustomWrapper.class).toInstance(new MyCustomWrapper(tmp));

Then all I need is:

@Inject private Provider myIndexProvider;

to access Elasticsearch in my UI or elsewhere via:  
myIndexProvider.get().doSomething();

On 10 Jul., 12:57, erez [erezma...@gmail.com](mailto:erezma...@gmail.com) wrote:

> Take a look at this post:
> 
> [http://techo-ecco.com/blog/elasticsearch-with-spring/](http://techo-ecco.com/blog/elasticsearch-with-spring/)
> 
> I have a TransportClientFactoryBean example here:
> 
> [https://github.com/erezmazor/projectx/blob/master/org.projectx.elasti](https://github.com/erezmazor/projectx/blob/master/org.projectx.elasti)...
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/Obtaining-an-Transpor](http://elasticsearch-users.115913.n3.nabble.com/Obtaining-an-Transpor)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:00am UTC](https://discuss.elastic.co/t/obtaining-an-transportclient-via-spring-inject/3159/5 "2017-07-06T04:00:47Z")

</div>


