Java Remote Method Invocation - Example

Example

The following classes implement a simple client-server program using RMI that displays a message.

RmiServer class — listens to RMI requests and implements the interface which is used by the client to invoke remote methods.

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; } } }

RmiServerIntf interface — defines the interface that is used by the client and implemented by the server.

import java.rmi.Remote; import java.rmi.RemoteException; public interface RmiServerIntf extends Remote { public String getMessage throws RemoteException; }

RmiClient class — this is the client which gets the reference (a proxy) to the remote object living on the server and invokes its method to get a message. If the server object implemented java.io.Serializable instead of java.rmi.Remote, it would be serialized and passed to the client as a value.

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) { // Create and install a security manager if (System.getSecurityManager == null) { System.setSecurityManager(new RMISecurityManager); } RmiClient cli = new RmiClient; System.out.println(cli.getMessage); } }

Before running this example, we need to make a 'stub' file of the interface we used. For this task we have the RMI compiller - 'rmic'

  • Note: we make a stub file from the '*.class' file with the implementation of the remote interface, not from the '*.java' file.*
rmic RmiServer

Note that since version 5.0 of J2SE support for dynamically generated stub files has been added, and rmic is only provided for backwards compatibility with earlier run times.

server.policy — this file is required on the server to allow TCP/IP communication for the remote registry and for the RMI server.

grant { permission java.net.SocketPermission "127.0.0.1:*", "connect,resolve"; permission java.net.SocketPermission "127.0.0.1:*", "accept"; };

The server.policy file should be used using the D switch of Java RTE, e.g.:

java.exe -Djava.security.policy=server.policy RmiServer

client.policy — this file is required on the client to connect to RMI Server using TCP/IP.

grant { permission java.net.SocketPermission "127.0.0.1:*", "connect,resolve"; };

no.policy — also if you have any troubles with connecting, try this file for server or client.

grant { permission java.security.AllPermission; };

Read more about this topic:  Java Remote Method Invocation

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)