users@jersey.java.net

Re: [Jersey] Business object for jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 07 Oct 2009 22:04:44 +0200

On Oct 6, 2009, at 6:12 PM, cepam wrote:

>
> Hi,
>
> Thanks a lot for your answer.
>
> Yes, I need that you describe your first proposition. For example, I
> want to
> create only one business object who is charge to communicate whis a
> Database
> and manage the differents concurrent requests from a lot of resource
> methods.
>
> Can you give me more details regarding your small example:
>
> For class MySingletonResource, if the constructor is for example :
> MySingletonRessource(File config), how (where) to instanciate it
> (new ...)
> for my whole web appli ?
>

I was going to present a very simple solution using resource classes
but i found a bug when verifying, drat!

So... there are two other choices:

1) Use a DI framework like Guice and Jersey's Guice integration; or

2) Uses Jersey's injection mechanism and register an injectable
provider.


For 2) you can create an injectable provider as follows:

   MyDBService s = ..
   SingletonTypeInjectableProvider<Context, MyDBService> ip =
      new SingletonTypeInjectableProvider<Context,
MyDBService(MyDBService.class, s) {};

this defines that the instance of MyDBService will be injected when
using @Context, for example:

   @Context MyDBService s;

Then we need to register the SingletonTypeInjectableProvider. There
are two choices:

1) Override the ServletContainer.configure method and do:

        protected void configure(WebConfig wc, ResourceConfig rc,
WebApplication wa) {
          super.configure(wc, rc, wa);

          MyDBService s = ...
          SingletonTypeInjectableProvider<Context, MyDBService> ip =
            new SingletonTypeInjectableProvider<Context,
MyDBService(MyDBService.class, s) {};
          rc.getSingletons().add(ip);
        }

2) Or register your own implementation of javax.ws.rs.core.Application
or ResourceConfig and do:

      public class MyResourceConfig extends ... {
         public MyResourceConfig() {
          MyDBService s = ...
          SingletonTypeInjectableProvider<Context, MyDBService> ip =
            new SingletonTypeInjectableProvider<Context,
MyDBService(MyDBService.class, s) {};
          getSingletons().add(ip);
         }
      }

Note that the class that extends from SingletonTypeInjectableProvider
can have a @PreDestroy method that will be called when the web app
shuts down.

Also note that It is not Jersey's intention to be a feature complete
DI framework, the classes used are those that also support JAX-RS/
Jersey requirements and happen to be useful to developers for advanced
use-cases. Hence the rather involved set of steps which can be easier
if using something like Guice.


> I suppose that MySingleton resource must be thread safe ?
>

Yes.


> The annotation @Pathname in some resource method will not be
> 'disturbed' by
> the @Context annotation ?
>

Do you mean @Path ?


> If necessary, I can post a piece of code.

That would help,
Paul.


> Thanks.
>
>
>
>
> Paul Sandoz wrote:
>>
>> Hi,
>>
>> Do you want some kind of singleton object to be injected into the
>> resource classes (the classes that contain the resource methods
>> annotated with @GET etc) ?
>>
>> If, this is going to get a lot easier when we release EE 6 with
>> support for 299 and managed beans. In the mean time you can do the
>> following:
>>
>> @Singleton // see [1]
>> public class MySingletonResource {
>> ...
>> }
>>
>>
>> @Path("/blah")
>> public class MyResourceClass {
>>
>> @GET
>> public ... get(@Context ResourceContext rc) { // see [2]
>> MySingletonResource msr =
>> rc.getResource(MySingletonResource.class);
>> }
>> }
>>
>>
>> If you want the MyResourceClass to be a singleton you can do:
>>
>> @Singleton
>> @Path("/blah")
>> public class MyResourceClass {
>> }
>>
>>
>> You do not have to use EJBs, but that is one option, but i recommend
>> only using it with EJB 3.1 and Java EE 6 which significantly improves
>> the ease of use of EJBs.
>>
>> Otherwise, if this is not what you require a simple example of what
>> you want to do will help me understand better your needs,
>> Paul.
>>
>> [1]
>> https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/spi/resource/Singleton.html
>> [2]
>> https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/api/core/ResourceContext.html
>>
>> On Oct 5, 2009, at 2:28 PM, cepam wrote:
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Business-object-for-jersey-tp3768500p3776023.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>