Re: Reference Tracking and Leases.

Gregg Wonderly <[email protected]> Wed, 5 Dec 2007 14:16:54 -0600
Newsgroups gmane.comp.java.sun.jini
Message-ID <[email protected]>
Peter Jones wrote:
> 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).

Right, that's what I've seen.  The work that I did was to just expose that 
leasing at the application level so that I can take specific actions at the 
point that such action is necessary.  For example, I have a smart proxy who's 
server side endpoint streams data over a UDP socket, as well as making normal 
RMI centric calls.  Those RMI calls would get a RemoteException when the DGC 
discovered that the client was gone.  But the UDP pump would not see any 
particular failures.  So, having the lease in place to manage that issue helps 
to alert the servers UDP generation that there is no longer a path so that it 
can throw a RemoteException which will allow the server to stop pumping out UDP 
events.

Gregg Wonderly

--------------------------------------------------------------------------
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]