各种超时归纳整理
如果不设置超时,连接会一直占用本地线程,端口,连接客户端一多,阻塞在那里,会导致本地端口用尽及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秒。
https://www.javadoc.io/doc/org.apache.httpcomponents/httpclient/4.5.5/org/apache/http/client/config/RequestConfig.html
Defines the socket timeout (SO_TIMEOUT
) in milliseconds, which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets). 不包括把数据从内核拷贝时间
java URL
HttpURLConnection con = (HttpURLConnection) object.openConnection(); con.setConnectTimeout(); con.setReadTimeout();
jedis
Jedis jedis = new Jedis(ip, port, 10000);
=============================================================================
https://zhuanlan.zhihu.com/p/535405145