users@jersey.java.net

[Jersey] Re: Livetime of singletons

From: Markus Karg <karg_at_quipsy.de>
Date: Mon, 17 Jan 2011 12:23:05 +0100

Paul,

thank you for the clarification. So I suspect you will change the
JavaDocs automatically and I don't need to file an extra enhancement
request?

BTW, I noticed that Jersey 1.4 calls getSingleTons() at least twice, see
this code and trace:

public Set<Object> getSingletons() {
  System.out.println("*Hello*");
  return MyApplication.singletons;
}

INFO: Initiating Jersey application, version 'Jersey: 1.4 09/11/2010
10:41 PM'
17.01.2011 12:20:28
com.sun.jersey.server.impl.application.DeferredResourceConfig$Applicatio
nHolder <init>
INFO: Instantiated the Application class
de.quipsy.application.QuipsyApplication
*Hello*
*Hello*
17.01.2011 12:20:28 com.sun.grizzly.Controller logVersion
INFO: Starting Grizzly Framework 1.9.18-e - Mon Jan 17 12:20:28 CET 2011

Is that a bug? Shall I file a report?

Regards
Markus

-----Original Message-----
From: Paul Sandoz [mailto:Paul.Sandoz_at_oracle.com]
Sent: Montag, 17. Januar 2011 12:07
To: Markus Karg
Cc: users_at_jersey.java.net
Subject: Re: [Jersey] Livetime of singletons


On Jan 14, 2011, at 4:56 PM, Markus Karg wrote:

> The description of "Application" doesn't contain information on the
> actual livetime of the result of "getSingletons()". I wonder whether
> it is valid to return new objects each time, or new objects for each
> instance of "Application", or whether "getSingletons()" actually
> must return the same object instances always and for all instances
> (i. e. final static)?

I think the JavaDoc should be clarified. Each call to getSingletons()
within the same web app should return a set with the same references.
It does not matter if instances are shared between web apps as that is
an implementation detail of your application. The "singleton" in this
regard means that the JAX-RS implementation can rely on the reference
being valid for the life-cycle of the web app.

FWIW Jersey will call getSingletons() once.

Paul.