March 5, 2021
Hot Topics:

JXTA-A Messaging System for Wireless Java

  • By Bilal Siddiqui
  • Send Email »
  • More Articles »

Each element, in turn, consists of four components or fields. The first field is the name of the element. For example, the name of the first element is "proxy:response". This name resembles the name of an XML element because it contains a namespace prefix (proxy) and an element name (response). You can create your application specific namespaces and use them to create messaging logic.

The second field (application/octet-stream) is the MIME type of the data. The third field is the length of the data field, and the fourth field is the data itself. For example, the MIME type of proxy:response data is "application/octet-stream", data length is 6, and the actual data stream is "result".

Just look at the various elements. The proxy:requestId element specifies the identifier of the request that corresponds to this response. The other three important elements of this search query response are the id (proxy:id), name (proxy:name), and type (proxy:arg) of the pipe that you searched for. You will need these three elements when you want to send messages to this pipe.

Sending Messages

Sending messages is a two-step procedure. First, you author a message and then send it to the relay.

// Author a message.
Element[] elementArray = new Element[2];
String data = "Bilal";
elementArray[0] = new Element("SenderName", data.getBytes(),
                               null, null);
data = "Hello from Bilal";
elementArray[1] = new Element("SenderMessage", data.getBytes(),
                               null, null);
Message message = new Message (elementArray);

The Element and Message classes used in the preceding code are part of the JXTA4J2ME implementation.

As we've already seen, a JXTA4J2ME message consists of elements. So during message authoring, we'll need to author each element separately (by calling the Element constructor), put all those elements in an array, and then pass on the element array to the Message constructor, which will pack them together to form the complete message.

The Element constructor takes four parameters:

  1. Name of the element (String)
  2. Data to be sent (array of bytes)
  3. Namespace identifier (String)
  4. MIME type of the data (String)

We have only specified the first two fields in the preceding code, passing null as the value of the third and fourth fields. The result will be that an empty namespace string ("") and the default MIME type ("application/octet-stream") will be sent in the element.

After you have finished authoring the individual elements of the message array, you will simply pass on the elements array to the message constructor.

The second step is to send the message to the relay. You simply will call the send method of the PeerNetwork class for this purpose, passing the authored message along with the method call:

//Send the authored message to the relay.
int messageID = peerNetwork.send(pipeName, pipeID, pipeType);

The send method call takes four parameters. The first three are the name, id, and type of the pipe that you want to use to send the message. All the three parameters are of the String type. Recall that these three parameters were received in response to the pipe search query discussed above. The fourth parameter is the message we just authored.

Checking for Responses

Have a look at the following lines of code:

Message message = null;
while (1) {
  message = peerNetwork.poll(1000);
  if (message == null) break;

  //Here is the place to add logic to store or process the
  //incoming message.
} //while

The while block will keep on polling until the poll method returns null, which happens only when the relay has no more messages to return.

The poll method takes just one parameter, which is the time in milliseconds to wait for the response. If the response from the relay is not received within this time interval, the method will return null. Specifying zero as the value of this parameter will mean that it waits forever.


In this article, we discussed the features that are common to all messaging applications. We also elaborated how JMS provides messaging features and how JXTA4J2ME matches the same functionality for J2ME devices. At the end, we followed a step-by-step how-to tutorial, explaining how to perform the basic tasks in a JXTA4J2ME-based messaging application.

Next time, we will go into the details of the three classes in the JXTA4J2ME implementation (PeerNetwork, Message, and Element). We will also see how a JXTA relay coordinates with edge devices to provide the heavy lifting part of a messaging application.

About the Author

Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the co-founder of WaxSys, a company focused on simplifying e-Business. After graduating in Electronics Engineering from the University of Engineering and Technology, Lahore, in 1995, he began designing software solutions for industrial control systems. Later, he turned to XML and used his experience programming in C++ to build Web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. He is a technology evangelist and a frequently published technical author. Bilal has also contributed to a couple of books, namely Java P2P Unleashed and Web Services Business Strategies and Architectures. Readers may contact Bilal at bsiddiqui@waxsys.com.


JXTA4J2ME (http://jxme.jxta.org/) comes with sample programs. Try the demo chat application that is included in the JXME zip (http://download.jxta.org/stablebuilds/libs/jxme.zip) and read a document (http://jxme.jxta.org/Chat_Demo.html) that shows how the demo works.

Page 3 of 3

This article was originally published on October 10, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date