Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Nov 11, 1998

RMI method invocation

Question:
I want my RMI server application to populate a java.awt.List with data on the client with a method invocation. What's the best way to do this?

Answer:
When writing distributed applications, it is usually desirable to minimize the amount of communication. This means you want to reduce the number of messages, and aggregate data into one message when possible. In other words, it is generally better to send a few large messages than a lot of little ones. It can often be difficult to do this when using distributed objects because the programming model tends to make code look just like a nondistributed program. In your situation, you want to avoid having the server modify the client's list one item at a time. Rather, you want to send all the data to the client, and let the client update the list.

In the following example, ListData and ListDataImpl comprise the server. After compiling these, rmic must be run on ListDataImpl. The main client program is contained in UpdateList, which uses the ClientList class.

/***
 * $RCSfile: ListData.java,v $
 ***/

import java.rmi.*;

public interface ListData extends Remote {
  public String[] getData() throws RemoteException;
}


/***
 * $RCSfile: ListDataImpl.java,v $
 ***/

import java.net.*;
import java.rmi.*;
import java.rmi.server.*;

public class ListDataImpl extends UnicastRemoteObject implements ListData {
  String[] _data = { "1", "2", "3", "4", "5" };

  public ListDataImpl() throws RemoteException { }
  
  public String[] getData() throws RemoteException {
    return _data;
  }

  public static final void main(String args[]) {
    String hostname;
    ListDataImpl listData;

    try {
      InetAddress address = InetAddress.getLocalHost();
      hostname = address.getHostName();
    } catch(java.net.UnknownHostException e) {
      e.printStackTrace();
      return;
    }

    System.setSecurityManager(new RMISecurityManager());

    try {
      String url = "//" + hostname + "/ListServer";
      listData = new ListDataImpl();
      Naming.rebind(url, listData);
      System.out.println("Bound ListServer in registry: " + url);
    } catch(Exception e) {
      e.printStackTrace();
      return;
    }
  }
}


/***
 * $RCSfile: ClientList.java,v $
 ***/

import java.awt.*;
import java.awt.event.*;

public class ClientList extends List {

  public ClientList() { }

  public ClientList(int rows) { super(rows); }

  public ClientList(int rows, boolean multipleMode) {
    super(rows, multipleMode);
  }

  public synchronized void setData(String[] data) {
    removeAll();
    for(int i=0; i < data.length; i++)
      add(data[i]);
  }

}


/***
 * $RCSfile: UpdateList.java,v $
 ***/

import java.awt.*;
import java.awt.event.*;

import java.rmi.*;

public final class UpdateList {
  public static final String[] DATA = {
    "One", "Two", "Three", "Four", "Five"
  };

  public static void main(String[] args) {
    ClientList list;
    Frame frame;
    WindowListener exitListener;
    String hostname;
    ListData listData;

    if(args.length < 1) {
      System.err.println("Usage: UpdateList ");
      return;
    }

    hostname = args[0];

    exitListener = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	Window window = e.getWindow();
	window.setVisible(false);
	window.dispose();
	System.exit(0);
      }
    };

    list = new ClientList(4);
    frame = new Frame();
    frame.addWindowListener(exitListener);
    frame.add(list);
    frame.pack();
    frame.setVisible(true);

    list.setData(DATA);

    // Get remote data and update list.
    try {
      listData = (ListData)Naming.lookup("//" + hostname + "/ListServer");
      list.setData(listData.getData());
    } catch(Exception e) {
      e.printStackTrace();
      return;
    }
  }
}

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap