Few things generate so much controversy as the release() method of the ServiceManager. Let's take a look at a few of the common comments made (we see these periodically):

try{}finally{release} is a PITA

Yes it is. It is the price you pay for having a rich set of component lifestyles available to you automagically - we need to deal with all components as if they have a rich lifestyle, since the lifestyle is not exposed to the client (which is a Good Thing).

Note there's several things you can do to make this stuff easier. Consider adopting a code convention where you have private members with a ms_ (or something, for "My Service") prefix, then an abstract base class which implements dispose() and will release all those private members, then call super.dispose() as the last method inside your child class dispose() (if any):

{{{ public abstract class AbstractServiceDisposer // UNTESTED CODE SKETCH

{

} }}}

alternative approaches include mixins/AOP/etc. Before you do any of this, words of caution:

GC magic with finalize() can work

GC magic is not part of the avalon-framework contract. Why? It can be somewhat buggy on obscure JDKs, it's expensive, it's complex, its misusing intended use of finalize().

To be on the safe and portable side, don't rely on it being available, nor on it to always work perfectly.

Developers are dumb/lazy

What you can do is create a framework on top of avalon that makes more assumptions about developer laziness that uses mechanisms such as the above. You'll want to coerce your dumb developer friends to all use your special class though.

No-one uses it

Errr...wrong. ServiceManager.release() is used in applications all over the globe. A notable one is cocoon.

what about Releaseable, ReleaseRequired, ReleaseUtil?

Been there, done that, won't work. Marking components as releasable will just mean you'll be checking whether they're marked as such, which is just as ugly.

what if we add a method to the service interface?

A SAXTransformer can't be used after its endDocument() method has been called, so let's add one method to all services that you can call, and which indicates that the component can be reclaimed / put in a pool / disposed.

Comment 1: Adding a release() method to the Component interface would be a double whammy - not only do we bring back the most hated interface in Avalon from its deprecation, we bring it back in tandem with the most hated method. Nice.

Comment 2: The code doesn't get that much better. Instead of:

{{{ MyService ms = null; try {

} finally {

} }}}

you get:

{{{ MyService ms = null; try {

} finally {

} }}}

which is a step sorta sideways-backward, with an extra null check being required. (Having that null check encapsulated in a ReleaseUtil.release(Object o) is not a solution, see above.)

It boils down to...

There's a simple choice:

either

or

ServiceManagerRelease (last edited 2009-09-20 23:16:45 by localhost)