简体   繁体   English

我可以设置默认的全局Java套接字超时吗?

[英]Can I set the default global Java Socket Timeout?

I am running into a problem where various places in my application involving http and ftp connections are hanging indefinitely (observed for hours at a time before killing the java process) on java.net.SocketInputStream.socketRead0. 我遇到一个问题,我的应用程序中涉及http和ftp连接的各个位置在java.net.SocketInputStream.socketRead0上无限期挂起(在杀死Java进程之前一次观察了几个小时)。

I'd like to override the default socket timeout of 0 globally so that I don't have to set it in every single location where I initiate a connection. 我想全局覆盖默认的套接字超时0,这样就不必在启动连接的每个位置都设置它。 Is there some global Java property or method I can call to set the default socket timeout to something other than 0? 我可以调用一些全局Java属性或方法来将默认套接字超时设置为0以外的值吗?

It depends on how your application creates sockets. 这取决于您的应用程序如何创建套接字。 If your application code uses URLConnection then you can set default timeout using JVM properties: 如果您的应用程序代码使用URLConnection,则可以使用JVM属性设置默认超时:

-Dsun.net.client.defaultReadTimeout=30000
-Dsun.net.client.defaultConnectTimeout=5000

This properties are implementation specific but still works for latest Oracle/OpenJDK. 此属性是特定于实现的,但仍适用于最新的Oracle / OpenJDK。

https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html https://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html

It depends upon how you wrote the code. 这取决于您如何编写代码。 From my perspective, you should have created an APIClient which takes care of making the http/ftp calls and returns the response to the caller function. 从我的角度来看,您应该已经创建了一个APIClient,它负责进行http / ftp调用,并将响应返回给调用者函数。 That is, every http/ftp call has to go through this APIClient file. 也就是说,每个http / ftp调用都必须通过此APIClient文件。 Its good to have a class parameter/constant which stores the timeout in ms and then use this constant as timeout while making a http/ftp call. 最好有一个类参数/常量,以毫秒为单位存储超时,然后在进行http / ftp调用时将此常量用作超时。

public abstract class WebServiceAPIClient {
    protected String authUser;
    protected String authPassword;
    // Set the connection timeout whatever you want...
    final Integer connectionTimeOut = 60000;

    public WebServiceAPIClient(String user, String pwd, int errorCode) {
        this.authUser = user;
        this.authPassword = pwd;
        this.errorIdentiferCode = errorCode;
    }

    public String requestRESTGet(String url, List<Integer> successCodes, boolean addBasicAuth) throws CloudWebServiceInvocationException {
    Long start = System.currentTimeMillis();
    HttpGet get = new HttpGet(url);
    DefaultHttpClient client = getHttpClient();
    try {
        if (addBasicAuth)
            addHeaders(get);
        HttpResponse response = client.execute(get);
        return responseAsString(response);
    } catch (ClientProtocolException e) {
        internalServerError(e);
    } catch (IOException e) {
        internalServerError(e);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return "";
 }

  protected DefaultHttpClient getHttpClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    if (this.connectionTimeOut != null) {
        HttpConnectionParams.setConnectionTimeout(params, this.connectionTimeOut);
        HttpConnectionParams.setSoTimeout(params, 3 * this.connectionTimeOut);
    }
    return client;
}

} }

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

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