gpt4 book ai didi

java - 这个 JAX-WS 客户端调用线程安全吗?

转载 作者:IT老高 更新时间:2023-10-28 21:06:22 26 4
gpt4 key购买 nike

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

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");
}

我类某处的电话:

myPort.someFunctionCall(requestContext, "someValue");

我的问题:这个调用是线程安全的吗?

最佳答案

根据CXF FAQ :

Are JAX-WS client proxies thread safe?

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 answer: CXF proxies are thread safe for MANY use cases. The exceptions are:

  • 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");

    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)

  • 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.

  • Session support - if you turn on sessions support (see jaxws spec), the session cookie is stored in the conduit. Thus, it would fall into the above rules on conduit settings and thus be shared across threads.
  • WS-Security tokens - If use WS-SecureConversation or WS-Trust, the retrieved token is cached in the Endpoint/Proxy to avoid the extra (and expensive) calls to the STS to obtain tokens. Thus, multiple threads will share the token. If each thread has different security credentials or requirements, you need to use separate proxy instances.

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

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

关于java - 这个 JAX-WS 客户端调用线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10599959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com