2013-10-23 33 views
8

com.sun.jersey.api.client.Clientjavax.ws.rs.client.Client如何配置客戶端?javax.ws.rs.client.Client如何配置readTimeOut?

FROM:

import com.sun.jersey.api.client.Client; 

Client client = Client.create(); 
client.setReadTimeout(1000 * 60 * 20); 
client.setConnectTimeout(1000 * 20); 
webResource = client.resource("someWhereOverTheRainbow"); 
..etc. 

TO:

import javax.ws.rs.client.*; 

Client client = ClientBuilder.newClient(); 
// **now what?** client.getConfiguration().getProperties().put("isThisTheWayToDoIt", 1000 * 60 * 2); 

WebTarget target = client.target("someWhereOverTheRainbow"); 
..etc. 

我使用javax.ws.rs-api-2.0.jar

回答

15

我假設你正在使用JAX-RS裏。爲此,您可以使用ClientProperties.CONNECT_TIMEOUTClientProperties.READ_TIMEOUT

實施例:

ClientConfig configuration = new ClientConfig(); 
configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000); 
configuration = configuration.property(ClientProperties.READ_TIMEOUT, 1000); 
Client client = ClientBuilder.newClient(configuration); 
WebTarget target = client.target(
     "http://developer.github.com/v3/"); 
String content = target.request().get(String.class); 
System.out.println(content); 

編輯:

我讀API文檔ClientConfig.property。而@吉利是對的。

+1

'ClientConfig'可能是不可變的。因此,'configuration.property()'不保證返回'this'。您有責任將結果返回給「配置」,否則您有可能丟失財產。 – Gili

+0

@Gili非常感謝您指出我的錯誤。 – longhua

+2

是ClientConfig和ClientProperties沒有球衣特定的類嗎?我認爲問題是通過JAX-RS API –