- When you use Unicastremoteobject.export (Remote) to publish an object, a full GC is triggered.
- You must manually manage the remote objects you publish, meaning that if there is no other client access, the objects/services you publish will be recycled and you will receive the following error
"NO SUCH OBJECT in TABLE."
Reference:
Packagetest;Import StaticJava.util.concurrent.TimeUnit.MILLISECONDS;Import StaticJava.util.concurrent.TimeUnit.SECONDS;ImportJava.rmi.Remote;Importjava.rmi.RemoteException;ImportJava.rmi.registry.LocateRegistry;ImportJava.rmi.registry.Registry;ImportJava.rmi.server.UnicastRemoteObject;InterfaceRemoteoperationsextendsRemote {String remoteoperation ()throwsremoteexception;} Public Final classRmiImplementsremoteoperations {Private Static FinalString remote_name = remoteoperations.class. GetName (); Private Static FinalRemoteoperations classvariable =NewRMI (); Private Static BooleanHoldstrongreference =false; Private Static BooleanInvokegarbagecollector =true; Private Static intDelay = 0; Public Static voidMainFinalString ... args)throwsException { for(FinalString Arg:args) { if("-GC". Equals (ARG)) {Invokegarbagecollector=true; } Else if("-NOGC". Equals (ARG)) {Invokegarbagecollector=false; } Else if("-hold". Equals (ARG)) {holdstrongreference=true; } Else if("-release". Equals (ARG)) {holdstrongreference=false; } Else if(Arg.startswith ("-delay") ) {delay= Integer.parseint (arg.substring ("-delay". Length ())); } Else{System.err.println ("Usage:javac Rmitest.java && java rmitest [-GC] [-NOGC] [-hold] [-release] [-delay<seconds>]"); System.exit (1); }} server (); if(invokegarbagecollector) {System.GC (); } if(Delay > 0) {System.out.println ("Delaying" + delay + "seconds"); Final Longmilliseconds =Milliseconds.convert (delay, SECONDS); Thread.Sleep (milliseconds); } client (); System.exit (0);//Stop RMI Server thread} @Override PublicString remoteoperation () {return"Foo"; } Private Static voidServer ()throwsException {//This reference are eligible for GC after this method returns FinalRemoteoperations methodvariable =NewRMI (); FinalRemoteoperations tobestubbed = holdstrongreference?classvariable:methodvariable; FinalRemote Remote = Unicastremoteobject.exportobject (tobestubbed, 0); FinalRegistry Registry =locateregistry.createregistry (Registry.registry_port); Registry.bind (Remote_name, REMOTE); } Private Static voidClient ()throwsException {FinalRegistry Registry =Locateregistry.getregistry (); FinalRemote Remote =Registry.lookup (remote_name); FinalRemoteoperations stub = Remoteoperations.class. Cast (remote); FinalString message =stub.remoteoperation (); System.out.println ("Received:" +message); }}
Http://stackoverflow.com/questions/645208/java-rmi-nosuchobjectexception-no-such-object-in-table
More interesting points about RMI services