Type the server bootstrap: strong reference, export, registry, rebind
Type the server bootstrap: strong reference, export, registry, rebind
Answer
private static PrimeServiceImpl impl; // strong reference outlives main public static void main(String[] args) throws Exception { impl = new PrimeServiceImpl(); PrimeService stub = (PrimeService) UnicastRemoteObject.exportObject(impl, 0); Registry registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); registry.rebind(PrimeService.NAME, stub); System.out.println("bound as " + PrimeService.NAME); }
The static field is what stops distributed GC from collecting the implementation once the last client lease lapses. main can then return — RMI keeps non-daemon listener threads alive while the object stays exported.
JDK javadoc java.rmi.registry.LocateRegistry; java.rmi.server.UnicastRemoteObject