users@jersey.java.net

Re: State

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 14 Aug 2007 17:41:47 -0400

Dale,

If you are feeling adventurous you could try out the
"resourcefactory" branch I've been working on. Its still a work in
progress but all the unit tests are now passing and it supports both
per-request and singleton lifecycles for resource classes with per-
request being the default. Using Paul's example below, with this
branch you can instead write:

@UriTemplate("foo")
public class FooResource {

    public FooResource() {...}

    ...
}

and the runtime will create a new instance for each request. The
branch also now supports constructor parameters for per-request
resources so you can write resources like

@UriTemplate("foo/{id}")
public class FooResource {

    public FooResource(@UriParam("id") String id) {...}

    ...
}

Where the constructor does any initialization required for a
particular instance of a FooResource class before the resource method
is called.

To get back to a singleton lifecycle you can annotate a resource
class with the com.sun.ws.rest.spi.resource.Singleton annotation. You
can also annotate a resource class with the
com.sun.ws.rest.spi.resource.PerRequest annotation if you want to be
explicit about the expected lifecycle.

Marc.

On Aug 14, 2007, at 7:37 AM, Paul Sandoz wrote:

> Hi Dale,
>
> See the following thread [1] for some information on this. We are
> currently working out the details of state and life-cycle. Per-
> request will be the default.
>
> As of now the 0.2 stable and trunk builds create a per-application
> singleton of a root resource (one annotated with UriTemplate). To
> create a per-request instance you need to do something like the
> following from a root resource, for example:
>
> @UriTemplate("/")
> public class Resource {
>
> @UriTemplate("foo")
> public FooResource getFooResource(<parameters>) {
> return new FooResource(<parameters>, <more parameter>);
> }
> }
>
> A URI path begining with "/foo" will result in a new instance of
> FooResource for each request.
>
> Hope this helps,
> Paul.
>
> [1] https://jersey.dev.java.net/servlets/BrowseList?
> list=users&by=thread&from=884340
>
> Dale Peakall wrote:
>> Hi,
>> I guess I must be missing something quite fundamental here, but
>> how do I create a JAX-RS service for an object that has (non-
>> global/static) state?
>> Thanks.
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
> --
> | ? + ? = To question
> ----------------\
> Paul Sandoz
> x38109
> +33-4-76188109
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.