RMI and ArrayLists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trispad
    New Member
    • Feb 2007
    • 2

    #1

    RMI and ArrayLists

    Does anyone know how to get at the data within and ArrayList using RMI. I have looked at the source code for ArrayList and the Object Array elementData is a transient variable. Therefor when elementData is not serialized when a call for it is made through RMI. I have tried using:
    Code:
    private final ObjectStreamField serialPersistentFields = new
               ObjectStreamField( "elementData", Object.class );
    to get around the fact that elementData is transient. But then I run into the problem that the Object class is not serializable. I have been searching for some time now and have not found an answer or a workaround. Does anyone here know a solution?

    Thanks much
    -trispad
  • trispad
    New Member
    • Feb 2007
    • 2

    #2
    Does any one know a forum that I can try??

    Thanks

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      have a look at this simple example which uses RMI to pass an ArrayList to a server and get it back

      The interface
      Code:
      // RMIarrayList.java  - the interface
      
      import java.util.*;
      
      import java.rmi.*;                 
      public interface RMIarrayList extends Remote
      {
        public ArrayList passArrayList(ArrayList a) throws RemoteException;
      }
      the server
      Code:
      // RMIarrayListImpl.java
      
      import java.rmi.*;
      import java.net.*;
      import java.util.*;
      
      public class RMIarrayListImpl extends java.rmi.server.UnicastRemoteObject
             implements RMIarrayList
      {
      
        public RMIarrayListImpl() throws RemoteException
        {
          super();
        }
      
        // receive an array list, print it, add an element and return it
        public ArrayList passArrayList(ArrayList a) throws RemoteException
        {
          System.out.println(" RMI ArrayList server received array list " + a);
          a.add( "goodbye" );      
          return a;
        }
      
        public static void main(String args[])
        {
          try
          {
            // bind to RMI server
            RMIarrayListImpl h = new RMIarrayListImpl();
            Naming.rebind("rmi://localhost/RMIarrayList", h);
            System.out.println("RMI ArrayList server ready");
          }
          catch (RemoteException re)
          {
            System.out.println("Remote Exception " + re);
          }
          catch (Exception e)
          {
            System.out.println(" Exception " + e);
          }
        }
      }
      the client
      Code:
      // RMIarrayListClient.java
      
      import java.rmi.*;
      import java.util.*;
      
      public class RMIarrayListClient
      {
        public static void main(String args[])
        {
          String host="localhost";    // RMI host name
          
          /* System.setSecurityManager(new RMISecurityManager()); */
          try
          {
            // lookup RMI server
            RMIarrayList h = (RMIarrayList) Naming.lookup("rmi://"+host+"/RMIarrayList");
            // create ArrayList and add some elements
            ArrayList<String>  v = new ArrayList<String>();
            v.add( "able" );
            v.add( "baker" );
            v.add( "charlie" );
            v.add( "delta" );      
            System.out.println("RMIarrayListClient sending: " +v);
            // send array list to server and print array list received back
            ArrayList a = h.passArrayList(v);
            System.out.println("RMIarrayListClient  received: " +a);
          }
          catch (Exception e)
          {
            System.out.println("Exception in main " + e);
          }
        }
      }
      instructions on running the RMI programs
      Code:
      Distributed Systems RMI
      First the simple RMIarrayList. 
          This consists of 3 files RMIarrayList.java , RMIarrayListClient.java   and RMIarrayListImpl.java . 
          RMIarrayList.java is the interface file, RMIarrayListImpl.java is the server. 
          Compile these files (i.e. javac file.java) 
          Then create the stub files with :- 
              rmic RMIarrayListImpl 
          If all has gone well you should have 2 stub files 
              RMIarrayListImpl_Stub.class 
              RMIarrayListImpl_Skel.class 
          You are now ready to run the application 
      First start the registry - 
              rmiregistry 
      In another window start the server with  java RMIarrayListImpl 
      It  should print the message 
      RMI ArrayList server ready
      
      In a third window start the client with java RMIarrayListClient 
      It should print 
      RMIarrayListClient sending: [able, baker, charlie, delta]
      RMIarrayListClient  received: [able, baker, charlie, delta, goodbye] 
      and the server should print
      RMI ArrayList server received array list [able, baker, charlie, delta] 
      Stop the registry and the server. 
      There is also a useful tutorial on the official site for Getting Started with RMI

      Comment

      Working...