这个JAX-WS客户端调用线程是否安全? [英] Is this JAX-WS client call thread safe?

查看:116
本文介绍了这个JAX-WS客户端调用线程是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于WS客户端服务和端口的初始化需要很长时间,我喜欢在启动时初始化它们并重用相同的端口实例。
初始化看起来像这样:

Since initialization of the WS client service and port takes ages I like to initialize them once at startup and reuse the same instance of the port. Initalization would look something like this:

private static RequestContext requestContext = null;

static
{
    MyService service = new MyService(); 
    MyPort myPort = service.getMyServicePort(); 

    Map<String, Object> requestContextMap = ((BindingProvider) myPort).getRequestContext();
    requestContextMap = ((BindingProvider)myPort).getRequestContext(); 
    requestContextMap.put(BindingProvider.USERNAME_PROPERTY, uName); 
    requestContextMap.put(BindingProvider.PASSWORD_PROPERTY, pWord); 

    rc = new RequestContext();
    rc.setApplication("test");
    rc.setUserId("test");
}

我班上某个地方的电话:

The call somewhere in my class:

myPort.someFunctionCall(requestContext, "someValue");

我的问题:这个电话会是线程安全的吗?

My question: Will this call be thread-safe?

推荐答案

根据 CXF常见问题解答


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

官方JAX-WS答案:否。
根据JAX-WS规范,客户端代理不是线程安全的。
要编写可移植代码,您应该将它们视为非线程安全且
同步访问或使用实例池或类似实例。

Official JAX-WS answer: No. According to the JAX-WS spec, the client proxies are NOT thread safe. To write portable code, you should treat them as non-thread safe and synchronize access or use a pool of instances or similar.

CXF回答:对于许多用例,CXF代理是线程安全的。
例外是:

CXF answer: CXF proxies are thread safe for MANY use cases. The exceptions are:


  • 使用((BindingProvider)代理).getRequestContext( ) - 根据JAX-WS规范,
    请求上下文为PER INSTANCE。因此,那里设置的任何内容都将
    影响其他线程上的请求。使用CXF,您可以:

  • Use of ((BindingProvider)proxy).getRequestContext() - per JAX-WS spec, the request context is PER INSTANCE. Thus, anything set there will affect requests on other threads. With CXF, you can do:

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

以及对getRequestContext()的未来调用将使用线程
本地请求上下文。这允许请求上下文为
threadsafe。 (注意:响应上下文在CXF中始终是线程本地的)

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)

管道上的设置 - 如果您使用代码或配置直接
操纵管道(比如设置TLS设置或类似设置),那些
不是线程安全的。管道是每个实例,因此将共享那些
设置。此外,如果您使用FailoverFeature和
LoadBalanceFeatures,则会立即替换管道。因此,管道上设置的
设置在用于
设置线程之前可能会丢失。

Settings on the conduit - if you use code or configuration to directly manipulate the conduit (like to set TLS settings or similar), those are not thread safe. The conduit is per-instance and thus those settings would be shared. Also, if you use the FailoverFeature and LoadBalanceFeatures, the conduit is replaced on the fly. Thus, settings set on the conduit could get lost before being used on the setting thread.

For管道问题,你可以安装一个新的
ConduitSelector使用本地或类似的线程。这虽然有点
复杂。

For the conduit issues, you COULD install a new ConduitSelector that uses a thread local or similar. That's a bit complex though.

对于大多数简单用例,你可以在多个
线程上使用CXF代理。以上概述了其他人的解决方法。

For most "simple" use cases, you can use CXF proxies on multiple threads. The above outlines the workarounds for the others.

这篇关于这个JAX-WS客户端调用线程是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆