With CXF, you can do:

((BindingProvider)proxy).getRequestContext().put("thread.local.request.context",
 "true");

and future calls to getRequestContext() will use a thread local request 
context. That allows the request context to be threadsafe. (Note: the response 
context is always thread local in CXF)

http://cxf.apache.org/faq.html

Cheers,
Andrei.

> -----Original Message-----
> From: jaybytez [mailto:[email protected]]
> Sent: Dienstag, 4. Dezember 2012 20:26
> To: [email protected]
> Subject: JaxWsProxyFactoryBean multi-threaded stateful services
> 
> I have a simple question that I just want some verification on with multi-
> threading/singleton/BindingProvider.
> 
> We are using CXF 2.4.5, Spring 3.0.5, and we have a
> DefaultMessageListenerContainer that has a min/max of 5/10 consumers.
> Within that class, we have a remote service that we will be calling, but we
> have to manage cookies for stateful invocation.
> 
> The remote service interface is a class level variable like this example from
> Spring below, that is set to the default scope of singleton:
> 
> http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#bea
> ns-factory-class-instance-factory-method
> 
> public class BusinessLogicServiceImpl implements BusinessLogicService {
> 
>     private transient AccessServiceRemote accessServiceRemote;
> 
> <bean id="businessLogicService " class="com.foo.BusinessLogicServiceImpl"
>                         p:accessServiceRemote-
> ref="accessServiceRemote"/>
> 
> <bean id="accessServiceRemote" class="com.foo.accessService"
>           factory-bean="accessServiceProxyFactory" factory-
> method="create"
> lazy-init="false"/>
> 
> <bean id="accessServiceProxyFactory"
> class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" lazy-init="true">
>       <property name="serviceClass" value="com.foo.accessService"/>
>       <property name="address" value="${external.url}"/> </bean>
> 
> In order to do stateful invocations, we have to determine if the service is
> proxied by BindingProvider so that we can use BindingProvider to
> getRequestContext and then put a user context id into the HTTP Headers for
> all calls (after our 3rd party login).
> 
> if (Proxy.isProxyClass(service.getClass()) &&
> BindingProvider.class.isAssignableFrom(service.getClass())) {
>             Map<String, Object> requestContext = ((BindingProvider)
> service).getRequestContext();
>             if (MapUtils.isNotEmpty(requestContext)) {
>                 if (!startState) {
>                     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
> 
> HttpHeaderThreadLocal.getHttpHeader(stateId.getUserContextId()));
>                 }
>             }
>         }
> 
> My concern is that the remote service class level variable
> accessServiceRemote is instantiated via Spring's default scope of singleton
> (via JaxWsProxyFactoryBean).  So I would assume that multiple threads will
> set the HTTP Headers (via the requestContext.put) and keep stepping on
> each other, causing a threading issue.
> 
> Should it just be as simple as changing the scope on the
> accessServiceRemote to prototype?
> 
> <bean id="accessServiceRemote" class="com.foo.AccessServiceRemote"
>           factory-bean="accessServiceProxyFactory" factory-
> method="create"
> lazy-init="false" scope="prototype"/>
> 
> Thanks...jay
> 
> 
> 
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/JaxWsProxyFactoryBean-multi-threaded-
> stateful-services-tp5719659.html
> Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to