# How to get a NodeClient inside a plugin?

**URL:** <https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703>\
**Category:** Elasticsearch\
**Created:** [November 22, 2023, 8:50am UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703 "2023-11-22T08:50:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azizi\_BESSEM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azizi_bessem/32/127904_2.png) [@Azizi\_BESSEM](https://discuss.elastic.co/u/Azizi_BESSEM)\
**Post date:** [November 22, 2023, 8:50am UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/1 "2023-11-22T08:50:02Z")

</div>

I am currently working on developing a schedule plugin for Elasticsearch. The objective is to display only the index number every 5 minutes. However, I am encountering an issue where the NodeClient is consistently null.

I would greatly appreciate any suggestions or guidance from the community on how to address this NodeClient null issue and any insights on improving the schedule plugin.

Thank you in advance for your assistance!

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [November 22, 2023, 11:17am UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/2 "2023-11-22T11:17:22Z")

</div>

Your plugin's `createComponents()` method is called with a `PluginServices` object, whose `.client()` method returns a `NodeClient`.

---

<div class="post-metadata">

**Author:** ![Azizi\_BESSEM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azizi_bessem/32/127904_2.png) [@Azizi\_BESSEM](https://discuss.elastic.co/u/Azizi_BESSEM)\
**Post date:** [November 22, 2023, 12:39pm UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/3 "2023-11-22T12:39:21Z")

</div>

When Elasticsearch loads the plugin, the `createComponents` method is not being invoked, resulting in a `null` client. The objective is to ensure that the `createComponents` method is executed during plugin initialization, allowing access to a non-null client.

```auto
public class ConnectorPlugin extends Plugin implements ActionPlugin {
    private static final Properties properties = new Properties();
    private boolean componentsCreated = false;
    private Client client;

    public ConnectorPlugin() throws IOException {
        Properties connectorProperties = new Properties();
        Properties pluginDescriptorProperties = new Properties();
        InputStream connectorStream = this.getClass().getResourceAsStream("/connector.properties");
        InputStream pluginDescriptorStream = this.getClass().getResourceAsStream("/plugin-descriptor.properties");
        connectorProperties.load(connectorStream);
        pluginDescriptorProperties.load(pluginDescriptorStream);
        properties.putAll(connectorProperties);
        properties.putAll(pluginDescriptorProperties);
    }
    @Override
    public Collection<Object> createComponents(
        Client client,
        ClusterService clusterService,
        ThreadPool threadPool,
        ResourceWatcherService resourceWatcherService,
        ScriptService scriptService,
        NamedXContentRegistry xContentRegistry,
        Environment environment,
        NodeEnvironment nodeEnvironment,
        NamedWriteableRegistry namedWriteableRegistry,
        IndexNameExpressionResolver indexNameExpressionResolver,
        Supplier<RepositoriesService> repositoriesServiceSupplier
    ){
        this.client=client;
        System.out.println("Client is properly initialized");
        List<Object> components = new ArrayList<>();
        return components;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Azizi\_BESSEM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azizi_bessem/32/127904_2.png) [@Azizi\_BESSEM](https://discuss.elastic.co/u/Azizi_BESSEM)\
**Post date:** [November 22, 2023, 12:41pm UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/4 "2023-11-22T12:41:57Z")

</div>

could you please check the code 😅

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [November 22, 2023, 1:09pm UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/5 "2023-11-22T13:09:49Z")

</div>

Looks ok to me. I don't think it's possible for Elasticsearch to create a plugin and not call `createComponents` later on, so I think it must be that Elasticsearch isn't creating the instance of `ConnectorPlugin` either.

---

<div class="post-metadata">

**Author:** ![Azizi\_BESSEM](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azizi_bessem/32/127904_2.png) [@Azizi\_BESSEM](https://discuss.elastic.co/u/Azizi_BESSEM)\
**Post date:** [November 23, 2023, 10:13am UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/6 "2023-11-23T10:13:36Z")

</div>

thanks a lot @DavidTurner 🙏

---

<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:** [December 21, 2023, 10:13am UTC](https://discuss.elastic.co/t/how-to-get-a-nodeclient-inside-a-plugin/347703/7 "2023-12-21T10:13:44Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
