问题描述
同一RMI服务端export多个远程服务实现时,在客户端调用时有的能访问,有的不能访问,出现异常:NoSuchObjectException: no such object in table
问题分析
在远程服务器export服务实现时是通过匿名对象的方式:
SmsSenderImplsmsSender = new SmsSenderImpl();
iSmsSender =(ISmsSender) UnicastRemoteObject.exportObject(smsSender, 0);
registry.bind("SmsSender",iSmsSender);
在匿名对象创建后,没有其他的引用,方法返回后,可能会被System GC(垃圾回收),所以出现可能该对象不存在的现象。
解决方法
将服务实现对象设为static静态变量,我的相关代码如下:public static SmsSenderImpl smsSender = new SmsSenderImpl();//用static,避免NoSuchObjectException public static ISmsSender iSmsSender = null; public static void startSmsServer() { try { iSmsSender = (ISmsSender) UnicastRemoteObject.exportObject(smsSender, 0); Registry registry = null; int port = 9999; try{ registry = LocateRegistry.getRegistry(port); registry.list(); }catch(Exception ex){ registry = LocateRegistry.createRegistry(port); } registry.bind("SmsSender", iSmsSender); logger.info("启动远程短信调用接口..."); } catch (Exception e) { logger.error("启动远程短信调用接口异常!", e); } }
RMI no such object in table异常,布布扣,bubuko.com
原文地址:http://blog.csdn.net/jakey766/article/details/26244851