@SuppressWarnings({"unchecked", "boxing", "rawtypes"}) private DiscoveryManager getRemoteProxyToDiscoveryManager(final String ip, final int port) { final String address = ip + ":" + port; final int timeout = 500; // 500 ms RemoteAPIImpl if need detailed version... this.logger.status("remoteproxytodiscovery/start", "ip", ip, "port", port); final DiscoveryManager newClient = Proxies.newClient( EXPORT_NAME, address, getClass().getClassLoader(), DiscoveryManager.class); // Execute collection asynchronously (TODO: cache pool usage could be improved) final ExecutorService cachePool = Executors.newCachedThreadPool(RemoteDiscoveryImpl.threadFactory); final ExecutorCompletionService<String> ecs = new ExecutorCompletionService(cachePool); final Future<String> future = ecs.submit( new Callable<String>() { public String call() throws Exception { return AccessController.doPrivileged( new PrivilegedAction<String>() { public String run() { return newClient.ping(667) == 667 ? "OK" : null; } }); } }); // Wait at most half a second (TODO: Make this configurable) try { final String string = future.get(timeout, TimeUnit.MILLISECONDS); if (string == null) return null; return newClient; } catch (final InterruptedException e) { this.logger.status("remoteproxytodiscovery/exception/interrupted", "message", e.getMessage()); e.printStackTrace(); } catch (final ExecutionException e) { this.logger.status( "remoteproxytodiscovery/exception/executionexception", "message", e.getMessage()); } catch (final TimeoutException e) { this.logger.status( "remoteproxytodiscovery/exception/timeoutexception", "message", e.getMessage()); } catch (final SecurityException e) { this.logger.status( "remoteproxytodiscovery/exception/securityexception", "message", e.getMessage()); e.printStackTrace(); } finally { AccessController.doPrivileged( new PrivilegedAction<Object>() { public Object run() { future.cancel(true); cachePool.shutdownNow(); return null; } }); this.logger.status("remoteproxytodiscovery/end"); } this.logger.status("remoteproxytodiscovery/end"); return null; }