users@jersey.java.net

Re: [Jersey] Singleton resource filter

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 28 Aug 2009 11:12:17 +0200

On Aug 27, 2009, at 8:58 PM, Yong Yuan wrote:

> Hi,
>
> I'd like to collect some stats in a filter across all incoming
> requests for certain resources. In that case, is it possible to
> apply @Singleton annotation to a resource filter, or should I simply
> keep my stats in a singleton object, or a resource filter will be a
> singleton per resource as long as the resource is a singleton?

Container request/response filters or resource filter factories are
always instantiated as singletons and the life-cycle is independent of
the life-cycle of the resource. (Note that the Jersey life-cycle
annotations only apply to resource classes.)

So you can keep state in your filter, and of course you will need to
ensure that state is protected from concurrent access.

Paul.