You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Dan Dubinsky <dd...@salmonllc.com> on 2008/06/17 16:42:29 UTC

Can a web service runing locally run in the same thread as the caller

Can a service called locally using the URL local://ServiceName run in the
same thread as the calling method. My code to invoke the service looks like
this

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(serviceClass);
factory.setAddress("local://" + serviceName);
factory.setDataBinding(new AegisDatabinding());
service = factory.create();

When I invoke a method on the service, it runs it fine but in a separate
thread. I'm having trouble because I have a Thread local variable in my
service that needs that's initialized by the calling thread, but isn't
present in the service thread.

-- 
View this message in context: http://www.nabble.com/Can-a-web-service-runing-locally-run-in-the-same-thread-as-the-caller-tp17911113p17911113.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Can a web service runing locally run in the same thread as the caller

Posted by Dan Dubinsky <dd...@salmonllc.com>.
Yes that worked. Thanks.

For anybody else that comes across this same problem, here is a code snippet
that solved the problem:

ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(serviceClass);
factory.setAddress("local://" + serviceName);
factory.setDataBinding(new AegisDatabinding());
service = factory.create();
//fix starts here
Client proxy = ClientProxy.getClient(service);
proxy.getOutInterceptors().add(new SingleThreadInterceptor());
//fix ends here

and the SingleThreadInterceptor class looks like this:

public class SingleThreadInterceptor extends
AbstractPhaseInterceptor<Message> {
    public SingleThreadInterceptor() {
        super(Phase.SETUP);
    }
    public void handleMessage(Message message) {
    	message.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE); 
    }
}  


dkulp wrote:
> 
> 
> The LocalConduit has a flag that can be used to specify that you want  
> direct dispatches.
> 
> LocalConduit.DIRECT_DISPATCH
> 
> 
> The problem is that it only looks at the message itself to see if that  
> is set.   With the JAX-WS frontend, you can set that in the request  
> context and it would work.   The problem is the request contexts only  
> exist in the JAX-WS frontend and your code for using the simple  
> frontend cannot use them.  I've logged an "improvement": 
> https://issues.apache.org/jira/browse/CXF-1654
> 
> With the current code, about the only thing you could do is write a  
> very simple interceptor that you stick on the out chain that does:
> 
> message.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
> 
> Dan
> 
> 
> 
> On Jun 17, 2008, at 10:42 AM, Dan Dubinsky wrote:
> 
>>
>> Can a service called locally using the URL local://ServiceName run  
>> in the
>> same thread as the calling method. My code to invoke the service  
>> looks like
>> this
>>
>> ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
>> factory.setServiceClass(serviceClass);
>> factory.setAddress("local://" + serviceName);
>> factory.setDataBinding(new AegisDatabinding());
>> service = factory.create();
>>
>> When I invoke a method on the service, it runs it fine but in a  
>> separate
>> thread. I'm having trouble because I have a Thread local variable in  
>> my
>> service that needs that's initialized by the calling thread, but isn't
>> present in the service thread.
>>
>> -- 
>> View this message in context:
>> http://www.nabble.com/Can-a-web-service-runing-locally-run-in-the-same-thread-as-the-caller-tp17911113p17911113.html
>> Sent from the cxf-user mailing list archive at Nabble.com.
>>
> 
> ---
> Daniel Kulp
> dkulp@apache.org
> http://www.dankulp.com/blog
> 
> 
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Can-a-web-service-runing-locally-run-in-the-same-thread-as-the-caller-tp17911113p17983189.html
Sent from the cxf-user mailing list archive at Nabble.com.


Re: Can a web service runing locally run in the same thread as the caller

Posted by Daniel Kulp <dk...@apache.org>.
The LocalConduit has a flag that can be used to specify that you want  
direct dispatches.

LocalConduit.DIRECT_DISPATCH


The problem is that it only looks at the message itself to see if that  
is set.   With the JAX-WS frontend, you can set that in the request  
context and it would work.   The problem is the request contexts only  
exist in the JAX-WS frontend and your code for using the simple  
frontend cannot use them.  I've logged an "improvement":  https://issues.apache.org/jira/browse/CXF-1654

With the current code, about the only thing you could do is write a  
very simple interceptor that you stick on the out chain that does:

message.put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);

Dan



On Jun 17, 2008, at 10:42 AM, Dan Dubinsky wrote:

>
> Can a service called locally using the URL local://ServiceName run  
> in the
> same thread as the calling method. My code to invoke the service  
> looks like
> this
>
> ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
> factory.setServiceClass(serviceClass);
> factory.setAddress("local://" + serviceName);
> factory.setDataBinding(new AegisDatabinding());
> service = factory.create();
>
> When I invoke a method on the service, it runs it fine but in a  
> separate
> thread. I'm having trouble because I have a Thread local variable in  
> my
> service that needs that's initialized by the calling thread, but isn't
> present in the service thread.
>
> -- 
> View this message in context: http://www.nabble.com/Can-a-web-service-runing-locally-run-in-the-same-thread-as-the-caller-tp17911113p17911113.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

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