这几天在开发过程中,启动项目会出现一个Timeout的异常
java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:525) at sun.net.NetworkClient.doConnect(NetworkClient.java:158) at sun.net.www.http.HttpClient.openServer(HttpClient.java:394) at sun.net.www.http.HttpClient.openServer(HttpClient.java:529) at sun.net.www.http.HttpClient.<init>(HttpClient.java:233) at sun.net.www.http.HttpClient.New(HttpClient.java:306) at sun.net.www.http.HttpClient.New(HttpClient.java:323) at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860) at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801) at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049) at net.sf.ehcache.util.UpdateChecker.getUpdateProperties(UpdateChecker.java:108) at net.sf.ehcache.util.UpdateChecker.doCheck(UpdateChecker.java:72) at net.sf.ehcache.util.UpdateChecker.checkForUpdate(UpdateChecker.java:60) at net.sf.ehcache.util.UpdateChecker.run(UpdateChecker.java:51) at java.util.TimerThread.mainLoop(Timer.java:512) at java.util.TimerThread.run(Timer.java:462)
从异常名称(SocketTimeoutException)来看是一个socket连接超时,应该是连接其他资源出现的异常。
接着看堆栈的信息,发现有sun.net.www.http.HttpClient,定位这是一个http请求。但是程序在启动的时候并没有去连接任何外部的http资源啊?奇怪了~继续向下看看会不会有新的发现吧。
果然有发现,ehcache,可爱的缓存怎么还会去请求http资源呢?程序使用RMI方式做ehcache集群配置,不应该会有http请求啊,先不想了,跟到源码里看一下吧。
run->checkForUpdate->doCheck都没什么内容,跟着逻辑就进到了net.sf.ehcache.util.UpdateChecker.getUpdateProperties(其实这个类名已经说明这是在做更新检查了,我竟然忽略的看类名)
private Properties getUpdateProperties(URL updateUrl) throws IOException { URLConnection connection = updateUrl.openConnection(); connection.setConnectTimeout(CONNECT_TIMEOUT); InputStream in = connection.getInputStream(); try { Properties props = new Properties(); props.load(connection.getInputStream()); return props; } finally { if (in != null) { in.close(); } } }
扔断点,启动调试观察updateUrl(我竟然还没意识到这是在检测更新)
http://www.terracotta.org/kit/reflector?pageID=update.properties&kitID=ehcache.default&id=-1062731420&os-name=Windows+7&jvm-name=Java+HotSpot%28TM%29+Client+VM&jvm-version=1.6.0_18&platform=x86&tc-version=2.6.3&tc-product=Ehcache+Core+2.6.3&source=Ehcache+Core&uptime-secs=1&patch=UNKNOWN
这么长的地址,但是看这个域名挺眼熟,好像是......没再想了,先把地方放到浏览器上看看是什么
## Top level keys # general.notice = This notice should rarely, if ever, be used as everyone running ehcache will see it ## 2.6.5 -- latest release 2.6.5.updates = 2.6.5.notices = This is the latest GA release. 2.6.5.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.6 ## 2.6.3 2.6.3.updates = 2.6.5 2.6.3.notices = This is the latest GA release. 2.6.3.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.6 ## 2.6.2 2.6.2.updates = 2.6.5 2.6.2.notices = We recommend upgrading to 2.6.5, our latest GA release 2.6.2.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.6 ## 2.6.0 2.6.0.updates = 2.6.5 2.6.0.notices = We recommend upgrading to 2.6.5, our latest GA release 2.6.0.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.5 ## 2.5.7 2.5.7.updates = 2.6.5 2.5.7.notices = We recommend upgrading to 2.6.5, our latest GA release 2.5.7.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.5 2.5.6-SNAPSHOT.updates = 2.6.5 ## 2.5.6 2.5.6.updates = 2.6.5 2.5.6.notices = We recommend upgrading to 2.6.5, our latest GA release 2.5.6.release-notes = http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.5
这是什么东西?好像是版本更新~版本更新?版本更新!竟然是版本更新!打开ehcache.xml检查一下
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
竟然真的启动了检查更新,折腾了一大圈,竟然是ehcache在检查版本更新,可能是我浏览器挂了goagent的原因,在浏览器里访问时正常的,但是在程序中确无法访问,所以就出现了Timeout的异常。
把updateCheck改成false就解决了。(写了这么一大堆就最后一句话有用。^_^)