First, RMI is integrated in Spring:
Http://blog.csdn.net/partner4java/article/details/7285055
Then, we will write a helloworld separately, which is divided into several steps.
1. Interface Definition. You need to implement remote
2. Implementation
3. Registration enabled
4. Call
For more information, see the JDK documentation.
Package COM. partner4java. demo. RMI; import Java. RMI. remote; import Java. RMI. remoteException;/*** first define an interface <br/> * implement remote ** @ author partner4java **/public interface Hello extends remote {Public String sayhello (string name) throws RemoteException;} package COM. partner4java. demo. RMI; import Java. RMI. alreadyboundexception; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; import J Ava. RMI. registry. registry; import Java. RMI. server. unicastremoteobject;/*** external services <br/> * There are three main steps: createregistry, exportobject, bind ** @ author partner4java **/public class helloservice implements Hello {Public String sayhello (string name) throws RemoteException {system. out. println ("hello" + name); Return "hello" + name;} public void start () throws RemoteException, alreadyboundexception {// use the provided specific port Guide To receive incoming calls. Hello = (Hello) unicastremoteobject. exportobject (this, 9001); // return the local host's reference to the remote object registry on the specified port. Registry registry = locateregistry. createregiregistry (9000); registry. BIND ("hello", hello);} public static void main (string [] ARGs) throws RemoteException, alreadyboundexception {New helloservice (). start () ;}} package COM. partner4java. demo. RMI; import Java. RMI. notboundexception; import Java. RMI. remoteException; import Java. RMI. registry. locateregistry; import Java. RMI. registry. registry;/*** call ** @ author partner4java **/public class helloclient {public static void main (string [] ARGs) throws RemoteException, notboundexception {registry = locateregistry. getregistry ("127.0.0.1", 9000); Hello = (Hello) registry. lookup ("hello"); system. out. println (hello. sayhello ("world "));}}