O'Reilly logo

Mobile Agents by Wilhelm R. Rossak, Peter Braun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.2 Survival 359
agency as soon as its run method has terminated. Otherwise, the agent
remains alive, but in state Waiting until the user, the agency, or any
other plugin restarts it. In this example it can only be activated by an
external event. Later, when we use asynchronous messaging, the agent
may be awakened by a message from another agent or from its owner. To
restart an agent, type in the runagent command, followed by the agent’s
full name. However, the agent still does not do anything sensible except
print Hello World messages to its owner. Every time you start the agent, a
new message will be printed. To eventually delete the agent manually, you
have to use the stopagent command, followed by the agent’s name. An
agent can also release the connection to the Survival service if it wants
to die.
In the following sections, we will introduce all the basic services that are
shipped with the Tracy agency, beginning with the Survival plugin.
10.2 Survival
The most important job of the Survival plugin is to prevent an agent from
being disposed after its run method has terminated. It can also be used to
schedule future agent execution. For example, an agent can define that it
wants to be started once at a specific time or time interval. An agent can
also define that it wants to be started periodically.
The complete API of the Survival service consists of the following
methods:
public void schedule( Date date )
Starts the agent once at the given date.
public void schedule( Date firstDate, long period )
Starts the agent at the given firstDate and then every period milliseconds.
public void schedule( long delay )
Starts the agent once after the given delay in milliseconds.
public void schedule( long delay, long period )
Starts the agent after the given delay in milliseconds and then every period milliseconds.
public void cancel()
Deregisters from this service. Stops every previously defined timer.
360 Chapter 10 Programming Agents with Tracy
The following example shows an agent that registers with the survival
service to be started every five seconds.
1 package examples.agents;
2
3 import de.fsuj.tracy2.kernel.Context;
4 import de.fsuj.tracy2.plugins.survival.interfaces.IAgentSurvivalContext;
5 import de.fsuj.tracy2.plugins.shell.interfaces.IAgentShellContext;
6
7 public class MyFirstAgent implements java.lang.Runnable
8{
9 private int state = 0;
10
11 public void run()
12 {
13 IAgentSurvivalContext survivalCxt;
14 IAgentShellContext shellCxt;
15
16 switch( state )
17 {
18 case 0:
19 if( (survivalCxt = Context.getContext( "survival" )) != null );
20 {
21 survivalCxt.schedule( 1000, 5000 );
22 }
23 state = 1;
24 break;
25 case 1:
26 if( (shellCxt = (IAgentShellContext)Context.getContext( "shell" )) != null );
27 {
28 shellCxt.writeToUser( "Hello World!" );
29 }
30 break;
31 }
32 }
33 }
This example shows an agent that uses a finite state machine. When the
agent is first started, it is in state 0, where it registers with the survival
service. In line 21, the agent defines that it wants to be restar ted every
5 seconds after a first initial delay of 1 second. After that it switches to
state 1 and terminates. As previously mentioned the agent is not disposed
of because it is registered with at least one service. The plugin that provides

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required