This is where we handle manager (and hence application) life cycle changes. Flow control begins here, with the coldStart() method; when this returns initialisation is essentially complete, and all other action is event-driven.
This is an essential plugin for any application as it helps it start.
In this example the bulk of the work is the manager adding plugins
to itself. See Section
for more on application
start-up, Section
on worker pools and Section
on resources.
package org.jtrix.project.skeleton2.plugins;
import org.jtrix.base.*;
import org.jtrix.facets1.util.properties.*;
import org.jtrix.project.libjtrix.debug.*;
import org.jtrix.project.beatrix.common.*;
import org.jtrix.project.beatrix.plugins.*;
import org.jtrix.project.beatrix.plugins.hooks.*;
import org.jtrix.project.beatrix.plugins.util.*;
import java.io.Serializable;
/**
* @author ulf@jtrix.org
* @author nadia@jtrix.org
* @version $Revision: 1.28 $
*/
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];
}
public boolean init(IPluginManager plugin_manager, Object data)
{
Debug.set("skeletonmanager",-1,0);
if (!super.init(plugin_manager, data))
{
return false;
}
try
{
Debug.msg(Debug.INFO, this, "init(): plugin_manager: ", pluginManager().toString());
Debug.msg(Debug.INFO, this, "Adding ServiceManager.class");
pluginManager().add(ServiceManager.class, false);
pluginManager().add(WORKER_POOL, WorkerPool.class, null, false);
// service manager needs to be initialised before add can be called
IServiceManager sm
= (IServiceManager)pluginManager().lookup(IServiceManager.class)[0];
sm.add( "admin", new AdminServiceProvider(), 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);
// Add a FileSystemLocator plugin to the worker pool so that it
// always demands file system resources for its workers. We give
// it a parameter, a LocatorInfo, which describes the file system
// requirements: 1MB of non-redundant disk space.
FileSystemLocator.LocatorInfo info
= new FileSystemLocator.LocatorInfo(1024, 1024, true, 1, "disk", 5);
wp.add("disk", FileSystemLocator.class, info, false);
wp.add(SocketFactoryLocator.class, false);
}
catch(Exception e)
{
Debug.exc(this, e, "Initialise failed");
return false;
}
return true;
}
public Warrant coldStart(IPropertyCollection args) throws BeatrixException
{
Debug.msg(Debug.INFO,this,"coldStart()");
changeLeaderMode(true);
return managerSupport().getWarrant("admin",null);
}
public void warmStart(Serializable arg)
{
Debug.msg(Debug.INFO,this,"warmStart()");
changeLeaderMode(true);
}
public void join(Serializable arg)
{}
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);
}
}
}
Nik Silver 2002-03-09