dancing around RMI |
Sep 22 |
How to create a simple client-server application using RMI interface and run it on linux?
- We need to create an interface that extends java.rmi.Remote interface:
public interface IFacade extends Remote { String sayHello() throws RemoteException; // its important exception - its required for object exporting } - Then we are implementing this interface. In
mainmethod we need to get|create a registry object, export our object marked as Remote, and bind it to some name (and actually start the server). Here we can specify the port number for binding.public class Facade implements IFacade { public String sayHello() { return "hello"; } public static void main(String[] args) { int port = 7001; String name = "Facade"; try { IFacade facade = new Facade(); Registry registry = LocateRegistry.createRegistry(port); IFacade stub = (IFacade) UnicastRemoteObject.exportObject(facade, port); registry.bind(name, stub); System.out.println("Server is up and running"); BufferedReader rdr = new BufferedReader( new InputStreamReader(System.in)); while (true) { System.out.println("Type EXIT to shutdown the server."); if ("EXIT".equals(rdr.readLine())) { break; } } registry.unbind(name); UnicastRemoteObject.unexportObject(facade, true); } catch (Exception e) { e.printStackTrace(); } } } - We need to start rmiregistry service:
traut@traut-laptop:~$ rmiregistry Bad port number - using defaultdefault port number is OK
- About client:
public class Client extends Thread { @Override public void run() { super.run(); System.out.println("Client started"); try { Registry registry = LocateRegistry.getRegistry(host, port); IFacade facade = (IFacade) registry.lookup("Facade"); System.out.println(facade.sayHello()); } catch (Exception e) { e.printStackTrace(); return; } } public static void main(String[] args) { int port = 7001; String host = "traut-laptop"; new Client(host, port).start(); new Client(host, port).start(); } } - Fin
A few Interesting things:
This release adds support for the dynamic generation of stub classes at runtime, obviating the need to use the Java(tm) Remote Method Invocation (Java RMI) stub compiler, rmic, to pregenerate stub classes for remote objects. Note that rmic must still be used to pregenerate stub classes for remote objects that need to support clients running on earlier versions.
So, we don’t need to create stub files with rmic program. It must be processed silently undercover from 1.5
I’m not created and used here SecurityManager, policy files, and other boring stuff that documentation advice to use
RMI is still easy-to-understand interface but horrible to implement and to use
