RMI method invocation

RMI method invocation

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?

When writing distributed applications, it is usually desirable to minimize the amount of communication. This means you want to reducethe 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 mainclient program is contained in UpdateList, which uses the ClientList class.

/*** * $RCSfile:,v $ ***/import java.rmi.*;public interface ListData extends Remote {  public String[] getData() throws RemoteException;}/*** * $RCSfile:,v $ ***/import*;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( 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:,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 ");      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;    }  }}


Share the Post: