Java remote method invocation: Difference between revisions

Content deleted Content added
Example: Further removal of security manager stuff, not required in a simple example.
Example: Further simplification of example code
Line 26:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
Line 35 ⟶ 34:
 
public RmiServer() throws RemoteException {
super(0); // required to avoid the 'mic' step, see below
}
 
Line 42 ⟶ 41:
}
 
public static void main(String args[]) throws Exception {
System.out.println("RMI server started");
 
Line 52 ⟶ 51:
System.out.println("java RMI registry already exists.");
}
try//Instantiate {RmiServer
RmiServer obj = //Instantiatenew RmiServer();
RmiServer obj = new RmiServer();
 
// Bind this object instance to the name "RmiServer"
Naming.rebind("//localhost/RmiServer", obj);
System.out.println("PeerServer bound in registry");
 
System.out.println("PeerServer bound in registry");
} catch (Exception e) {
System.err.println("RMI server exception:" + e);
e.printStackTrace();
}
}
}
Line 84 ⟶ 77:
<source lang=java>
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
 
public class RmiClient {
// "obj" is the reference of the remote object
RmiServerIntf obj = null;
 
public String getMessage() {
try {
obj = (RmiServerIntf)Naming.lookup("//localhost/RmiServer");
return obj.getMessage();
} catch (Exception e) {
System.err.println("RmiClient exception: " + e);
e.printStackTrace();
return e.getMessage();
}
}
 
public static void main(String args[]) throws Exception {
RmiServerIntf obj = (RmiServerIntf)Naming.lookup("//localhost/RmiServer");
RmiClientreturn cli = new RmiClientobj.getMessage();
 
System.out.println(cli.getMessage());
}
}
</source>