Interacting with JADE Agents from non-JADE environments using the JMS-MTP
Authors: Edward Curry (ECRG, NUI, Galway), Eduardo
H. Ramïrez (Monterrey Institute of Technology).
Date: January 14, 2004
JMS-MTP version 1.0
Java platform: Sun JDK 1.4 Linux
JADE version 2.1
Purpose
The aim of this tutorial is to illustrate how to interact with JADE agents
from a J2EE-based (non-agent) Java environment using the JMS-MTPs.
Prerequisites
This tutorial assumes the following:
- You have basic knowledge of the Java language and have Java (JDK1.4) properly
installed.
- You have basic knowledge of the JADE framework and have JADE (JADE-3.1
recommended) properly installed.
- You have basic knowledge of the JMS-MTP and are familiar with the
JMS-MTP Tutorial.
- You have the JMS-MTP installed and configured with your Jade platform,
see the JMS-MTP Tutorial for more details.
- You have a JMS provider installed and configured to work with the JMS-MTP.
- You have the Jakarta Ant tool properly installed.
- You have the JADE PingAgent examples installed and working.
Compiling
The JADE Makefile will not compile the JMS-MTP or the examples used in this tutorial.
To compile the JMS-MTP and its examples you have to use the 'build.xml' ant-file
located in the add-ons/jmsmtp directory. The following rules are available:
- ant compile - compiles the JMS-MTP classes
- ant lib - creates the jmsmtp.jar archive
- ant clean - removes the compiled classes and the .jar archive
- ant doc - creates javadoc for the JMS-MTP
Usage
Activating the example from the command line
First start your JMS Provider and setup the required queue (see next section),
then start the JADE container using the JMS-MTP and running a PingAgent, 'ping',
from the JADE examples. In order to run the example client, the jmsmtp-examples.jar
and jmsmtp-util.jar files must be on the classpath. Here is an example of how
you would start the example assuming you are in the root of the Jade directory:
java -classpath ./add-ons/jmsmtp/lib/jmsmtp-examples.jar:./add-ons/jmsmtp/lib/jmsmtp-util.jar
ie.nuigalway.ecrg.jade.jmsmtp.examples.JadeQueueSender
( for Unix )
or
java -classpath .\add-ons\jmsmtp\lib\jmsmtp-examples.jar;.\add-ons\jmsmtp\lib\jmsmtp-util.jar
ie.nuigalway.ecrg.jade.jmsmtp.examples.JadeQueueSender
( for Windows )
The support files for your JMS provider are assumed to be on the classpath.
Activating the example using Ant
Alternatively, you must use the following ant targets to execute the example
- Use exec_example_container to launch a JADE container with the relevant
settings (JMS-MTP installed/ping agent started)
- Use exec_example_client to launch the client
Configuring the JadeQueueSender Example
The default settings for the JadeQueueSender are listed below, the default
JMS provider is OpenJMS running on localhost. The constants/settings will be
used in the following sections of code:
- JMS Providers Initial Context Factory : ICF = "org.exolab.jms.jndi.InitialContextFactory"
- JMS Providers URL : PROVIDER_URL = "rmi://localhost:1099"
- Queue to send Reply : REPLY_TO_DEST = "JadeQueueSenderExample"
- Queue JADE platform is listening to : PLATFORM_DEST = "jade/localhost"
- JMS Address to reply to : REPLY_TO_JMSMTP_ADDRESS = "jms:openjms|xml|non_persistent|||rmi://localhost:1099/JadeQueueSenderExample"
- Name of PingAgent : PING_AGENT_NAME = "ping@localhost:1098/JADE"
These setting can be changed in the ie.nuigalway.ecrg.jade.jmsmtp.examples.JadeQueueSender
class.
(Note: for new setting to take effect you will need to recompile the example)
JMS-MTP Example Description
The example performs the following steps:
- Sends a 'ping' message to a PingAgent within a JADE platform
- Listens for a 'pong' (alive) response from the PingAgent.
Utility Classes
This example uses two utility classes (located in jmsmtp-util.jar) to encode
and decode messages used to interact with the PingAgent. These utility classes
are:
- ie.nuigalway.ecrg.jade.jmsmtp.util.FipaXMLUtil - Used to encode/decode
FIPA XML message
- ie.nuigalway.ecrg.jade.jmsmtp.util.MapMessageUtil - Used to encode/decode
JMS-MTP custom MapMessage
Sending a Message
// Assuming connection to JMS provider setup
jadeQueue = (Queue) ctx.lookup(PLATFORM_DEST);
QueueSender sender = session.createSender(jadeQueue);
//Create an ACL Message object using JADE API.
ACLMessage msg = new ACLMessage(ACLMessage.QUERY_REF);
//Sender properties
AID s_aid = new AID();
s_aid.setName("JadeQueueSenderExample");
// Use a JMS-MTP Address as the return address for the agent to reply to
// This client will listen to 'rmi://localhost:1099/JadeQueueSenderExample'
// Set the other options of the address to your desired setting,
// in this case non persistent FIPA XML messages
s_aid.addAddresses(REPLY_TO_JMSMTP_ADDRESS);
msg.setSender(s_aid);
// Alternatively we are also able to get the agent to reply to another agent. i.e.:
// Reply to self: s_aid.setName("ping@mogador:1098/JADE");
// or reply to another agent: s_aid.setName("personal_agent@mogador:1098/JADE");
// Setup the PingAgents properties (This is the agent that will receive the message)
AID pingAgent = new AID();
pingAgent.setName(PING_AGENT_NAME);
msg.addReceiver(pingAgent);
// Set the message content "ping"
msg.setContent("ping");
msg.setDefaultEnvelope();
// Encode map message
Message message = session.createMapMessage();
mapUtil.encode((MapMessage) message, msg.getEnvelope(), msg.toString());
// Or use a FIPA XML Message
//TextMessage message = session.createTextMessage();
//message.setText(xmlUtil.encode(msg.getEnvelope(), msg.toString()));
// Send the message
sender.send(message);
|
Receiving a Message
// Assume listener is set to listen to REPLY_TO_DEST
public void onMessage(Message msg) {
StringBuffer payload = new StringBuffer();
Envelope env = new Envelope();
if (msg instanceof TextMessage) {
// Text Message received
TextMessage tm = (TextMessage) msg;
try {
// Decode XML
env = xmlUtil.decode(tm.getText(), payload);
} catch (Exception jmse) {
// Error in JMS TextMessage Extraction
}
} else if (msg instanceof MapMessage) {
// Map Message received
MapMessage mm = (MapMessage) msg;
try {
// Decode MapMessage
env = mapUtil.decode(mm, payload);
} catch (Exception jmse) {
// Error in JMS MapMessage Extraction
}
}
// Message decode
System.out.println("Message body:" + payload.toString());
}
|
Known Issues
- The current release of the JMS-MTP only supports messages in the form of
TextMessage and MapMessage.
JADE is
a trademark of CSELT.
JADE has been developed jointly by CSELT and the Computer Engineering Group of the University of Parma.
The JMS-MTP was developed in the Enterprise Computing Research Group
(ECRG) at the National University of
Ireland, Galway by Edward
Curry.
The support of the Informatics Research Initiative of Enterprise Ireland is gratefully
acknowledged.
Copyright 2002/2003/2004 Enterprise Computing Research Group.