users@jersey.java.net

[Jersey] Re: Where to put the application logic of a Jersey server

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 13 Feb 2015 18:06:55 +0100

Normally I would say that you may want to create a single instance of your worker thread manager in your JAX-RS Application subclass (e.g. as a static final field) and then return this instance from your getSingletons() method.
But you say that classloader is constructing your class multiple times, which means that you are most likely trying to deploy multiple versions of the application (or do a lot of deploy/undeploy operations) - am I right? (If not, chances are, you have a classpath configuration problem.)

If you indeed deploy multiple applications (or do frequent deploy/undeploy), you may want to extract your worker thread manager into a standalone library that is placed on a "system classpath" of your application server. And then you have multiple options how to get hold of the singleton instance - using a static getInstance() mehtod or binding a singleton into JNDI are two of them.

Cheers,
Marek

> On 13 Feb 2015, at 14:03, ax487 <ax487_at_gmx.de> wrote:
>
> Hello all,
>
> I have been using Jersey to write the front end of a REST service, and I
> am quite pleased with the API / performance so far.
>
> My server is designed to take requests, perform some calculations and
> provide a set of results for each request. I need one place (I guess a
> singleton class) to gather the requests, perform the computations (using
> some worker threads) and provide the results. I guess that due to the
> presence of a large number of worker threads I will have to put some
> synchronization in place as well.
>
> My question is: How can I make sure that such a class is constructed
> before the server starts answering queries? I am writing a
> WebApplication and not an ordinary java application so there is no
> `main()` method which would be the obvious place. I tried extending the
> `javax.ws.rs.core.Application` which acts as a resource configurator but
> this class seems to be constructed multiple times by the ClassLoader.
> Are the singletons which I can register in the application guaranteed to
> be constructed in time?
>
> ax487