Java has a Remote Method Invocation mechanism.
// RMIExample.java // Interface for the RMI remote object. // Note: Interface must extend from java.rmi.Remote // Methods must throw RemoteExcpetion import java.rmi.*; public interface RMIExample extends Remote { public boolean PostMsg(String strMsg) throws RemoteException; public long Factorial(long lVal) throws RemoteException; }
Note: the class Remote doesn't have any methods so none have to be implemented.
This code implements the remote object.
// RMIExampleImpl.java // Implements the remote object // Note: The object must extend from UnicastRemoteObject // The object must implement the associated interface import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.io.*; public class RMIExampleImpl extends UnicastRemoteObject implements RMIExample { protected static String m_strName; public RMIExampleImpl() throws RemoteException { super(); // call base class constructor } public boolean PostMsg(String strMsg) throws RemoteException { System.out.println("Server: PostMsg() invoked..."); System.out.println("Server: Message > " + strMsg); return true; } public long Factorial(long lVal) throws RemoteException { long lRes = FactorialEx(lVal); System.out.println("Server: Factorial() invoked..."); System.out.println("Server: Factorial("+lVal+") = " + lRes); return lRes; } protected long FactorialEx(long lVal) { if (lVal <= 1) return 1; else return lVal * FactorialEx(lVal-1); } public static void main(String argv[]) { try { m_strName = "TheRMIExample"; System.out.println("Server: Registering RMIExampleImpl as \"" + m_strName +"\""); RMIExampleImpl Example = new RMIExampleImpl(); Naming.rebind(m_strName, Example); System.out.println("Server: Ready..."); } catch (Exception e) { System.out.println("Server: Failed to register RMIExampleImpl: " + e); } } }
// RMIClient.java // // This sample Java RMI client can perform the // following operations: // (1) Send a message to a remote object. This // is done by using the -m command line switch. // Example: java RMIClient -m "My message in quotes" // (2) Calculate the factorial of a given number via // a method of the remote object. // Example: java RMIClient -f 5 import java.rmi.*; import java.rmi.server.*; public class RMIClient { public static void main(String argv[]) { // Validate command line parameters if (argv.length < 2) { System.out.println("Usage: java RMIClient [-m \"MESSAGE\"] [-f INTEGER]"); System.exit(1); } // Command line option flags boolean bMessage = false; boolean bFactorial = false; String strMsg = "No message."; long lVal = 1; // Determine data to be processed for (int i=0; i<argv.length; i++) { if (argv[i].equals("-m")) { bMessage = true; strMsg = argv[++i]; } if (argv[i].equals("-f")) { bFactorial = true; lVal = Long.parseLong(argv[++i]); } } // Install security manager. This is only necessary // if the remote object's client stub does not reside // on the client machine (it resides on the server). System.setSecurityManager(new RMISecurityManager()); // Get a remote reference to the RMIExampleImpl class String strName = "rmi://wpi.wpi.edu/TheRMIExample"; System.out.println("Client: Looking up " + strName + "..."); RMIExample RemRMIExample = null; try { RemRMIExample = (RMIExample)Naming.lookup(strName); } catch (Exception e) { System.out.println("Client: Exception thrown looking up " + strName); System.exit(1); } // Send a messge to the remote object if (bMessage) { try { if (!RemRMIExample.PostMsg(strMsg)) System.out.println("Client: Remote PostMsg() call failed."); } catch (Exception e) { System.out.println("Client: Exception thrown calling PostMsg()."); System.exit(1); } } // Calculate the factorial if (bFactorial) { try { long lRes = RemRMIExample.Factorial(lVal); System.out.println("Client: Factorial(" + lVal + ") = " + lRes); } catch (Exception e) { System.out.println("Client: Excpetion thrown calling Factorial()."); System.exit(1); } } } }
RMIExampleImplStubs: RMIExampleImpl rmic RMIExampleImpl RMIExampleImpl: RMIExample RMIClient javac RMIExampleImpl.java RMIExample: RMIExample.java javac RMIExample.java RMIClient: RMIClient.java javac RMIClient.java clean: rm RMIClient.class RMIExample.class RMIExampleImpl.class RMIExampleImpl_Skel.class RMIExampleImpl_Stub.class
< 1 >ls Makefile RMIExample.java java.policy RMIClient.java RMIExampleImpl.script < 2 >make javac RMIExample.java javac RMIClient.java javac RMIExampleImpl.java rmic RMIExampleImpl < 3 >rmiregistry& [1] 70 < 4 >ls Makefile RMIExampleImpl.class RMIClient.class RMIExampleImpl.java RMIClient.java RMIExampleImpl_Skel.class RMIExample.class RMIExampleImpl_Stub.class RMIExample.java java.policy < 5 >java RMIExampleImpl& [2] 19426 Server: Registering RMIExampleImpl as "TheRMIExample" Server: Ready... < 6 > java RMIClient -m "How is the weather over there?" Client: Looking up rmi://..edu/TheRMIExample... Client: Exception thrown looking up rmi://..edu/TheRMIExample < 8 >java -Djava.security.policy=java.policy RMIClient -m "hello from the client" Client: Looking up rmi://..edu/TheRMIExample... Server: PostMsg() invoked... Server: Message > hello from the client < 9 >java -Djava.security.policy=java.policy RMIClient -f 5 Client: Looking up rmi://..edu/TheRMIExample... Server: Factorial() invoked... Server: Factorial(5) = 120 Client: Factorial(5) = 120 < 10 >jobs [1] + Running rmiregistry [2] - Running java RMIExampleImpl < 11 >kill %1 %2 < 12 > [1] Terminated rmiregistry < 13 > [2] Terminated java RMIExampleImpl
rmiregistry &
(remember
to kill -9 the process when your done using it)
This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -no_navigation -split 0 -t 'Java RMI' week4-javarmi.tex
The translation was initiated by Craig Wills on 2002-11-07