You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Bob Brown <bo...@transentia.com.au> on 2007/08/12 13:41:29 UTC

How to reclaim/pool returned object instances?

Hi list!

I seek advice from those wiser than I...

I am writing a service that exposes a method looking a little like the
following:

List<Stuff> doManyThings(int howMany)
  {
  List<Stuff> l = new ArrayList<Stuff>(howMany);
  
  for (int i = 0; i < howMany; i ++)
    l.add(new Stuff(i));

  return l;
  }

Since the value for 'howMany' may work out to be very large, I am worried
about performance.

(I am leaving aside the overheads implicit with XML at the moment...may bite
me later, of course)

I can create a pool of Stuff instances (using commons pool for example) and
retrieve from the pool, rather than creating myriad new Stuff instances all
the time...this should be beneficial.

The question is: since the list of Stuff instances is returned to the
'container' and never seen by my code again, how/when do I release instances
back into the pool?

Can I write an interceptor for this? How? My gut feel is that this is/should
be a faq but I can't find anything!

Is there a more effective approach than what I am taking? Perhaps I should
create the XML directly, rather than rely on CXF's marshalling...but I'd
rather not be forced to do this in a way that changes the existing method
signature (by having to return an org.w3c.dom.Document, say)

Cheers,

BOB


Re: How to reclaim/pool returned object instances?

Posted by Daniel Kulp <dk...@apache.org>.
Bob,

An interceptor would definitely work for you.   You would basically 
register an OutInterceptor (and OutFaultInterceptor in case of an 
exception or something) that would live in just about any phase that 
occurs after the MARSHAL phase.   Most likely the POST_MARSHAL or 
MARSHAL_ENDING would be the most appropriate.

The interceptor would look something like:
public void handleMessage(Message message) throws Fault {
    List<?> params = message.getContent(List.class);
    List<Stuff> stuff = (List)params.get(0);
    for (Stuff s : stuff) {
       // reclaim s
    }
    //null out the list to be sure
    params.set(0, null);
}


Dan



On Sunday 12 August 2007, Bob Brown wrote:
> Hi list!
>
> I seek advice from those wiser than I...
>
> I am writing a service that exposes a method looking a little like the
> following:
>
> List<Stuff> doManyThings(int howMany)
>   {
>   List<Stuff> l = new ArrayList<Stuff>(howMany);
>
>   for (int i = 0; i < howMany; i ++)
>     l.add(new Stuff(i));
>
>   return l;
>   }
>
> Since the value for 'howMany' may work out to be very large, I am
> worried about performance.
>
> (I am leaving aside the overheads implicit with XML at the
> moment...may bite me later, of course)
>
> I can create a pool of Stuff instances (using commons pool for
> example) and retrieve from the pool, rather than creating myriad new
> Stuff instances all the time...this should be beneficial.
>
> The question is: since the list of Stuff instances is returned to the
> 'container' and never seen by my code again, how/when do I release
> instances back into the pool?
>
> Can I write an interceptor for this? How? My gut feel is that this
> is/should be a faq but I can't find anything!
>
> Is there a more effective approach than what I am taking? Perhaps I
> should create the XML directly, rather than rely on CXF's
> marshalling...but I'd rather not be forced to do this in a way that
> changes the existing method signature (by having to return an
> org.w3c.dom.Document, say)
>
> Cheers,
>
> BOB



-- 
J. Daniel Kulp
Principal Engineer
IONA
P: 781-902-8727    C: 508-380-7194
daniel.kulp@iona.com
http://www.dankulp.com/blog