Being the leader

The ILeaderSupport plugin is provided by the Manager netlet, and intended to help the current leader.


public interface ILeaderSupport
{
    /**
     *  Check if this netlet is the leader.
     * @return   True if this netlet is the leader; false if not.
     */
    public boolean amLeader();

    /**
     *  Set manager redundancy parameters.  If the number falls below
     * <code>min</code>, managers will be created to bring the
     * number of managers up to <code>max</code>. These parameters
     * may be changed mid-application without any problem, though such a
     * change may not have immediate effect. Only has any effect in the
     * current leader.
     */
    public void setRedundancy(int min, int max);

    /**
     *  Set the argument used for application warm starts.  Should allow the
     *  warm start netlet to reconnect or reinitialise the application.
     * @param arg  The argument to pass to the <code>warmStart()</code>
     *     method of the IManagerLifeCycle plugin.
     */
    public void enableWarmStart(Serializable arg) throws BeatrixException;

    /**
     *  Stop the application from being reinitialised.
     */
    public void disableWarmStart();

    /**
     *  Set the argument used for manager joins.  
     *  This setting lifes only as long as the current leader.
     *
     * @param arg  serializable argument
     */
    public void setJoinArgument(Serializable arg);

    /** Terminate the application. Only works in the leader */
    public void quitApplication();

    /** Utility class helping lookup */
    public static class Get
    {
        /** Get the only instance of this plugin in the given manager.
         * Only the first such plugin is returned. If there isn't one then
         * an ArrayIndexOutOfBoundsException is thrown.
         */
        public static ILeaderSupport one(IPluginManager pm)
        {
            return ((ILeaderSupport)pm.lookup(ILeaderSupport.class)[0]);
        } 
    }
}

Invoking the enableWarmStart() method means we set the parameter given to the warmStart() method in the IManagerLifeCycle plugin. This is application specific, and should be anything which helps the application recover after a complete application failure. Similarly for the setJoinArgument() method.

The following example is adapted from the skeleton application. It shows how an IManagerLifeCycle plugin is written to make use of the Manager's ILeaderSupport plugin, using a leadership change as a trigger to ensure there are the right number of managers running:


public class LifeCyclePlugin extends AbstractPlugin
    implements IManagerLifeCycle, IPlugin
{
    // ... much work omitted

    public void changeLeaderMode(boolean you_are_leader)
    {
        if (you_are_leader)
        {
            ILeaderSupport ls
                = (ILeaderSupport)pluginManager().lookup(ILeaderSupport.class)[0];
            ls.setRedundancy(2,4);

        }
    }
}

Nik Silver 2002-03-09