Java remote method invocation

1,022 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,022
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java remote method invocation

  1. 1. ByOm Nishant 080911216
  2. 2. Java remote method invocation The Java Remote Method Invocation Application Programming Interface (API), or Java RMI, is a Java application programming interface that performs the object-oriented equivalent of remote procedure calls (RPC). Typically comprise 2 separate programs:-  Client  Server A remote interface specifies the methods that can be invoked remotely by a client
  3. 3. RMI Server Interfaceimport java.rmi.Remote;import java.rmi.RemoteException;/*interface must extend Remote to enable access fromanother JVMThe methods being remote methods can throw RemoteException */public interface RmiServerIntf extends Remote { public String getMessage() throws RemoteException;/*This is the method implemented by server andaccessed by client*/}
  4. 4. Implementing the interface An RMI server program needs to create the initial remote objects and export them to the RMI runtime The setup procedure should do the following:  Create and install a security manager  Create and export one or more remote objects  Register at least one remote object with the RMI registry (or with another naming service
  5. 5. RMI Server Programimport java.rmi.Naming;import java.rmi.registry.*;import java.rmi.RemoteException;import java.rmi.RMISecurityManager;import java.rmi.server.UnicastRemoteObject;//public class classname implements remote-interfacepublic class RmiServer extends UnicastRemoteObject implements RmiServerIntf { public static final String MESSAGE = "Hello world"; public RmiServer() throws RemoteException { //constructor defn } public String getMessage() {//Todo code for the remote method(s) return MESSAGE; }
  6. 6. public static void main(String args[]) {//main method is used to create an instance and make it available to clients.//Write the todo code here 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."); }
  7. 7. try {//special exception handler for registry creation//Registry used for naming services 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."); }
  8. 8. try { //Instantiate RmiServer //(ClassNAme)UnicastRemoteObject.exportObject(object1, portno); 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(); }}}
  9. 9. 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); return e.getMessage(); } }
  10. 10. 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()); }}
  11. 11. Implementation: Compile the source code files  javac filename Create the stub for client and skeleton for server  rmic serverfilename Start the server  rmiregistry Ensure security permits are in order Run the server and client files
  12. 12. Common sources of errors Before running the server, make sure you have an instance of rmiregistry running on the server Make sure that your CLASSPATH variable is empty Not having a suitable security policy Mixing Java versions

×