Created
December 8, 2013 08:26
-
-
Save ubinix-warun/7854623 to your computer and use it in GitHub Desktop.
Java RMI Example, Reference=http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Java_remote_method_invocation.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grant { | |
permission java.net.SocketPermission "127.0.0.1:*", "connect,resolve"; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.Naming; | |
import java.rmi.RemoteException; | |
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[]) { | |
// Create and install a security manager | |
if (System.getSecurityManager() == null) { | |
System.setSecurityManager(new RMISecurityManager()); | |
} | |
RmiClient cli = new RmiClient(); | |
System.out.println(cli.getMessage()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.Naming; | |
import java.rmi.RemoteException; | |
import java.rmi.RMISecurityManager; | |
import java.rmi.server.UnicastRemoteObject; | |
import java.rmi.registry.*; | |
public class RmiServer extends UnicastRemoteObject | |
implements RmiServerIntf { | |
public static final String MESSAGE = "Hello world"; | |
public RmiServer() throws RemoteException { | |
} | |
public String getMessage() { | |
return MESSAGE; | |
} | |
public static void main(String args[]) { | |
System.out.println("RMI server started"); | |
// Create and install a security manager | |
if (System.getSecurityManager() == null) { | |
System.setSecurityManager(new RMISecurityManager()); | |
System.out.println("Security manager installed."); | |
} else { | |
System.out.println("Security manager already exists."); | |
} | |
try { //special exception handler for registry creation | |
LocateRegistry.createRegistry(1099); | |
System.out.println("java RMI registry created."); | |
} catch (RemoteException e) { | |
//do nothing, error means registry already exists | |
System.out.println("java RMI registry already exists."); | |
} | |
try { | |
//Instantiate 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"); | |
} catch (Exception e) { | |
System.err.println("RMI server exception:" + e); | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.rmi.Remote; | |
import java.rmi.RemoteException; | |
public interface RmiServerIntf extends Remote { | |
public String getMessage() throws RemoteException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac RmiServer.java | |
---> RmiServer.class | |
---> RmiServerIntf.class | |
rmic RmiServer | |
---> RmiServer_Stub.class | |
javac RmiClient.java | |
---> RmiClient.class | |
java -Djava.security.policy=server.policy RmiServer | |
java -Djava.security.policy=no.policy RmiClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grant { | |
permission java.net.SocketPermission "127.0.0.1:*", "connect,resolve"; | |
permission java.net.SocketPermission "127.0.0.1:*", "accept"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment