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. RMI Server Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
/*interface must extend Remote to enable access from
another JVM
The methods being remote methods can throw Remote
Exception */
public interface RmiServerIntf extends Remote {
public String getMessage() throws RemoteException;
/*This is the method implemented by server and
accessed by client*/
}
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. RMI Server Program
import 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-interface
public 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. 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. 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. 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. 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. 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. 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. 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