各种超时
Posted silyvin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各种超时相关的知识,希望对你有一定的参考价值。
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();
以上是关于各种超时的主要内容,如果未能解决你的问题,请参考以下文章
HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
转载HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法