My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎

RMI

Java has in-build RPC called RMI.


First about simple implementation:

Interface:

package rmi;

import java.io.Serializable;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Map;

public interface Hello extends Remote, Serializable{
    Map<String, Object> sayHello(Serializable lol) throws RemoteException;
}

Server:

package rmi;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
        
public class Server implements Hello {
	private static final long serialVersionUID = 1L;

	public Server() {}
    
    @Override
    public Map<String, Object> sayHello(Serializable lol) throws RemoteException{
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("lol", lol.toString());
		return hashMap;
    }
        
    public static void main(String args[]) throws Throwable {
    	Registry registry = LocateRegistry.createRegistry(2020);
    	Server obj = new Server();
    	Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    	registry.bind("Hello", stub);
    	System.err.println("Server ready");
    }
}


Client:

package rmi;

import java.io.Serializable;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Map;

public class Client {

    private Client() {}

    public static void main(String[] args) {

        try {
        	// Same
            // Hello stub =  (Hello) Naming.lookup ("//127.0.0.1:2020/Hello");
        	
    		Registry registry = LocateRegistry.getRegistry("127.0.0.1", 2020);
    		Hello stub = (Hello) registry.lookup("Hello");
        	
            Map<String, Object> response = stub.sayHello(new Serializable() {
				private static final long serialVersionUID = 1L;

				@Override
            	public String toString() {
            		return "dymamical";
            	}
			});
            System.out.println("response: " + response);
        } catch (Exception e) {
            System.err.println("Client exception: " + e.toString());
            e.printStackTrace();
        }
    }
}



And notes:

For running server we need to specify SecurityMeneger. There are many way to do this(copy). 
I do in simplest way - I just add program arguments to java runtime: 
-Djava.security.policy=java.security.AllPermission
Self-written security meneger can control client origin (ip address, etc.)

RMI can not aquire reference from remote server/client. All objects that are not implement Remote interface are serializing/deserialazing(In case if we work with interface or with child type - code of concrette type will be send too - this is some kind of remote classloading).

Another thing: RMI server can start as separate executable sa well as it current process:
http://stackoverflow.com/questions/8337215/
http://stackoverflow.com/questions/1823305
And there are some difference with work in different modes.

Now we know how to start server. But how to stop it?
There many answers here:
https://www.google.com.ua/search?q=close+RMI+server
http://stackoverflow.com/questions
http://stackoverflow.com/questions/8386001/
But it is to complicated;

Also there is still open question about https://www.google.com.ua/search?q=rmi+performance

RMI can work over HTTP: https://www.google.com.ua/search?q=rmi+via+http (not checked yet)


Tutorial:

http://docs.oracle.com/javase/tutorial/rmi/overview.html





Comments