2013-10-23 12 views

cevap

51

Aşağıdaki kod Jersey 2.3.1 benim için çalışıyor (ilham tespit edildi: https://stackoverflow.com/a/19541931/1617124)

public static void main(String[] args) { 
    Client client = ClientBuilder.newClient(); 

    client.property(ClientProperties.CONNECT_TIMEOUT, 1000); 
    client.property(ClientProperties.READ_TIMEOUT, 1000); 

    WebTarget target = client.target("http://1.2.3.4:8080"); 

    try { 
     String responseMsg = target.path("application.wadl").request().get(String.class); 
     System.out.println("responseMsg: " + responseMsg); 
    } catch (ProcessingException pe) { 
     pe.printStackTrace(); 
    } 
} 
+2

umarım bu işe şüpheliyim. .property (...) bir istemci örneği (oluşturucu modeli) döndürür. .target() öğesini çağırdığınızda ayarlar kullanılmaz. – mkuff

+4

Aslında işe yarıyor. Oluşturucu modeli başka bir örnek oluşturulmamalıdır demez. Sadece kaynak koduna bakın, dönüş değeri gerçek müşteri (sadece sonraki aramaları yapmak için daha kolay hale getirmek için). –

20

Ayrıca istek başına bir zaman aşımı belirtebilirsiniz:

public static void main(String[] args) { 
    Client client = ClientBuilder.newClient(); 
    WebTarget target = client.target("http://1.2.3.4:8080"); 

    // default timeout value for all requests 
    client.property(ClientProperties.CONNECT_TIMEOUT, 1000); 
    client.property(ClientProperties.READ_TIMEOUT, 1000); 

    try { 
     Invocation.Builder request = target.request(); 

     // overriden timeout value for this request 
     request.property(ClientProperties.CONNECT_TIMEOUT, 500); 
     request.property(ClientProperties.READ_TIMEOUT, 500); 

     String responseMsg = request.get(String.class); 
     System.out.println("responseMsg: " + responseMsg); 
    } catch (ProcessingException pe) { 
     pe.printStackTrace(); 
    } 
} 
İlgili konular