Worker pool example

The following is liberally adapted from the skeleton application's IManagerLifeCycle plugin and shows how a manager uses a worker pool. We discuss it after:


public class LifeCyclePlugin extends AbstractPlugin implements IManagerLifeCycle, IPlugin
{
    static final String WORKER_POOL = "worker_pool_1";

    IWorkerPool workerPool()
    {
        return (IWorkerPool)pluginManager().lookup(WORKER_POOL, IWorkerPool.class)[0];
    }
    
    /** Start the plugin. Make sure 2-4 workers are always running. */

    public boolean init(IPluginManager plugin_manager, Object data)
    {
        if (!super.init(plugin_manager, data))
        {
            return false;
        }

        try
        {
            pluginManager().add(WORKER_POOL, WorkerPool.class, null, false);

            IWorkerPool wp = workerPool();
            wp.setRedundancyMin(2);
            wp.setRedundancyMax(4);
            wp.setWorkerType(WORKER_POOL);
        
            wp.add(InternalFacetCache.class, false);
            wp.add(SkeletonWorkerManager.class, false);

            // The file system locator ensures the worker will have
            // 1024kB of disk space. The socket factory locator ensures
            // the worker will have networking.

            FileSystemLocator.LocatorInfo info
                = new FileSystemLocator.LocatorInfo(1024, 1024, true, 1, "disk", 5);
            wp.add("disk", FileSystemLocator.class, info, false);
            wp.add(SocketFactoryLocator.class, false);

            // ...Other plugin work omitted
        }
        catch(Exception e)
        {
            Debug.exc(this, e, "Initialise failed");
            return false;
        }
               
        return true;
    }

    public Warrant coldStart(IPropertyCollection args) throws BeatrixException
    {
        changeLeaderMode(true);
        return managerSupport().getWarrant("admin",null);
    }

    public void changeLeaderMode(boolean you_are_leader)
    {
        IWorkerPool wp = workerPool();

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

            wp.start(false);
        }
        else
        {
            wp.stop(false);
        }
    }

    // ...Lots of other work omitted
}

Nik Silver 2002-03-09