starting multiple instances of one agent with (some) dynamic properties

10 views
Skip to first unread message

CBF

unread,
Oct 2, 2007, 4:09:11 PM10/2/07
to Comet Way Agent Kernel
hi,

i am struggling with following problem:
one agent FOO will assign work to multiple instances of another agent
BAR
The configuration for all agents is given in "ak.xstartup".
Some properties of BAR are set by FOO prior to starting a specific
instance. Other properties should be taken from "ak.xstartup"-file.

How to set up that configuration? Where is the best place to store
configuration-infos for such agents? (Using "setDefault" in Java-Code
is not an option.)

Specific example:
FOO sends sql-updates via JDBCAgent to multiple databases. So DB-Name,
even db-driver may be dynamic. Other properties like username should
be configured one time.

kind regards
Clemens

PS: Next question would be: How to start these multiple instances of
agent BAR in different OS-Threads on multi-processor-machines?

Message has been deleted
Message has been deleted

Paul

unread,
Oct 2, 2007, 5:55:35 PM10/2/07
to Comet Way Agent Kernel
Thank you for using our Google Groups forum!

It sounds like what you want is to load FOO from the ak.xstartup file,
and subsequently have FOO create and startup BAR for as many instances
as are necessary to get the job done. This kind of setup is relatively
simple to create with a little programming in FOO.

/* Bar.java */

import com.cometway.ak.Agent;

public class Bar extends Agent
{
public void initProps()
{
setDefault("message", "Hello!");
}

public void start()
{
println("Doing BAR... " + getTrimmedString("message"));
}
}


/* Foo.java */

import com.cometway.ak.Agent;
import com.cometway.ak.AK;
import com.cometway.ak.AgentControllerInterface;
import com.cometway.props.Props;

public class Foo extends Agent
{
public void initProps()
{
setDefault("bar_message", "Hello!");
}

public void start()
{
println("Doing Foo... ");

String bar_message = getTrimmedString("bar_message");

Props p = new Props();
p.setProperty("classname", "Bar");
p.setProperty("message", bar_message);

AgentControllerInterface agent =
AK.getAgentKernel().createAgent(p);
agent.start();

p = new Props();
p.setProperty("classname", "Bar");
p.setProperty("message", "This is a differently configured
Bar.");

agent = AK.getAgentKernel().createAgent(p);
agent.start();
}
}

=-=-=-=-=-=-=-=-=-=-=
[columbo:~/projects/20071002] paul% ak Foo
[071002-173230 AK] Comet Way Agent Kernel 2.7 Final 04-24-2007 -
Patience.
[071002-173230 000_AgentKernel] Starting on 2007/10/02 17:32:30.214
EDT
[071002-173230 000_AgentKernel] Creating agent
com.cometway.xml.XMLStartupAgent
[071002-173230 001_XMLStartupAgent] Starting on 2007/10/02
17:32:30.227 EDT
[071002-173230 001_XMLStartupAgent] Loading agents from ak.xstartup
[071002-173230 001_XMLStartupAgent] Loading agents from .
[071002-173230 000_AgentKernel] Creating agent Foo
[071002-173230 1003_Foo] Starting on 2007/10/02 17:32:30.250 EDT
[071002-173230 1003_Foo] Doing Foo...
[071002-173230 000_AgentKernel] Creating agent Bar
[071002-173230 1004_Bar] Starting on 2007/10/02 17:32:30.253 EDT
[071002-173230 1004_Bar] Doing BAR... Hello!
[071002-173230 000_AgentKernel] Creating agent Bar
[071002-173230 1005_Bar] Starting on 2007/10/02 17:32:30.256 EDT
[071002-173230 1005_Bar] Doing BAR... This is a differently configured
Bar.
=-=-=-=-=-=-=-=-=-=-=

This is just a simple example that illustrates how to configure,
create, and startup two instances of Bar from Foo.

In your specific example using JDBCAgent, you would do nearly the same
thing except specify the different properties for the JDBCAgent that
you will be using.

...
String jdbc_username = getTrimmedString("jdbc_username");
String jdbc_password = getTrimmedString("jdbc_password");

Props p = new Props();
p.setProperty("classname", "MyJDBCAgent");
p.setProperty("jdbc_driver", "sun.jdbc.odbc.JdbcOdbcDriver");
p.setProperty("jdbc_url", "jdbc:odbc:mydb");
p.setProperty("jdbc_username", jdbc_username);
p.setProperty("jdbc_password", jdbc_password);

agent = AK.getAgentKernel().createAgent(p);
agent.start();

p = new Props();
p.setProperty("classname", "MyJDBCAgent");
p.setProperty("jdbc_driver",
"net.sourceforge.jtds.jdbc.Driver");
p.setProperty("jdbc_url", "jdbc:jtds:sqlserver://localhost/
ANOTHERDB");
p.setProperty("jdbc_username", jdbc_username);
p.setProperty("jdbc_password", jdbc_password);

agent = AK.getAgentKernel().createAgent(p);
agent.start();
...

That's pretty simple. It will create and start the first MyJDBCAgent,
wait for it to finish, then do the same for the 2nd MyJDBCAgent.

Now about starting them in different OS threads... This depends
heavily on your JVM implementation. Java threads are usually
implemented as OS threads and will be distributed across multiple CPUs
at the kernel's discretion. This means that you need to startup each
JDBCAgent on a different java Thread. Are you familiar with thread
programming in Java? If not I can provide another short example that
should get you going.

-pc

CBF

unread,
Oct 3, 2007, 5:13:55 PM10/3/07
to Comet Way Agent Kernel
Thanks for the info. I understand now, that there are 2 options for
setting properties for BAR.
a) The properties are placed in BAR's initProps()-Method.
b) The properties are placed in FOO's ak.xstartup-properties and
subsequently passed to BAR's new instances.

This helps a lot.
I am familiar with threads, but an example is always welcome :-)
regards
Clemens

Reply all
Reply to author
Forward
0 new messages