Studyon Minte9.com
Head First (Java)




RMI



Method calls are always between two objects of the same heap. What if you want to invoke a method on an 
object running on another machine? You can't do it, not directly! You can pretend that you're invoking a 
method on a local object. That what RMI is all about.

In Java, RMI builds the client and service helper objects for you, and it even knows how to make the client 
helper look like th e Real Service. 

Plus, RMI provides all the runtime infrastructure to make it work, including a lookup service so that the client 
can find and get the client helper (the proxy for the Real Service).

With RMI. you don't write any of the networking or I/O code yourself.


A) Server Side:

	1) Remote Interface	

		%java
			// MyRemote.java

			package rmi;
			import java.rmi.*;

			public interface MyRemote extends Remote {

			    public String helloWorld() throws RemoteException;
			}

			

	2) Remote Service (implemenation)

		%java
			// MyRemoteImpl.java

			package rmi;
			import java.rmi.*;
			import java.rmi.server.*;

			public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
			    
			    public MyRemoteImpl() throws RemoteException {}
			    
			    @Override
			    public String helloWorld() {
				return "Server says: Hello World";
			    }
			    
			    public static void main(String[] args) {
				try {
				    MyRemote service = new MyRemoteImpl();
				    Naming.rebind("RemoteHello", service);
					// bind the remote object to rmiregistry
					// The name is the one that clients will need to look up in the rmi registry
				} catch (Exception e) {
				    e.printStackTrace();
				}
			    }
			}

			
	3) Generate stubs and skeletons

		Run rmic

			%
			rmic -classpath d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\  
                               -d d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\rmi\ rmi.MyRemoteImpl

		The rmic tool takes a service implementation and creates a new class:
			
			/build/classes/rmi/rmi/MyRemoteImpl_Stub.class

	4) Start rmiregistry

		Be sure you start it from a directory that has access to your classes.
			
			cd d:\htdocs\java\NetBeansProjects\RMIserver\build\classes\

			%rmiregistry

	5) Start the service (command or IDE)

		%java MyRemoteImpl



B) Client Side

	%java
		// RMIclient.java

		package rmi;
		import java.rmi.*;

		public class RMIclient {
		    public static void main (String[] args) {
			new RMIclient().go();
		    }
		    public void go() {
			try {
			    MyRemote service = (MyRemote) Naming.lookup("rmi://192.168.1.100/RemoteHello");
			    System.out.println(service.helloWorld());
			} catch(Exception e) {
			    e.printStackTrace();
			}
		    }
		}

	%java
		// MyRemote.java

		package rmi;
		import java.rmi.*;

		public interface MyRemote extends Remote {
		    public String helloWorld() throws RemoteException;
		}

	Also, you need to supply the client with _stub file:

	/build/classes/rmi/rmi/MyRemoteImpl_Stub.class


!! It works localy, but not remote :(


http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/rmic.html