/** * 获取RMI接口状态 * * @return "0":服务正常,"1": 连接报错,"2":连接超时 */ @Override public String getRMIJkzt() { final ExecutorService es = Executors.newFixedThreadPool(1); Callable<String> callable = new Callable<String>() {//使用Callable接口作为构造参数 @Override public String call() throws Exception {//真正的任务在这里执行,这里的返回值类型为String,可以为任意类型 URL url = new URL("rmi://192.168.20.188:1098/ht"); InputStream in = url.openStream(); in.close(); return "0"; } }; try { Future future = es.submit(callable); String value = null; try { value = future.get(5000, TimeUnit.MILLISECONDS).toString();//取得结果,同时设置超时执行时间为5秒。同样可以用future.get(),不设置执行超时时间取得结果 return "0"; } catch (ExecutionException e) { return "1"; } catch (TimeoutException e) { return "2"; } } catch (InterruptedException e) { return "1"; } }
java测试网络连接是否成功并设置超时时间,布布扣,bubuko.com
原文地址:http://blog.csdn.net/w_basketboy24/article/details/25403111