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 < data.length; i++)      add(data[i]);  }}/*** * $RCSfile:,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;    }  }}
See also  5 Benefits of Using Time Tracking Software

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist