Three Agents Are Better Than Two
So far not much is really happening. The Agent Kernel quits after starting DemoServiceAgent, because we have no other Agents to load. There's probably no point in registering a Service Agent that isn't going to be used, so let's create a buddy for DemoServiceAgent:
import com.cometway.ak.Agent;
public class DemoClientAgent extends Agent
{
public void start()
{
DemoServiceAgent service = (DemoServiceAgent) getServiceImpl("demo");
println("Accessing the DemoServiceAgent: " + service);
service.run();
println("Completed.");
}
}
Go ahead and compile and run this agent along with com.cometway.ak.ServiceManager and DemoServiceAgent and you should see similar output:
[demo_service] % jc DemoClientAgent.java
Compiling...
DemoClientAgent.java
[demo_service] % ak com.cometway.ak.ServiceManager DemoServiceAgent DemoClientAgent
[AK] Comet Way Agent Kernel 2.7 Final
[000_AgentKernel] Starting on 2007/05/25 18:43:23.963 EDT
[000_AgentKernel] Creating agent com.cometway.xml.XMLStartupAgent
[001_XMLStartupAgent] Starting on 2007/05/25 18:43:23.985 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:43:24.097 EDT
[000_AgentKernel] Creating agent DemoServiceAgent
[1004_DemoServiceAgent] Starting on 2007/05/25 18:43:24.118 EDT
[1003_ServiceManager] Registered DemoServiceAgent as demo
[000_AgentKernel] Creating agent DemoClientAgent
[1005_DemoClientAgent] Starting on 2007/05/25 18:43:24.133 EDT
(1003_ServiceManager) Requested: demo 2007/05/25 18:43:24.135 EDT
[1005_DemoClientAgent] Accessing DemoService...
[1004_DemoServiceAgent] The Demo Service Agent has been accessed.
[1005_DemoClientAgent] Completed.
What happened here is simply a continuation of the previous article with a few additional steps enacted by our newly added Demo Client Agent:
- 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 Demo Client Agent is started by the Agent Kernel.
- The Demo Client Agent requests a reference to the "demo" service using the method
Agent.getServiceImpl().
- The Demo Client Agent calls
DemoServiceAgent.run().
- The Demo Service Agent prints a message that it was accessed.
- The Demo Client Agent prints a message that it has completed.
- The Agent Kernel exits.
This is exactly what we wanted to happen and is the basis for how all Agents find and communicate with each other in the Agent Kernel. Always remember:
- Start the Service Manager first.
- Start your Service Agents next.
- Start the agents that use your Service Agents.
Attempting to access a Service Agent or Service Manager that does not exist yet will always result in failure.
Description: Create a client Agent that finds and uses the DemoServiceAgent from the previous article.
Updated: Mon May 19 06:31:08 EDT 2008