users@jersey.java.net

[Jersey] Re: Does jersey create a new resource instance for every call?

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Fri, 21 Jan 2011 09:59:17 +0100

On Jan 20, 2011, at 9:37 PM, Tony Anecito wrote:

> Okay. I saw that this (creation of a new object) is standard for
> Jersey after I
> sent the last message. The reason it is important not to call the
> constructor is
> that is where connections to my EJB's was being made so I wanted it
> done only
> once.

Yes, that makes sense. The developer should change the scope if
expensive "resources" are created in the constructor or post construct
method.


> Metro seems to be doing that and it appeared to be faster than
> Jersey till
> I resolved this issue.
> Anyway, I decided to use @Singleton and got a 50% performance
> improvement I was
> looking for because the constructor was not being called all the time.
> In my case my post time went from 8.2milliseconds to 3.5
> milliseconds for round
> trip as measured at the client.
>
> Response time is important to me and conservative use of the CPU
> also. Creating
> a new Resource object for every call is not practical in my case.
>

Generally what i recommend is, if using CDI, for any expensive to be
defined in a separate class that can be injected into the resource
class.

Paul.