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:

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:

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

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:

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:

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:

Sending a Message

// Assuming connection to JMS provider setup
jadeQueue = (Queuectx.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((MapMessagemessage, 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 = (TextMessagemsg;

    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 = (MapMessagemsg;

    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


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.