- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在以下UserResource
类中:
public class UserResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/users")
public void createUser() {
//JerseyClient is needed to send a REST request to another RESTful service
}
}
JerseyClient
需要将请求发送到评论中提到的另一个 RESTful API。
由于可能有多个线程同时调用此 UserResource
,因此 JerseyClient
可能应该在每个线程的每次调用中初始化,以确保线程安全。然而,JAX-RS Client
提到过
Clients are heavy-weight objects that manage the client-side communication infrastructure. Initialization as well as disposal of a Client instance may be a rather expensive operation. It is therefore advised to construct only a small number of Client instances in the application. Client instances must be properly closed before being disposed to avoid leaking resources.
根据文档,在createUser
主体内初始化JerseyClient
可能会昂贵,从而带来性能问题。
问题:如何以线程安全的方式高效优化JerseyClient
实例的数量?
最佳答案
如果将 Client
实例化为类字段就足够了,因为 Client
类是线程安全的。
Client instances are expensive resources. It is recommended a configured instance is reused for thecreation of Web resources. The creation of Web resources, the building of requests and receiving ofresponses are guaranteed to be thread safe. Thus a Client instance and WebResource instances maybe shared between multiple threads.
关于java - 如何以线程安全的方式在 JAX-RS 资源中使用 JerseyClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58225041/
我是一名优秀的程序员,十分优秀!