Interface Plugin


  • public interface Plugin
    « start hereEntry point to core API of Hekate plugins.

    Overview

    Plugin is a kind of a micro application who's lifecycle is bound to the lifecycle of a Hekate instance. Each Plugin has full access to Hekate services API and can customize services configuration during its startup (f.e. register messaging channels, cluster event listeners, custom metrics, etc) as well as register custom services.

    Registration

    Plugins can be registered to a Hekate instance via HekateBootstrap.setPlugins(List).

    Lifecycle

    • When Hekate instance is created it calls install(HekateBootstrap) method on all of its plugins in their registration order. Plugins can use the provided HekateBootstrap instance to customize Hekate instance configuration. Note that this method gets called only once during the whole lifetime of Hekate instance.
    • When Hekate instance starts joining to a cluster it calls start(Hekate) method on all of its plugins in their registration order. This method gets called after services initialization but before actual joining to the cluster begins.
    • When Hekate instance starts Hekate.leave() leaving the cluster it calls stop() method on all of its plugins in the reveres order.

    Note: Plugin should be implemented in such a way so that its start(Hekate) and stop() methods could be called multiple times on the same instance. Those methods are not required to be thread safe since stop() is always called by the same thread that called start(Hekate).

    Example

    Below is a simple example of Plugin that manages cluster topology information within a text file:

    
    public class ClusterInfoPlugin implements Plugin {
        private Path file;
    
        @Override
        public void install(HekateBootstrap boot) {
            // Nothing to configure.
        }
    
        @Override
        public void start(Hekate hekate) throws HekateException {
            // Prepare file
            file = Paths.get(hekate.localNode().name() + "-cluster.txt");
    
            // Register cluster event listener that will update file on join/change cluster events.
            hekate.cluster().addListener(this::updateFile, ClusterEventType.JOIN, ClusterEventType.CHANGE);
        }
    
        @Override
        public void stop() throws HekateException {
            try {
                Files.deleteIfExists(file);
            } catch (IOException e) {
                throw new HekateException("Failed to delete " + file, e);
            }
        }
    
        private void updateFile(ClusterEvent event) throws HekateException {
            List<String> nodesInfo = event.topology().stream().map(ClusterNode::toString).collect(Collectors.toList());
    
            try {
                Files.write(file, nodesInfo);
            } catch (IOException e) {
                throw new HekateException("Failed to update " + file, e);
            }
        }
    }
    

    ... and register this plugin to a Hekate node:

    
    Hekate node = new HekateBootstrap()
        .withNodeName("example-node")
        .withPlugin(new ClusterInfoPlugin())
        .join();
    

    See Also:
    HekateBootstrap.setPlugins(List)