1

我使用 Guice 依赖注入将 javax.ws.rs.client.Client 作为单例,并且我的线程池中有多个线程共享单例对象。

@Provides
@Singleton
protected Client provideRestClient() {
    log.info("Http client instance created");
    ClientConfig cfg = new ClientConfig();
    cfg.property(ClientProperties.CONNECT_TIMEOUT, 5000);
    cfg.property(ClientProperties.READ_TIMEOUT, 5000);
    final Client client = ClientBuilder.newClient(cfg);
    return client;
}

当多个线程在几秒钟内使用单例客户端对象查询同一个端点时,第二个线程得到空响应。这与休息客户端缓存响应数据有关吗?

跨线程使用客户端单例对象的代码

final Response response = client.target("URL").request().get();
if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
    String response = response.readEntity(String.class);
}

任何帮助,将不胜感激

4

0 回答 0