java Jersey 2.1 客户端线程安全吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18078666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is java Jersey 2.1 client thread safe?
提问by jakub.piasecki
Documentation for jersey 2.0 says:
Client instances are expensive resources. It is recommended a configured instance is reused for the creation of Web resources. The creation of Web resources, the building of requests and receiving of responses are guaranteed to be thread safe. Thus a Client instance and WebResource instances may be shared between multiple threads
客户端实例是昂贵的资源。建议重用配置的实例来创建 Web 资源。Web 资源的创建、请求的构建和响应的接收都保证是线程安全的。因此一个 Client 实例和 WebResource 实例可以在多个线程之间共享
Is client still thread-safe in version 2.1? I cannot find information about thread safety in docs for 2.1.
回答by Erik Pragt
I think, based on the 2.1 release notes, nothing has changed in that regard, but I cannot find the motivation for this change in documentation.
我认为,基于2.1 发行说明,在这方面没有任何变化,但我在文档中找不到这种变化的动机。
回答by Miroslav Fuksa
Yes, the Jersey 2.1 client is thread safe and it should be thread safe even in the future Jersey version. You can create many WebTarget from one Client instance and invoke many requests on these WebTargets and even more requests on one WebTarget instance in the same time.
是的,Jersey 2.1 客户端是线程安全的,即使在未来的 Jersey 版本中也应该是线程安全的。您可以从一个 Client 实例创建多个 WebTarget,并在这些 WebTarget 上调用多个请求,甚至在一个 WebTarget 实例上同时调用多个请求。
The thread safety can be broken if you register your custom non-thread safe provider into a Client or a WebTaget. For example a ClientRequestFilter that is not thread safe and cannot handle more requests simultaneously. Jersey built-in providers are thread safe. Some Jersey extension providers must not be thread safe and in this case this is specified in the javadoc of a provider.
如果您将自定义的非线程安全提供程序注册到客户端或 WebTaget 中,则线程安全可能会被破坏。例如 ClientRequestFilter 不是线程安全的并且不能同时处理更多请求。Jersey 内置提供程序是线程安全的。某些 Jersey 扩展提供程序不能是线程安全的,在这种情况下,这是在提供程序的 javadoc 中指定的。