You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Nathaniel Auvil <na...@gmail.com> on 2009/08/20 19:18:18 UTC

Thread Safety

I am implementing JAX-RS web service with CXF and i want to confirm my
Resource classes are thread safe.  i am thinking they are as the examples
show member variables holding various contexts and the like and no where
down the chain does the "AbstractService" extend java servlet.  Can someone
in the know please confirm?

Re: Thread Safety

Posted by Daniel Kulp <dk...@apache.org>.
Yes, that is safe.

The injected WebServiceContext is a wrapper around a ThreadLocal so any 
modifications and stuff to it are affecting the current thread/request only.

Dan


On Tue August 25 2009 8:38:27 am Nicola D'Elia wrote:
> Hi,
> i have a similar question.
>
> I have a spring bean exposed as web service: it is a singleton and gets the
> context injected to work with the http session:
>
> ...
>     @Resource
>     private WebServiceContext context;
>
>     public String login(String username) {
>         String challenge = auth.login(username);
>         if (challenge != null) {
>              ((HttpServletRequest)
> context.getMessageContext().get(HTTP_REQUEST)).getSession().setAttribute(US
>ERNAME, username);
> ...
>
>
> Is it safe or i should turn the bean into a prototype ?
>
>
>
> Thanks,
> Nicola
>
> On Fri, Aug 21, 2009 at 3:00 PM, Sergey Beryozkin 
<sb...@progress.com>wrote:
> > Hi
> >
> > I thought you were asking about fileds like
> >
> > @Context
> > private HttpHeaders headers;
> >
> > such fields are indeed thred-safe even with singleton resource classes,
> > it is thread-safe proxies which are injected in such cases.
> >
> > Now, as far as supporting Spring prototypes is concerned I haven't got to
> > fixing this JIRA yet :
> >
> > http://issues.apache.org/jira/browse/CXF-2197
> >
> > You might want to try using CXFNonSpringJaxrsServlet and set a
> > jaxrs.scope parameter to 'prototype' or register a JAXRS Application impl
> > - that is the only way at the moment to work with prototypes
> > (programmatic config is supported too)
> >
> > cheers, Sergey
> >
> > ----- Original Message ----- From: "Nathaniel Auvil" <
> > nathaniel.auvil@gmail.com>
> > To: <us...@cxf.apache.org>
> > Sent: Friday, August 21, 2009 1:15 PM
> > Subject: Re: Thread Safety
> >
> >
> >
> >  I thought a singleton was not threadsafe by default, such as a Java
> >
> >> Servlet?  I did a more thorough reading of the CXF docs and saw the
> >> Spring configured Resource classes are Singletons and not Prototype
> >> Beans.  So in other words, each request will share the same instance of
> >> my
> >> AccountService
> >> Resource class.  If i have a member variable: private Account account,
> >> this
> >> will be shared by all requests and get overwritten by each request.
> >>  Right?
> >>
> >>
> >>
> >>
> >> On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <sberyozk@progress.com
> >>
> >> >wrote:
> >>
> >>  Hi
> >>
> >>> ----- Original Message ----- From: "Nathaniel Auvil" <
> >>> nathaniel.auvil@gmail.com>
> >>> To: <us...@cxf.apache.org>
> >>> Sent: Thursday, August 20, 2009 6:18 PM
> >>> Subject: Thread Safety
> >>>
> >>>
> >>>
> >>>  I am implementing JAX-RS web service with CXF and i want to confirm my
> >>>
> >>>> Resource classes are thread safe.  i am thinking they are as the
> >>>> examples
> >>>> show member variables holding various contexts and the like and no
> >>>> where down the chain does the "AbstractService" extend java servlet. 
> >>>> Can someone
> >>>> in the know please confirm?
> >>>>
> >>>>
> >>>>  All the fields containing injected contexts are thread safe, when
> >>>
> >>> resource
> >>> classes are singletons, so yes resource classes are thread safe.
> >>>
> >>>  and no where down the chain does the "AbstractService" extend java
> >>>
> >>>> servlet.
> >>>
> >>> Can you clarify please what you meant to say ?
> >>>
> >>> thanks, Sergey

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog

Re: Thread Safety

Posted by Sergey Beryozkin <se...@iona.com>.
CXF JAXRS now supports spring prototypes

http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Resourcelifecycles
http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Lifecyclemanagement 

I reckon it should be possible to create a utility factory the JAXRS runtime
uses for JAXWS too which will implement a CXF Factory interface...

cheers, Sergey



Nathaniel Auvil wrote:
> 
> My understanding is they do not support Spring Prototype beans....so your
> resource classes are Singletons.  Therefore any members are most likely
> not
> Threadsafe unless they are using ThreadLocal or some such trickery under
> the
> covers.
> 
> 
> On Tue, Aug 25, 2009 at 8:38 AM, Nicola D'Elia
> <ni...@gmail.com>wrote:
> 
>> Hi,
>> i have a similar question.
>>
>> I have a spring bean exposed as web service: it is a singleton and gets
>> the
>> context injected to work with the http session:
>>
>> ...
>>    @Resource
>>    private WebServiceContext context;
>>
>>    public String login(String username) {
>>        String challenge = auth.login(username);
>>        if (challenge != null) {
>>             ((HttpServletRequest)
>>
>> context.getMessageContext().get(HTTP_REQUEST)).getSession().setAttribute(USERNAME,
>> username);
>> ...
>>
>>
>> Is it safe or i should turn the bean into a prototype ?
>>
>>
>>
>> Thanks,
>> Nicola
>>
>>
>>
>>
>> On Fri, Aug 21, 2009 at 3:00 PM, Sergey Beryozkin <sberyozk@progress.com
>> >wrote:
>>
>> > Hi
>> >
>> > I thought you were asking about fileds like
>> >
>> > @Context
>> > private HttpHeaders headers;
>> >
>> > such fields are indeed thred-safe even with singleton resource classes,
>> it
>> > is thread-safe proxies which are injected in such cases.
>> >
>> > Now, as far as supporting Spring prototypes is concerned I haven't got
>> to
>> > fixing this JIRA yet :
>> >
>> > http://issues.apache.org/jira/browse/CXF-2197
>> >
>> > You might want to try using CXFNonSpringJaxrsServlet and set a
>> jaxrs.scope
>> > parameter to 'prototype' or register a JAXRS Application impl - that is
>> the
>> > only way at the moment to work with prototypes (programmatic config is
>> > supported too)
>> >
>> > cheers, Sergey
>> >
>> > ----- Original Message ----- From: "Nathaniel Auvil" <
>> > nathaniel.auvil@gmail.com>
>> > To: <us...@cxf.apache.org>
>> > Sent: Friday, August 21, 2009 1:15 PM
>> > Subject: Re: Thread Safety
>> >
>> >
>> >
>> >  I thought a singleton was not threadsafe by default, such as a Java
>> >> Servlet?  I did a more thorough reading of the CXF docs and saw the
>> Spring
>> >> configured Resource classes are Singletons and not Prototype Beans. 
>> So
>> in
>> >> other words, each request will share the same instance of my
>> >> AccountService
>> >> Resource class.  If i have a member variable: private Account account,
>> >> this
>> >> will be shared by all requests and get overwritten by each request.
>> >>  Right?
>> >>
>> >>
>> >>
>> >>
>> >> On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <
>> sberyozk@progress.com
>> >> >wrote:
>> >>
>> >>  Hi
>> >>>
>> >>> ----- Original Message ----- From: "Nathaniel Auvil" <
>> >>> nathaniel.auvil@gmail.com>
>> >>> To: <us...@cxf.apache.org>
>> >>> Sent: Thursday, August 20, 2009 6:18 PM
>> >>> Subject: Thread Safety
>> >>>
>> >>>
>> >>>
>> >>>  I am implementing JAX-RS web service with CXF and i want to confirm
>> my
>> >>>
>> >>>> Resource classes are thread safe.  i am thinking they are as the
>> >>>> examples
>> >>>> show member variables holding various contexts and the like and no
>> where
>> >>>> down the chain does the "AbstractService" extend java servlet.  Can
>> >>>> someone
>> >>>> in the know please confirm?
>> >>>>
>> >>>>
>> >>>>  All the fields containing injected contexts are thread safe, when
>> >>> resource
>> >>> classes are singletons, so yes resource classes are thread safe.
>> >>>
>> >>>  and no where down the chain does the "AbstractService" extend java
>> >>>
>> >>>> servlet.
>> >>>>
>> >>>>
>> >>> Can you clarify please what you meant to say ?
>> >>>
>> >>> thanks, Sergey
>> >>>
>> >>>
>> >>
>> >
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/Thread-Safety-tp25066315p25722063.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Thread Safety

Posted by Nathaniel Auvil <na...@gmail.com>.
My understanding is they do not support Spring Prototype beans....so your
resource classes are Singletons.  Therefore any members are most likely not
Threadsafe unless they are using ThreadLocal or some such trickery under the
covers.


On Tue, Aug 25, 2009 at 8:38 AM, Nicola D'Elia <ni...@gmail.com>wrote:

> Hi,
> i have a similar question.
>
> I have a spring bean exposed as web service: it is a singleton and gets the
> context injected to work with the http session:
>
> ...
>    @Resource
>    private WebServiceContext context;
>
>    public String login(String username) {
>        String challenge = auth.login(username);
>        if (challenge != null) {
>             ((HttpServletRequest)
>
> context.getMessageContext().get(HTTP_REQUEST)).getSession().setAttribute(USERNAME,
> username);
> ...
>
>
> Is it safe or i should turn the bean into a prototype ?
>
>
>
> Thanks,
> Nicola
>
>
>
>
> On Fri, Aug 21, 2009 at 3:00 PM, Sergey Beryozkin <sberyozk@progress.com
> >wrote:
>
> > Hi
> >
> > I thought you were asking about fileds like
> >
> > @Context
> > private HttpHeaders headers;
> >
> > such fields are indeed thred-safe even with singleton resource classes,
> it
> > is thread-safe proxies which are injected in such cases.
> >
> > Now, as far as supporting Spring prototypes is concerned I haven't got to
> > fixing this JIRA yet :
> >
> > http://issues.apache.org/jira/browse/CXF-2197
> >
> > You might want to try using CXFNonSpringJaxrsServlet and set a
> jaxrs.scope
> > parameter to 'prototype' or register a JAXRS Application impl - that is
> the
> > only way at the moment to work with prototypes (programmatic config is
> > supported too)
> >
> > cheers, Sergey
> >
> > ----- Original Message ----- From: "Nathaniel Auvil" <
> > nathaniel.auvil@gmail.com>
> > To: <us...@cxf.apache.org>
> > Sent: Friday, August 21, 2009 1:15 PM
> > Subject: Re: Thread Safety
> >
> >
> >
> >  I thought a singleton was not threadsafe by default, such as a Java
> >> Servlet?  I did a more thorough reading of the CXF docs and saw the
> Spring
> >> configured Resource classes are Singletons and not Prototype Beans.  So
> in
> >> other words, each request will share the same instance of my
> >> AccountService
> >> Resource class.  If i have a member variable: private Account account,
> >> this
> >> will be shared by all requests and get overwritten by each request.
> >>  Right?
> >>
> >>
> >>
> >>
> >> On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <
> sberyozk@progress.com
> >> >wrote:
> >>
> >>  Hi
> >>>
> >>> ----- Original Message ----- From: "Nathaniel Auvil" <
> >>> nathaniel.auvil@gmail.com>
> >>> To: <us...@cxf.apache.org>
> >>> Sent: Thursday, August 20, 2009 6:18 PM
> >>> Subject: Thread Safety
> >>>
> >>>
> >>>
> >>>  I am implementing JAX-RS web service with CXF and i want to confirm my
> >>>
> >>>> Resource classes are thread safe.  i am thinking they are as the
> >>>> examples
> >>>> show member variables holding various contexts and the like and no
> where
> >>>> down the chain does the "AbstractService" extend java servlet.  Can
> >>>> someone
> >>>> in the know please confirm?
> >>>>
> >>>>
> >>>>  All the fields containing injected contexts are thread safe, when
> >>> resource
> >>> classes are singletons, so yes resource classes are thread safe.
> >>>
> >>>  and no where down the chain does the "AbstractService" extend java
> >>>
> >>>> servlet.
> >>>>
> >>>>
> >>> Can you clarify please what you meant to say ?
> >>>
> >>> thanks, Sergey
> >>>
> >>>
> >>
> >
>

Re: Thread Safety

Posted by Nicola D'Elia <ni...@gmail.com>.
Hi,
i have a similar question.

I have a spring bean exposed as web service: it is a singleton and gets the
context injected to work with the http session:

...
    @Resource
    private WebServiceContext context;

    public String login(String username) {
        String challenge = auth.login(username);
        if (challenge != null) {
             ((HttpServletRequest)
context.getMessageContext().get(HTTP_REQUEST)).getSession().setAttribute(USERNAME,
username);
...


Is it safe or i should turn the bean into a prototype ?



Thanks,
Nicola




On Fri, Aug 21, 2009 at 3:00 PM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
> I thought you were asking about fileds like
>
> @Context
> private HttpHeaders headers;
>
> such fields are indeed thred-safe even with singleton resource classes, it
> is thread-safe proxies which are injected in such cases.
>
> Now, as far as supporting Spring prototypes is concerned I haven't got to
> fixing this JIRA yet :
>
> http://issues.apache.org/jira/browse/CXF-2197
>
> You might want to try using CXFNonSpringJaxrsServlet and set a jaxrs.scope
> parameter to 'prototype' or register a JAXRS Application impl - that is the
> only way at the moment to work with prototypes (programmatic config is
> supported too)
>
> cheers, Sergey
>
> ----- Original Message ----- From: "Nathaniel Auvil" <
> nathaniel.auvil@gmail.com>
> To: <us...@cxf.apache.org>
> Sent: Friday, August 21, 2009 1:15 PM
> Subject: Re: Thread Safety
>
>
>
>  I thought a singleton was not threadsafe by default, such as a Java
>> Servlet?  I did a more thorough reading of the CXF docs and saw the Spring
>> configured Resource classes are Singletons and not Prototype Beans.  So in
>> other words, each request will share the same instance of my
>> AccountService
>> Resource class.  If i have a member variable: private Account account,
>> this
>> will be shared by all requests and get overwritten by each request.
>>  Right?
>>
>>
>>
>>
>> On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <sberyozk@progress.com
>> >wrote:
>>
>>  Hi
>>>
>>> ----- Original Message ----- From: "Nathaniel Auvil" <
>>> nathaniel.auvil@gmail.com>
>>> To: <us...@cxf.apache.org>
>>> Sent: Thursday, August 20, 2009 6:18 PM
>>> Subject: Thread Safety
>>>
>>>
>>>
>>>  I am implementing JAX-RS web service with CXF and i want to confirm my
>>>
>>>> Resource classes are thread safe.  i am thinking they are as the
>>>> examples
>>>> show member variables holding various contexts and the like and no where
>>>> down the chain does the "AbstractService" extend java servlet.  Can
>>>> someone
>>>> in the know please confirm?
>>>>
>>>>
>>>>  All the fields containing injected contexts are thread safe, when
>>> resource
>>> classes are singletons, so yes resource classes are thread safe.
>>>
>>>  and no where down the chain does the "AbstractService" extend java
>>>
>>>> servlet.
>>>>
>>>>
>>> Can you clarify please what you meant to say ?
>>>
>>> thanks, Sergey
>>>
>>>
>>
>

Re: Thread Safety

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

I thought you were asking about fileds like

@Context
private HttpHeaders headers;

such fields are indeed thred-safe even with singleton resource classes, it is thread-safe proxies which are injected in such cases.

Now, as far as supporting Spring prototypes is concerned I haven't got to fixing this JIRA yet :

http://issues.apache.org/jira/browse/CXF-2197

You might want to try using CXFNonSpringJaxrsServlet and set a jaxrs.scope parameter to 'prototype' or register a JAXRS Application 
impl - that is the only way at the moment to work with prototypes (programmatic config is supported too)

cheers, Sergey

----- Original Message ----- 
From: "Nathaniel Auvil" <na...@gmail.com>
To: <us...@cxf.apache.org>
Sent: Friday, August 21, 2009 1:15 PM
Subject: Re: Thread Safety


>I thought a singleton was not threadsafe by default, such as a Java
> Servlet?  I did a more thorough reading of the CXF docs and saw the Spring
> configured Resource classes are Singletons and not Prototype Beans.  So in
> other words, each request will share the same instance of my AccountService
> Resource class.  If i have a member variable: private Account account, this
> will be shared by all requests and get overwritten by each request.  Right?
>
>
>
>
> On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <sb...@progress.com>wrote:
>
>> Hi
>>
>> ----- Original Message ----- From: "Nathaniel Auvil" <
>> nathaniel.auvil@gmail.com>
>> To: <us...@cxf.apache.org>
>> Sent: Thursday, August 20, 2009 6:18 PM
>> Subject: Thread Safety
>>
>>
>>
>>  I am implementing JAX-RS web service with CXF and i want to confirm my
>>> Resource classes are thread safe.  i am thinking they are as the examples
>>> show member variables holding various contexts and the like and no where
>>> down the chain does the "AbstractService" extend java servlet.  Can
>>> someone
>>> in the know please confirm?
>>>
>>>
>> All the fields containing injected contexts are thread safe, when resource
>> classes are singletons, so yes resource classes are thread safe.
>>
>>  and no where down the chain does the "AbstractService" extend java
>>> servlet.
>>>
>>
>> Can you clarify please what you meant to say ?
>>
>> thanks, Sergey
>>
> 


Re: Thread Safety

Posted by Nathaniel Auvil <na...@gmail.com>.
I thought a singleton was not threadsafe by default, such as a Java
Servlet?  I did a more thorough reading of the CXF docs and saw the Spring
configured Resource classes are Singletons and not Prototype Beans.  So in
other words, each request will share the same instance of my AccountService
Resource class.  If i have a member variable: private Account account, this
will be shared by all requests and get overwritten by each request.  Right?




On Fri, Aug 21, 2009 at 6:19 AM, Sergey Beryozkin <sb...@progress.com>wrote:

> Hi
>
> ----- Original Message ----- From: "Nathaniel Auvil" <
> nathaniel.auvil@gmail.com>
> To: <us...@cxf.apache.org>
> Sent: Thursday, August 20, 2009 6:18 PM
> Subject: Thread Safety
>
>
>
>  I am implementing JAX-RS web service with CXF and i want to confirm my
>> Resource classes are thread safe.  i am thinking they are as the examples
>> show member variables holding various contexts and the like and no where
>> down the chain does the "AbstractService" extend java servlet.  Can
>> someone
>> in the know please confirm?
>>
>>
> All the fields containing injected contexts are thread safe, when resource
> classes are singletons, so yes resource classes are thread safe.
>
>  and no where down the chain does the "AbstractService" extend java
>> servlet.
>>
>
> Can you clarify please what you meant to say ?
>
> thanks, Sergey
>

Re: Thread Safety

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

----- Original Message ----- 
From: "Nathaniel Auvil" <na...@gmail.com>
To: <us...@cxf.apache.org>
Sent: Thursday, August 20, 2009 6:18 PM
Subject: Thread Safety


>I am implementing JAX-RS web service with CXF and i want to confirm my
> Resource classes are thread safe.  i am thinking they are as the examples
> show member variables holding various contexts and the like and no where
> down the chain does the "AbstractService" extend java servlet.  Can someone
> in the know please confirm?
>

All the fields containing injected contexts are thread safe, when resource classes are singletons, so yes resource classes are 
thread safe.

> and no where down the chain does the "AbstractService" extend java servlet.

Can you clarify please what you meant to say ?

thanks, Sergey