zoukankan      html  css  js  c++  java
  • 各种超时归纳整理

    如果不设置超时,连接会一直占用本地线程,端口,连接客户端一多,阻塞在那里,会导致本地端口用尽及CPU压力

    netty

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

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

    关于netty connect异常,3个点要注意:1)connect可能直接抛出异常 2)要设置连接超时,否则网络不通一直卡着 3)connect不抛出异常,在回调中显示连接失败,netty(十五)负载均衡实践中代码处理了这1 3情况,并重连,不过好像忘了设置2

    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();
    

    jedis

    Jedis jedis = new Jedis(ip, port, 10000);
    
  • 相关阅读:
    图标字体化浅谈
    HTML5 data-* 自定义属性
    NodeJS初识
    [转]响应式web设计之CSS3 Media Queries
    [转]移动端web页面使用字体的思考
    运用@media实现网页自适应中的几个关键分辨率
    不同内核浏览器的差异以及浏览器渲染简介
    动画库tween.js
    ORACLE查看表空间使用率
    oracle存储过程游标循环
  • 原文地址:https://www.cnblogs.com/silyvin/p/12371545.html
Copyright © 2011-2022 走看看