如何使用 Jersey 2.x 设置连接和读取超时? [英] How to set the connection and read timeout with Jersey 2.x?

查看:23
本文介绍了如何使用 Jersey 2.x 设置连接和读取超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在球衣 1 中,我们有一个函数 setConnectTimeout.

In jersey 1 we had a function setConnectTimeout in the class com.sun.jersey.api.client.Client.

在 jersey 2 中,javax.ws.rs.client.Client 类用于缺少此功能的地方.

In jersey 2 the javax.ws.rs.client.Client class is used where this function is missing.

jersey 2.x中如何设置连接超时和读取超时?

How to set connection timeout and read timeout in jersey 2.x?

推荐答案

下面的代码在 Jersey 2.3.1 中适用于我(灵感在这里找到:https://stackoverflow.com/a/19541931/1617124)

The code below works for me in Jersey 2.3.1 (inspiration found here: 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();
    }
}

这篇关于如何使用 Jersey 2.x 设置连接和读取超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆