Configuring a JXTA Peer in Spring

Important update: This article is old and out of date. This method will no longer work with the current version of the JXTA platform. I am looking into what changes might be required to the platform and/or the example. I’ll post the results in a new entry and link to it here when it’s ready. I notice many people searching for JXTA+Spring configuration and landing here. Sorry for the inconvenience. VW, 23/07/2008

The new net.jxta.platform.NetworkConfigurator class provides a clean and straightforward way to configure an instance of the JXTA platform without a UI. It can be used programmatically (see the main() method for an example of stand-alone use) or—because it is a Java Bean—declaratively deployed inside containers like Spring.

Here is a simple example of a Spring context file, applicationContext-rdv.xml, which configures a Rendezvous Server:


    
        
        .jxta
        
        2752
        
        
             urn:jxta:uuid-XXXXXXXXXXXXXXXXXXXXXXXXXX...
        
        
        
            uuid-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        
        
           MyNetPG
        
        
            My Infrastructure Group
        
        
        
           com.mycompany RDV1
        
        
           Rendezvous Node
        
        user
        pwd
        
        9700
        9701
    

    
        
    

And here’s what MyJxtaBean.java might look like, in part:


public class MyJxtaBean {

    private NetworkConfigurator netConfig;
    
    private PeerGroup infrastructureGroup;
    
    public MyJxtaBean() {}
    
    public setNetConfig(NetworkConfigurator netConfig) {
        this.netConfig = netConfig;
    }
    
    public synchronized void start() {

        /*
        * Simplest case, we don't overwrite an existing configuration, nor
        * do we load and alter/ammend it. Only if there is no PlatformConfig
        * available, construct a new one using the bean's properties.
        */
        if (!netConfig.exists()) {
            try {
                netConfig.save();
            } catch (IOException io) {
                System.out.println("Error saving PlatformConfig");
            }
        }
          
        try {
            infrastructureGroup = PeerGroupFactory.newNetPeerGroup();
        } catch (PeerGroupException pge) {
            System.out.println("Couldn't create NetPeerGroup");
            System.exit(1);
        }
    }
}

Now all your applicaton has to do is get the MyJxtaBean from the context and call start() to start the JXTA platform.


    applicationContext = 
        new ClassPathXmlApplicationContext("applicationContext-rdv.xml");
    myPlatform = (MyJxtaBean)applicationContext.getBean("myJxtaBean");
    myPlatform.start();

There’s more to writing a JXTA application, of course, but I hope this example demonstrates how NetworkConfigurator takes some of the mystery out of JXTA plaform configuration, while making it easy to combine JXTA and Spring.

Update: April 10, 2007 Christopher Marsh-Bourdon points out a flaw in this example when used with Spring 2.0.1. It’s related to the specification of the JXTA home directory. You can read Christopher’s analysis of Spring’s FileEditor Anomaly and solution to the probelm here. Thanks, Christopher.

4 thoughts on “Configuring a JXTA Peer in Spring”

  1. Thanks for posting this. I believe this is a JXTA <> Spring first.

    Can you say a bit about why you are using Spring in this overall context? Spring neophytes may learn something from your choices.

  2. There is a problem in that if you set the home property in Spring (2.x) and that directory does not exist, Spring will error when loading the configuration. This is not a direct problem with JXTA, but one that could bite you the first time you run a JXTA application configured in this manner.

    I am looking at way to work around this and I’ll let you know if there is one.

Leave a Reply

Your email address will not be published. Required fields are marked *