简体   繁体   中英

Getting java.rmi.NoSuchObjectException while invoking a method on remote object

I know such kind of questions have been asked before.But my problem is little different. I have 2 services , A and BA uses methods of B via RMI. B exports itself via below 3 statments:-

Remote stub = UnicastRemoteObject.exportObject( rs, port,socketFactory,socketFactory);
Registry registry = LocateRegistry.createRegistry( port,socketFactory,socketFactory  );
registry.rebind( serviceBindName, stub );  

where rs is the remote object implementation.

Now A looks up B via below 2 statements :-

Registry registry = LocateRegistry.getRegistry( BsIP, BsRMIPort);
RemoteServiceRegistry rs = ( RemoteServiceRegistry ) registry.lookup( serviceBindName);

Now everything works smoothly.A obtains stub from B via lookup and caches it for all subsequent requests. But problems starts when i restart BI have mechanism via which whenever B restarts i clear the cache and perform a fresh lookup of B in A.But somehow its not working and throws exceptions in both A and B.

I am clueless about this . Please help.Do let me know if you need more information.

Exception in A

java.rmi.NoSuchObjectException: no such object in table
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
        at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
        at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)

Exception in B ( after adding a property to JVM ,-Djava.rmi.server.logCalls=true)

FINE: RMI TCP Connection(10)-192.168.50.243: [192.168.50.243] exception:
java.rmi.NoSuchObjectException: no such object in table
        at sun.rmi.transport.Transport.serviceCall(Transport.java:135)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)

This exception means that the remote stub is 'stale', ie refers to an object that is no longer exported from the JVM the stub came from. When you get this exception you must re-acquire the stub via the same means you got it in the first place:

  • if via a lookup() , redo the lookup.
  • if via a remote method call of your own, redo it, and note that that in turn can fail with the same exception, and so on recursively.

.I have mechanism via which whenever B restarts i clear the cache and perform a fresh lookup of B in A

Clearly that mechanism isn't working or isn't being invoked.

NB You should always store the result of LocateRegistry.createRegistry() in a static variable, to prevent the Registry from being garbage-collected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM