且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这个JAX-WS客户端调用线程是否安全?

更新时间:2023-09-11 22:01:40

根据 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.