A plugin for the manager which handles both worker resource requirements and their life cycle events.
package org.jtrix.project.skeleton2.plugins;
import org.jtrix.project.libjtrix.debug.*;
import org.jtrix.project.beatrix.handle.*;
import org.jtrix.project.beatrix.plugins.*;
import org.jtrix.project.beatrix.plugins.hooks.*;
import org.jtrix.project.beatrix.plugins.util.*;
/**
* Plugin that manages start up and shutdown of workers.
*
* @author ulf@jtrix.org
* @author nadia@jtrix.org
* @version $Revision: 1.17 $
*/
public class SkeletonWorkerManager extends AbstractPlugin
implements IWorkerLifeCycle, IWorkerResourceRequester
{
/** Respond to a new worker starting by inserting plugins remotely.
*/
public void workerStarted(IWorkerHandle worker)
{
try
{
Debug.msg(Debug.INFO, this, "workerStarted()");
IInternalFacetCache ifc = (IInternalFacetCache) pluginManager()
.lookup(IInternalFacetCache.class)[0];
// Add a service manager plugin to the worker.
// Add a plugin for the worker's basic functionality.
// Add a plugin to the service manager which provides the skeleton gservice.
IRemotePluginManager pm = (IRemotePluginManager)ifc
.getInternalFacet(worker, IRemotePluginManager.class.getName(), null, true);
pm.remoteAdd(RemoteServiceManager.class, false);
pm.remoteAdd(SkeletonWorker.class, true);
Debug.msg(Debug.INFO, this, "workerStarted, looking up service manager");
IRemoteServiceManager sm = (IRemoteServiceManager)pm
.remoteLookup(IRemoteServiceManager.class)[0];
Debug.msg(Debug.INFO, this, "Got service manager");
sm.remoteAdd("skeleton",SkeletonServiceProvider.class, null, false);
}
catch(Exception e)
{
Debug.exc(this, e, "workerStarted() failed.");
}
}
/** Respond to a worker dying.
*/
public void workerDied(IWorkerHandle worker)
{}
/** Demand particular resources for a worker. We only do this as a demonstration
* of how to demand resources. We demand filesystem resources easily
* with FileSystemLocator.LocatorInfo in the LifeCyclePlugin. Here we demand
* a socket factory and set our requirements by hand.
*/
public void configureResourceHandle(IResourceHandle resource)
throws IWorkerResourceRequester.ResourceUnavailableException
{
ISocketFactoryLocator sfl
= (ISocketFactoryLocator)pluginManager().lookup(ISocketFactoryLocator.class)[0];
IResourceClassHandle[] net_classes = sfl.locateByCount(1, 1);
if(net_classes.length == 0)
{
throw new IWorkerResourceRequester.ResourceUnavailableException(
"No resource classes");
}
try
{
resource.addBinding("net", 10, net_classes, null);
}
catch (Exception e)
{
Debug.exc(this,e,"Resource creation failed");
throw new IWorkerResourceRequester.ResourceUnavailableException(
"Resource creation failed");
}
}
}
A worker starting is a prompt to add plugins remotely; a worker dying requires no action. Meanwhile, the plugin also sets worker resource requirements for networking; worker disk requirements are handled entirely by the FileSystemLocator.
See Section
on internal facets, Section
on remote plugin management and Section
on resources.
Nik Silver 2002-03-09