ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Hello JXTA!
Pages: 1, 2, 3

Now that the "server" end of the P2P service has been written, it's time to write the "client" end. The HelloService interface provides one method, sendMessage, that will open an output pipe to a random peer's input pipe and send it a message.

  // the method that the application may call to send
  // a message to another random peer running the HelloService
  public void sendMessage() {
  // send out a query to all peers in our peer group
  // looking for advertisements of pipes that are
  // prefixed with "HelloService:" as the pipes
  // for the HelloService are all named like that
  this.discovery.getRemoteAdvertisements( null, Discovery.ADV, "name", "HelloService:*", 10 );

  // wait 5 seconds for all the responses to come
  // in and be incorporated into the platform's cache
  try {
    Thread.sleep( 5000 );

  } catch( InterruptedException error ) {

  try {
    // retrieve a list of all the advertisements
    // from our cache of responses that match the
    // query
    Enumeration pipeEnumeration = this.discovery.getLocalAdvertisements( Discovery.ADV, "name", "HelloService:*" );
    if( pipeEnumeration != null ) {
      // look for the first PipeAdvertisement    
    // out of all the advertisements that
    // have been returned
    PipeAdvertisement pipeAdvertisement = null;
    while( pipeEnumeration.hasMoreElements() ) {
      Advertisement advertisement = (Advertisement)pipeEnumeration.nextElement();
      if( advertisement instanceof PipeAdvertisement ) {
        pipeAdvertisement = (PipeAdvertisement)advertisement;

    if( pipeAdvertisement != null ) {
      try {
        // construct a non-blocking output pipe
      // to the pipe that we found being
      // advertised
      OutputPipe helloPipe = this.pipes.createOutputPipe( pipeAdvertisement, Pipe.NonBlocking, -1 );

      // construct a message with the sender
      // name's as part of the object
      Message helloMessage = this.pipes.createMessage();
      helloMessage.push( "sender", new ByteArrayInputStream( this.peerGroup.getPeerID().toString().getBytes() ) );

      // send the message down the pipe
      helloPipe.send( helloMessage );
      System.out.println( "message sent!" );
      } catch( IOException error ) {
        System.err.println( "problem sending the message to the peer" );
      } finally {
    } else
      System.err.println( "could not find a pipe to talk to" );

  } catch( IOException error ) {
    System.err.println( "problem retrieving advertisements from the local cache" );





A service is useless without an application to call it. The next step is to create an application that will call the HelloService.

public class HelloApp
extends Object
implements Application {
  // a handle to the running hello service on this platform
  private HelloService helloService;
  // initialize the application
  public void init( PeerGroup group, Advertisement advertisement )
  throws PeerGroupException {
    // get a handle to the hello service
  try {
    this.helloService = (HelloService)group.lookupService( "HelloService" );

  } catch( ServiceNotFoundException error ) {
    System.err.println( "could not find the hello service" );
    throw new PeerGroupException();



  // start the application -- all that is done here is to call
  // the sendMessage method on the hello service
  public int startApp( String[] arg ) {
    // call the one method on the service  

  return 0;


  // does not actually need to do anything
  public void stopApp() {



Related articles:

The JXTA Position -- an introduction

JXTA Takes Its Position -- an analysis of the framework, by Rael Dornfest

The JuxtaNet -- a look at how JXTA may enable a network as popular as Gnutella, by Kelly Truelove

Learning the JXTA Shell -- a tutorial by Rael Dornfest

on this articleIs JXTA all talk? If not, should JXTA be jXTA? Join the debate.

Running Hello JXTA

Running this code is harder than is ideal. The JXTA Java platform is presently optimized to run the JXTA Shell. Launching the Shell prepares the platform to run JXTA (it creates a jxtaConfig file and a PlatformPeerGroup advertisement directory in the current working directory), so you have to launch the Shell once to allow it to configure the node. Then you can terminate the shell and edit the jxtaConfig and the PlatformPeerGroup files to make the platform do what we want it to do.

The jxtaConfig properties file contains information that's used to create the PlatformPeerGroup advertisement file that the node uses to configure itself. There are two lines to edit. First, change the value of the isRendezvous property to "true". This peer will act as a rendezvous for its specific peer group (a discussion of rendezvous and router peers is beyond the scope of this article). Second, change the value of InitialNetPeerGroupAppCode to the class name of the HelloApp. If you look carefully, you will notice InitialNetPeerGroupAppCodeURL which ideally would be set to a URL from which the platform could download the application code. This property will be ignored if the application classes are in the classpath when the platform's invoked.

PlatformPeerGroup is the group advertisement (XML document) for the group that the platform boots into. Modify the value of the Name tag to HelloPeerGroup, which will cause this platform to boot into its own custom peer group, separate from the default network peer group. Then modify isRendezvous to "true". Finally, we need to add a new service advertisement to this peer group so that the platform will boot the HelloService. Add the following advertisement at the end of the Services element, which is the advertisement passed to the HelloServiceImpl upon initialization:


When the platform is restarted, it will boot into the HelloPeerGroup while running the HelloService. It will create an input pipe, create an output pipe, connect the two together, and send a message. If you are lucky (or are running two peers), then the application will connect to another service and send it a message containing its peer ID. Otherwise, the service will probably connect to its own pipe and sending itself a message.

Sun's Java binding for JXTA is a long way from being completed. The code needs to mature before developers can use it to develop robust P2P applications. However, by building toy projects and learning the terminology along with the basic concepts, developers will be better poised to use it when it matures.

Raffi Krikorian makes a career of hacking everything and anything. Professionally, he is the founding partner at Synthesis Studios: a technological design and consulting firm that orchestrates his disjointed train of thought.

Return to ONJava.com.