# Plugin: Getting LocalNode

**URL:** <https://discuss.elastic.co/t/plugin-getting-localnode/22544>\
**Category:** Elasticsearch\
**Created:** [March 6, 2015, 4:10am UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544 "2015-03-06T04:10:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbaranick](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jbaranick](https://discuss.elastic.co/u/jbaranick)\
**Post date:** [March 6, 2015, 4:10am UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/1 "2015-03-06T04:10:49Z")

</div>

What do the proper way to get the localnode (and it's id) in a plugin? I have tried many ways and it always seems to be null. The node just started and is loading the data from disk. Because of my inability to get localNodeId, I have had to fall back to using node.name for my lookups, but this seems more error prone.

--  
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/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [March 6, 2015, 9:36am UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/2 "2015-03-06T09:36:57Z")

</div>

Use something like this for node name

public class MyService extends AbstractLifeCycleComponent {  
@Inject  
public MyService(Settings settings, Node node) {  
super(settings);  
String name = node.settings().get("name");  
...  
}

and for node IDs

public class MyService extends AbstractLifeCycleComponent {  
@Inject  
public MyService(Settings settings, ClusterService clusterService) {  
super(settings);  
String nodeId = clusterService.localNode().getId();  
...  
}

Jörg

On Fri, Mar 6, 2015 at 5:10 AM, Kadaan [jbaranick@gmail.com](mailto:jbaranick@gmail.com) wrote:

> What do the proper way to get the localnode (and it's id) in a plugin? I  
> have tried many ways and it always seems to be null. The node just started  
> and is loading the data from disk. Because of my inability to get  
> localNodeId, I have had to fall back to using node.name for my lookups,  
> but this seems more error prone.
> 
> --  
> 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/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com)  
> .  
> 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/CAKdsXoF49xdMZ5LpQCgWD69OxYXstFiyNVs-\_da05hu2JFyikA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoF49xdMZ5LpQCgWD69OxYXstFiyNVs-_da05hu2JFyikA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jbaranick](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jbaranick](https://discuss.elastic.co/u/jbaranick)\
**Post date:** [March 6, 2015, 4:44pm UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/3 "2015-03-06T16:44:10Z")

</div>

Interesting. I was trying  
((ClusterStateResponse)state).getState().nodes().localNode(), but this  
didn't work.

On Friday, March 6, 2015 at 1:37:10 AM UTC-8, Jörg Prante wrote:

> Use something like this for node name
> 
> public class MyService extends AbstractLifeCycleComponent {  
> @Inject  
> public MyService(Settings settings, Node node) {  
> super(settings);  
> String name = node.settings().get("name");  
> ...  
> }
> 
> and for node IDs
> 
> public class MyService extends AbstractLifeCycleComponent {  
> @Inject  
> public MyService(Settings settings, ClusterService clusterService) {  
> super(settings);  
> String nodeId = clusterService.localNode().getId();  
> ...  
> }
> 
> Jörg
> 
> On Fri, Mar 6, 2015 at 5:10 AM, Kadaan \<[jbar...@gmail.com](mailto:jbar...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > What do the proper way to get the localnode (and it's id) in a plugin? I  
> > have tried many ways and it always seems to be null. The node just started  
> > and is loading the data from disk. Because of my inability to get  
> > localNodeId, I have had to fall back to using node.name for my lookups,  
> > but this seems more error prone.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%40googlegroups.com)  
> > .  
> > 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/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [March 6, 2015, 5:40pm UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/4 "2015-03-06T17:40:39Z")

</div>

In ClusterStateRequest, you have to set clusterStateRequest.nodes(true) to  
get nodes returned.

Jörg

On Fri, Mar 6, 2015 at 5:44 PM, Kadaan [jbaranick@gmail.com](mailto:jbaranick@gmail.com) wrote:

> Interesting. I was trying  
> ((ClusterStateResponse)state).getState().nodes().localNode(), but this  
> didn't work.
> 
> On Friday, March 6, 2015 at 1:37:10 AM UTC-8, Jörg Prante wrote:
> 
> > Use something like this for node name
> > 
> > public class MyService extends AbstractLifeCycleComponent {  
> > @Inject  
> > public MyService(Settings settings, Node node) {  
> > super(settings);  
> > String name = node.settings().get("name");  
> > ...  
> > }
> > 
> > and for node IDs
> > 
> > public class MyService extends AbstractLifeCycleComponent {  
> > @Inject  
> > public MyService(Settings settings, ClusterService clusterService) {  
> > super(settings);  
> > String nodeId = clusterService.localNode().getId();  
> > ...  
> > }
> > 
> > Jörg
> > 
> > On Fri, Mar 6, 2015 at 5:10 AM, Kadaan [jbar...@gmail.com](mailto:jbar...@gmail.com) wrote:
> > 
> > > What do the proper way to get the localnode (and it's id) in a plugin? I  
> > > have tried many ways and it always seems to be null. The node just started  
> > > and is loading the data from disk. Because of my inability to get  
> > > localNodeId, I have had to fall back to using node.name for my lookups,  
> > > but this seems more error prone.
> > > 
> > > --  
> > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%  
> > > [40googlegroups.com](http://40googlegroups.com).  
> > > 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/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%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/CAKdsXoFgk99fOZoU49J\_cDpFt3TnNO0LmVbNavFxk0Pdq2egeA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoFgk99fOZoU49J_cDpFt3TnNO0LmVbNavFxk0Pdq2egeA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jbaranick](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@jbaranick](https://discuss.elastic.co/u/jbaranick)\
**Post date:** [March 6, 2015, 7:06pm UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/5 "2015-03-06T19:06:52Z")

</div>

Yes. I did that [  
_clusterStateRequest.clear().nodes(true).routingTable(true)_ ], but I still  
wasn't able to get the localNodeId. It seems like the localNodeId is not  
set while the node is recovering.

On Friday, March 6, 2015 at 9:40:59 AM UTC-8, Jörg Prante wrote:

> In ClusterStateRequest, you have to set clusterStateRequest.nodes(true) to  
> get nodes returned.
> 
> Jörg
> 
> On Fri, Mar 6, 2015 at 5:44 PM, Kadaan \<[jbar...@gmail.com](mailto:jbar...@gmail.com) \<javascript:\>\>  
> wrote:
> 
> > Interesting. I was trying  
> > ((ClusterStateResponse)state).getState().nodes().localNode(), but this  
> > didn't work.
> > 
> > On Friday, March 6, 2015 at 1:37:10 AM UTC-8, Jörg Prante wrote:
> > 
> > > Use something like this for node name
> > > 
> > > public class MyService extends AbstractLifeCycleComponent {  
> > > @Inject  
> > > public MyService(Settings settings, Node node) {  
> > > super(settings);  
> > > String name = node.settings().get("name");  
> > > ...  
> > > }
> > > 
> > > and for node IDs
> > > 
> > > public class MyService extends AbstractLifeCycleComponent {  
> > > @Inject  
> > > public MyService(Settings settings, ClusterService clusterService) {  
> > > super(settings);  
> > > String nodeId = clusterService.localNode().getId();  
> > > ...  
> > > }
> > > 
> > > Jörg
> > > 
> > > On Fri, Mar 6, 2015 at 5:10 AM, Kadaan [jbar...@gmail.com](mailto:jbar...@gmail.com) wrote:
> > > 
> > > > What do the proper way to get the localnode (and it's id) in a plugin?  
> > > > I have tried many ways and it always seems to be null. The node just  
> > > > started and is loading the data from disk. Because of my inability to get  
> > > > localNodeId, I have had to fall back to using node.name for my  
> > > > lookups, but this seems more error prone.
> > > > 
> > > > --  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/116cd053-a26e-480f-b7ca-367a6af4236f%  
> > > > [40googlegroups.com](http://40googlegroups.com).  
> > > > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/e1a5c310-8dd6-48cd-b371-a49919d4fd56%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/159af095-35f2-4e39-b104-1ea761bd6021%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/159af095-35f2-4e39-b104-1ea761bd6021%40googlegroups.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, 12:27am UTC](https://discuss.elastic.co/t/plugin-getting-localnode/22544/6 "2017-07-06T00:27:59Z")

</div>


