users@jersey.java.net

Re: [Jersey] Cleanup

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 05 Nov 2009 09:52:02 +0100

Hi,

A response filter is not really suitable for cleanup because it is not
guaranteed to get called if an exception is thrown from a request
filter (or the resource) and that exception is passed through to the
container. See:

   https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/container/filter/package-summary.html

There are three choices:

1) Register a ServletRequestInitializer:

      http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequestListener.html

      This is obviously specific to servlet so may not be ideal.

2) Use @PreDestroy on a resource class (which does work for all
supported scopes).

3) Use the CloseableService:

      https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/spi/CloseableService.html

      You can inject the CloseableService:

          @Context CloseableService cs;

      and then register instances of Closeable:

          Closeable c = ...
          cs.add(c);

     The Jersey multipart module, specifically the message body reader
for multipart messages uses the
     Closeable service such that temporary files that are cached body
parts are cleaned up.


All of the above are guaranteed to be processed regardless of
Exceptions being thrown.

Paul.


On Nov 4, 2009, at 11:45 PM, dloy wrote:

> Sorry to be slow on this.
>
> I haven't found a working code example on the web. <http://n2.nabble.com/Filters-td2285491.html
> > is about as close as I could find. <http://n2.nabble.com/ContainerResponseFilter-is-not-suitable-for-cleanup-job-td2312274.html
> > is far from encouraging.
>
> The javadoc for ContainerResponseFilter has:
> A filter for filtering a response after the response is returned
> from a resource class and before the response is written to the
> container.
>
> The comment "before the response is written" implies (to me) that
> the ContainerResponseFilter is called before the response is sent
> and/or that the implementation must write out the response. Knowing
> some of these details for how to do a cleanup following the
> transmission of a response would be very useful. Also, are there
> other changes (web.xml?)
>
> Does the @PreDestroy annotated method work?
>
> Any help on this would be much appreciated.
>
> Thanks
> David
>
>
>
> Craig McClanahan wrote:
>> On Wed, Nov 4, 2009 at 12:47 PM, Moises Lejter
>> <moilejter_at_gmail.com> wrote:
>>
>>> Would a CleanupFilter work? They get call "around" each HTTP
>>> request,
>>> right?
>>>
>>
>> More precisely, you'll want to take a look at ContainerResponseFilter
>> (for cleanup after the resource method returns) and perhaps
>> ContainerRequestFilter (to initialize things on the way in). It's
>> also possible to have a single Filter instance that implements both
>> interfaces if you need "around" type functionality. Note that, in
>> addition to cleanup, you can actually modify the response entity or
>> headers in a ContainerResponseFilter.
>>
>> There are some gotchas to be aware of if your resource method might
>> throw exceptions ... Google these class names for more info.
>>
>> Craig
>>
>>
>>> Moises
>>>
>>> On Wed, Nov 4, 2009 at 1:02 PM, dloy <david.loy_at_ucop.edu> wrote:
>>>
>>>> I would like to do cleanup after a Jersey request completes (e.g.
>>>> delete
>>>> temporary files, ...). Does Jersey provide any mechanism for
>>>> running a
>>>> process at the completion of an http request?
>>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>