Re: Reference Tracking and Leases.

Peter Jones <[email protected]> Tue, 4 Dec 2007 01:52:02 -0500
Newsgroups gmane.comp.java.sun.jini
Message-ID <20071204065201.GB6312@east>
On Wed, Nov 28, 2007 at 09:01:39AM -0600, Gregg Wonderly wrote:
> One of the common patterns that I have used, is per-client exported objects 
> which are referenced by smart proxies that also hold leases which need to 
> be renewed by the client.  When the lease is cancelled or expired, the 
> service needs to unexport the remote object (DGC could do this part but I 
> don't use it) and do other associated cleanup.  I have a utility class that 
> I use for managing  this activity on the client in a ServiceUI environment 
> where the client JVM may run for a long time, and thus the 
> LeaseRenewalManager may not actually stop renewing the Lease depending on 
> which instance is being used, and how it's life cycle can be managed.
>
> This class uses a ReferenceQueue and PhantomReference objects to track 
> object references to the smart proxy with association to another object 
> which is typically the Lease.  I know that the PreferredClassProvider has 
> some similar behavior, for dropping cached classloaders which are no longer 
> referenced.
>
> If there is interest in this class, I can post it here, or I can just put 
> it into the http://startnow.dev.java.net project for everyones access.  A 
> typical use would be something like.
>
> public class FooSmartProxy implements Foo {
> 	private final Lease lease;
> 	private final Foo foo;
>
> 	// Create a static instance that will hang around as long as
> 	// the classloader is active.
> 	private static FooReferenceTracker trker =
> 		new ReferenceTracker<Foo,Lease>() {
> 		public void released( Lease l ) {
> 			try {
> 				l.cancel();
> 			} catch( Exception ex ) {
> 				log.log( Level.WARNING, ex.toString(), ex );
> 			}
> 		}
> 	}
>
> 	public FooSmartProxy( Foo foo, Lease l ) {
> 		this.foo = foo;
> 		this.lease = l;
> 	}
>
> 	private void readObject( ObjectInputStream is )
> 			throws IOException, ClassNotFoundException {
> 		is.defaultReadObject();
> 		trker.trackReference( this, lease );
> 	}
> 	.... rest of smart proxy definition ....
> }
>
> It manages a single thread for ReferenceQueue.remove() calls that comes and 
> goes as references are actively being tracked.  So, techically, it shuts 
> everything down automatically.
>
> It is designed with the assumption that the second argument to 
> trackReference() will never have a reference to the first argument so that 
> the PhantomReference will actually work.
>
> Thoughts or Comments welcome.

FYI, this seems very similar to how the client side of DGC works under
the covers.  For DGC, instead of a smart proxy, the tracked object is
a so-called "live remote reference" object inside a remote proxy--
BasicObjectEndpoint for JERI (or sun.rmi.transport.LiveRef for JRMP);
the analogue of the Lease is the object ID and transport-layer
endpoint (which are contained in the live reference) and a specialized
lease renewal/cancellation protocol.  For JERI, the implementation is
in com.sun.jini.jeri.internal.runtime.AbstractDgcClient (and the spec
is in BasicObjectEndpoint).

-- Peter

--------------------------------------------------------------------------
Getting Started:     http://www.jini.org/wiki/Category:Getting_Started
Community Web Site:  http://jini.org
jini-users Archive:  http://archives.java.sun.com/archives/jini-users.html
Unsubscribing:       email "signoff JINI-USERS"  to [email protected]