java.rmi.NoSuchObjectException: no such object in table

后端 未结 7 2359
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 06:43

I am writing a very simple RMI server, and I am seeing intermittent java.rmi.NoSuchObjectExceptions in the unit tests.

I have a string of remote method

7条回答
  •  星月不相逢
    2020-11-28 07:25

    I have the same problem and now I've solved it. The solution is simple, you MUST create strong reference 'object' to avoid the object being GC'd.

    for example in your server class:

    ...
    private static ServiceImpl serviceImpl = null;
    
    public static void register (int port) {
        serviceImpl = new ServiceImpl();
        Registry registry = LocateRegistry.createRegistry(port);
        registry.rebind ("serviceImpl", serviceImpl);
    }
    
    public static void main(String[] args) throws RemoteException, NotBoundException {
        register(1099);    
        ...the rest of your code...
    }
    

    So, it protects "serviceImpl" object from being GC'd. CMIIW

提交回复
热议问题