码迷,mamicode.com
首页 > 其他好文 > 详细

各种超时

时间:2020-02-27 13:08:14      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:highlight   exception   enable   阻塞   jdk   man   ram   work   port   

netty

            /**
             * *******************************************************************
             * 如果不设置超时,连接会一直占用本地线程,端口,连接客户端一多,阻塞在那里,会导致本地端口用尽及CPU压力
             */
            bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);

netty client 连接超时设置,如果是127.0.0.1,则会直接抛连接异常,由于192。*网络不通,故到时抛超时异常

 

socket

Socket socket = new Socket(); 
socket.connect(new InetSocketAddress(ipAddress, port), 1000);//设置连接请求超时时间1s socket.setSoTimeout(5000);//设置读操作超时时间5s

 

/**
     *  Enable/disable {@link SocketOptions#SO_TIMEOUT SO_TIMEOUT}
     *  with the specified timeout, in milliseconds. With this option set
     *  to a non-zero timeout, a read() call on the InputStream associated with
     *  this Socket will block for only this amount of time.  If the timeout
     *  expires, a <B>java.net.SocketTimeoutException</B> is raised, though the
     *  Socket is still valid. The option <B>must</B> be enabled
     *  prior to entering the blocking operation to have effect. The
     *  timeout must be {@code > 0}.
     *  A timeout of zero is interpreted as an infinite timeout.
     *
     * @param timeout the specified timeout, in milliseconds.
     * @exception SocketException if there is an error
     * in the underlying protocol, such as a TCP error.
     * @since   JDK 1.1
     * @see #getSoTimeout()
     */
    public synchronized void setSoTimeout(int timeout) throws SocketException {
        if (isClosed())
            throw new SocketException("Socket is closed");
        if (timeout < 0)
          throw new IllegalArgumentException("timeout can‘t be negative");

        getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
    }

 

a read() call on the InputStream associated with
     *  this Socket will block for only this amount of time.  If the timeout
     *  expires, a <B>java.net.SocketTimeoutException</B> is raised, 

 

jdbc

DriverManager.setLoginTimeout(5);

work log  ,文中的连接url用了xxx,是真的xxx哦,然后到时间爆了超时而不是直接连接异常抛出,jdbc是jconn3-6.0.jar,sybase

/**
     * Sets the maximum time in seconds that a driver will wait
     * while attempting to connect to a database once the driver has
     * been identified.
     *
     * @param seconds the login time limit in seconds; zero means there is no limit
     * @see #getLoginTimeout
     */
    public static void setLoginTimeout(int seconds) {
        loginTimeout = seconds;
    }

 

 

httpclient

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(3000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);

 ConnectionPoolTimeoutException 获取连接池连接超时  

ConnectionTimeout:这定义了通过网络与服务器建立连接的超时时间。Httpclient包中通过一个异步线程去创建与服务器的socket连接,这就是该socket连接的超时时间,此处设置为3秒。  将url改为一个不存在的url,则会抛出org.apache.commons.httpclient.ConnectTimeoutException  

SocketTimeout:这定义了Socket读数据的超时时间,即从服务器获取响应数据需要等待的时间,此处设置为5秒。

 

java URL

            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setConnectTimeout();
            con.setReadTimeout();

 

各种超时

标签:highlight   exception   enable   阻塞   jdk   man   ram   work   port   

原文地址:https://www.cnblogs.com/silyvin/p/12371545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!