JAX-WS客户端线程安全吗?

JAX-WS客户端线程安全吗?

问题描述:

由于WS客户端端口的初始化非常昂贵,因此我们想重用同一实例.我们还想在每次调用之前在BindingProvider/RequestContext中设置不同的值.最初,我们想这样做:

Because the initialization of the WS client side port is so costly we would like to reuse the same instance. We would also like to set different values in the BindingProvider/RequestContext before each call. Initially we would like to do this:

MyService service = new MyService(wsdlURL, name); 
MyPort myPort = service .getMyServicePort(); 

然后,在每次调用之前执行此操作:

then later, before each call do this:

Map requestContext = ((BindingProvider)myPort ).getRequestContext(); 
requestContext.put(BindingProvider.USERNAME_PROPERTY, uName); 
requestContext.put(BindingProvider.PASSWORD_PROPERTY, pWord); 
myPort.someFunctionCall();

我的问题是,此代码线程安全吗? JAX-WS文档似乎表明它是线程安全的.但是,如果您采取预防措施, CXF似乎是这样.如果特别是JAX-WS和 Metro 不是线程安全的,是否有任何方法可以确保线程安全而不同步访问进行端口或ws操作?

My question is, is this code thread safe? JAX-WS documentation seems to indicate that it is not thread safe. However, CXF seems to be so if you take precautions. If JAX-WS and Metro in particular is not thread safe, is there any way of ensuring thread safety without synchronizing access to port or ws operations?

对于JAX-WS/Metro,这绝对不是线程安全的.最好的选择是创建一个代理池,并在需要时从池中提取代理,进行配置,使用,清除设置值,然后返回到池中.

For JAX-WS/Metro, that's definitely not thread safe. The best bet is to create a Pool of proxies and, when needed, pull a proxy from the pool, configure it, use it, clear the set values, return to the pool.

或使用CXF.