How to get a NodeClient inside a plugin?

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!

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

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.

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;
    }
}

could you please check the code :sweat_smile:

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.

1 Like

thanks a lot @DavidTurner :pray:

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