简体   繁体   English

使用 javax.ws.rs.client.WebTarget 设置连接超时

[英]Set connection time out with javax.ws.rs.client.WebTarget

I have an URL for which I am trying to set connection time out for 10 seconds.我有一个 URL,我试图将连接超时设置为 10 秒。 Code snippet:-代码片段:-

WebTarget t=null;
Client c = ClientBuilder.newBuilder()
                .hostnameVerifier(<HostNameVerifier>)
                .sslContext(<SSLContext>)
                .build();
        client.register(<credentials for url>); 
        t = c.t(url);

From different sources, I have found the solution like below.从不同的来源,我找到了如下解决方案。

ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.READ_TIMEOUT, 2000);
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 500);

But both ClientConfig and ClientProperties is a utility of org.glassfish.jersey.client package. I have a limitation to use this.但是ClientConfigClientProperties都是org.glassfish.jersey.client package 的实用程序。我使用它有一个限制。

Can anyone suggest here how to set connection timeout without using ClientConfig and ClientProperties.谁能在这里建议如何在不使用 ClientConfig 和 ClientProperties 的情况下设置连接超时。

Any help will be appreciated.任何帮助将不胜感激。

Maybe this is just in newer versions of JAX-RS (it says 'since 2.1'), but now you can do this:也许这只是在较新版本的 JAX-RS 中(它说“从 2.1 开始”),但现在您可以这样做:

ClientBuilder.newBuilder()
    .connectTimeout(3, SECONDS)
    .readTimeout(1, SECONDS)
    .build()
    .target(...)

Just use String values instead of ClientProperties constants: 只需使用字符串值而不是ClientProperties常量即可:

        client.property("jersey.config.client.connectTimeout", 500);
        client.property("jersey.config.client.readTimeout", 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM