Hi Marko
On 03/04/12 11:45, Voß, Marko wrote:
Hello Sergey,

I see. Thank you again. So do I even need to care about thread-safety on the 
services then or do I have to use the
SpringResourceFactory here?

As far as the thread safety of Contexts injected into the class fields is concerned, it does not matter what lifecycle a given root resource follows, the runtime will make sure the contexts are thread safe, injecting tl proxies in case of singletons, or plain instances in case per-request root resources.

The thread safety of the custom local data is another issue altogether, you can use SpringResourceFactory to enforce per-request instantiation of root resources if you'd to avoid syncing on the custom data/state...

Cheers, Sergey


Best regards,
Marko

Am 03.04.2012 10:34, schrieb Sergey Beryozkin:
Hi Marko
On 03/04/12 11:25, Voß, Marko wrote:
Hello,

We have to use @Context to get access to MessageContext and/or any of the other 
supported classes.

We cannot use @Context in the method signatures of the JAX-RS interfaces, 
because of these interfaces should be reused
on the client-side.

So we have to use @Context on class fields, which leads to the problem of 
thread-safety.

Even if I setup the service beans to be prototypes, they are still singletons, 
which is most-likely happening because of
the implementation of:

org.apache.cxf.jaxrs.JAXRSServiceFactoryBean#setResourceClassesFromBeans

Which is not following the rules of the spring beans, I guess.


You'd need to use SpringResourceFactory to get the bean scopes properly 
enforced, see
http://cxf.apache.org/docs/jaxrs-services-configuration.html#JAXRSServicesConfiguration-FromSpring

Also, when debugging this, I noticed that even the MessageContext instance was 
the same instance for multiple threads.
It's a thread-local proxy
So if I change the HttpHeaders in one thread, this will affect the HttpHeaders 
in another thread.

The thread safety is enforced via a thread local proxy, so it should not be the 
case


How am I supposed to use @Context, reuse the JAX-RS interface on client-side 
and be thread-safe? Any ideas?

Inject contexts in fields and this should be OK for proxies :-). From 2.5.3 it 
will be possible to use proxies with
Contexts in the method signatures too, proxies would need to set 'null'

HTH, Sergey


Best regards,







--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to