Brord

Custom plugin

May 9th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Two ways a developer can implement a custom plugin. Lets start with the basic method; extending our abstract class:

public class TestPlugin extends AccountPlugin {

    @Override
    public boolean start() {
        // Start any processes that you want to have running continuously

        // Return true if all went well, otherwise false
        return true;
    }

    @Override
    public String name() {
        return "AwesomeTestPlugin";
    }
}

You can override any other method if you need them, as can be found in the next example.

If for some reason, you are unable to extend a class, or do not want to, you can implement our Plugin interface.
This will require you to make a getter and setter for the account object your plugin will work with.

public class TestPlugin implements Plugin {

    private Account account;

    @Override
    public void setAccount(Account account) {
        this.account = account;
    }

    @Override
    public Account getAccount() {
        return account;
    }

    @Override
    public void load() throws Exception {
        // Load required data for this plugin. Think of reading storage, generating memory intensive resources, etc..
    }

    @Override
    public boolean start() {
        // Start any processes that you want to have running continuously

        // Return true if all went well, otherwise false
        return true;
    }

    @Override
    public void shutdown() {
        // Stop any running processes here
    }

    @Override
    public String name() {
        return "AwesomeTestPlugin";
    }
}

Once you have constructed a plugin class; you can add it to the builder of the account object like the following:

Plugin myPlugin = new TestPlugin();
IotaAccount account = new IotaAccount.Builder(SEED)
        .plugin(myPlugin)
        .build();

If everything went well, you will find the following message printed in your log stream:
Loaded plugin AwesomeTestPlugin
Each plugin will automatically be added as listening to events, so there is no need to register your plugin manually.

Add Comment
Please, Sign In to add comment