Registering Agents With the Service Manager
The Service Manager is probably one of the most important Agents you will meet in the Agent Kernel. Once the Service Manager has started, other Agents can register their unique services with it for other Agents to find and use. For this reason, nearly every Agent application will load the Service Manager first.
To register with the Service Manager automatically, extend your Agent from a subclass of com.cometway.ak.ServiceAgent. Default start() and stop() methods are provided by ServiceAgent which correspondingly register and unregister the Agent with the ServiceManager.
Take a look at some code that implements a simple Demo Service:
import com.cometway.ak.ServiceAgent;
public class DemoServiceAgent extends ServiceAgent
{
public void initProps()
{
setDefault("service_name", "demo");
}
public void run()
{
println("The Demo Service Agent has been accessed.");
}
}
This is just a Plain Old Java Object that extends ServiceAgent. When this agent is started, its service_name will be used to register itself with the Service Manager and wait to be used by another Agent. That's all we have to do besides write the method we want to allow other Agents to use. Is up to you to decide what interfaces your agent supports.
Instead of hard-coding all of the Agents used in your application to instantiate and reference each other at compile-time, we can use the Agent Kernel to instantiate, initialize, and start each Agent, one by one until they have all started.
Let's compile and run the DemoServiceAgent:
[demo_service] % jc DemoServiceAgent.java
Compiling...
DemoService.java
[demo_service] % ak DemoServiceAgent
[AK] Comet Way Agent Kernel 2.7 Final
[000_AgentKernel] Starting on 2007/05/25 18:14:48.265 EDT
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2007/05/25 18:14:48.306 EDT
[001_XMLStartupAgent] Loading agents from ak.xstartup
[001_XMLStartupAgent] Loading agents from .
[000_AgentKernel] Creating agent DemoServiceAgent
[1003_DemoServiceAgent] Starting on 2007/05/25 18:14:48.360 EDT
!1003_DemoServiceAgent! CommandException
java.lang.RuntimeException: Service Manager is not loaded
at com.cometway.ak.ServiceManager.register(ServiceManager.java:240)
at com.cometway.ak.Agent.registerService(Agent.java:382)
at com.cometway.ak.Agent.register(Agent.java:370)
at com.cometway.ak.ServiceAgent.start(ServiceAgent.java:18)
at com.cometway.ak.AgentCommand.execute(AgentCommand.java:61)
at com.cometway.ak.AgentController.executeState(AgentController.java:216)
at com.cometway.ak.AgentController.waitForState(AgentController.java:165)
at com.cometway.ak.AgentController.start(AgentController.java:84)
at com.cometway.ak.AK.main(AK.java:298)
!AK! Could not start the Agent Kernel. Exiting.
java.lang.NullPointerException
at com.cometway.ak.AgentController.executeState(AgentController.java:200)
at com.cometway.ak.AgentController.waitForState(AgentController.java:165)
at com.cometway.ak.AgentController.start(AgentController.java:84)
at com.cometway.ak.AK.main(AK.java:298)
Wow, what was all that about? If you are not used to programming in Java, part of the output you are seeing is a Runtime Exception that is being reported by the Agent Kernel. This particular exception was caused because DemoServiceAgent is attempting to register itself before the Service Manager was loaded -- in our case never!
The Service Manager Always Loads First
Apparently we forgot to load the Service Agent. Let's try again, but this time we will add the Java class name of the Service Manager before the Demo Service Agent:
[demo_service] % ak com.cometway.ak.ServiceManager DemoServiceAgent
[AK] Comet Way Agent Kernel 2.7 Final
[000_AgentKernel] Starting on 2007/05/25 18:18:03.021 EDT
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2007/05/25 18:18:03.056 EDT
[001_XMLStartupAgent] Loading agents from ak.xstartup
[001_XMLStartupAgent] Loading agents from .
[000_AgentKernel] Creating agent com.cometway.ak.ServiceManager
[1003_ServiceManager] Starting on 2007/05/25 18:18:03.147 EDT
[000_AgentKernel] Creating agent DemoServiceAgent
[1004_DemoServiceAgent] Starting on 2007/05/25 18:18:03.176 EDT
[1003_ServiceManager] Registered DemoServiceAgent as demo_service
That's much better! In the output, you can see:
- The Agent Kernel starts.
- The Service Manager is started by the Agent Kernel.
- The Demo Service Agent is started by the Agent Kernel.
- The Demo Service Agent registers itself with the Service Manager as "demo".
- The Agent Kernel exits.
Continue to the next article to see how Demo Service Agent is used.
Description: In this example, we use the Service Manager to register with the Service Agent.
Updated: Mon May 19 06:31:13 EDT 2008