Reference Tracking and Leases.
Gregg Wonderly <[email protected]> Wed, 28 Nov 2007 09:01:39 -0600
| Newsgroups | gmane.comp.java.sun.jini |
|---|---|
| Message-ID | <[email protected]> |
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.
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]