Your object which will be accessible through RMI must meet the following requirements:
- have its own interfaces which extends
java.rmi.Remote
- all methods must “
throws RemoteException
“ - all returned returned objects must implement
java.io.Serializable
Starting a RMI server:
- Set system property:
System.setProperty("java.rmi.server.hostname", "hostname");
- create registry:
Registry registry = LocateRegistry.createRegistry(1099);
- export Object:
RemotableObject stub = (RemotableObject) UnicastRemoteObject.exportObject(remoteObject, 0);
- bind object:
registry.rebind("RemoteName", stub);
Remote call:
- get Registry:
Registry registry = LocateRegistry.getRegistry("hostname");
- get Remote object:
RemotableObject remoteObject = (RemotableObject) registry.lookup("RemoteName");
- call method:
remoteObject.remoteMethod();