@SuppressWarnings("UseOfSystemOutOrSystemErr") protected static void start(Remote remote) throws Exception { setupRMI(); banJNDI(); if (ourRemote != null) throw new AssertionError("Already started"); ourRemote = remote; Registry registry; int port; for (Random random = new Random(); ; ) { port = random.nextInt(0xffff); if (port < 4000) continue; try { registry = LocateRegistry.createRegistry(port); break; } catch (ExportException ignored) { } } try { Remote stub = UnicastRemoteObject.exportObject(ourRemote, 0); final String name = remote.getClass().getSimpleName() + Integer.toHexString(stub.hashCode()); registry.bind(name, stub); String id = port + "/" + name; System.out.println("Port/ID: " + id); long waitTime = 2 * 60 * 1000L; Object lock = new Object(); //noinspection InfiniteLoopStatement while (true) { //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (lock) { lock.wait(waitTime); } RemoteDeadHand deadHand = (RemoteDeadHand) registry.lookup(RemoteDeadHand.BINDING_NAME); waitTime = deadHand.ping(id); } } catch (Throwable e) { e.printStackTrace(System.err); System.exit(1); } }