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: 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;    }  }}
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved