RMI 連線逾時時間設定

來源:互聯網
上載者:User

標籤:

System.setProperty("sun.rmi.transport.tcp.responseTimeout", "2000");
System.setProperty("sun.rmi.transport.tcp.readTimeout", "2000");
System.setProperty("sun.rmi.transport.connectionTimeout", "2000");
System.setProperty("sun.rmi.transport.proxy.connectTimeout", "2000");
System.setProperty("sun.rmi.transport.tcp.handshakeTimeout", "2000");

 

我向如果問度娘也只能等到上面的內容了,但是當我們設定好了以後,也不好使,這個時候我們就懷疑是不是我們審定的值沒有影響當其中的內容。其實不然,裡面的資料已經被改寫。

做一下分析:

這裡最後可能應當連結逾時的是“sun.rmi.transport.proxy.connectTimeout”。其實SUN說明文檔中也是這麼提出的:

The value of this property represents the maximum length of time (in milliseconds) that the Java RMI runtime will wait for a connection attempt (createSocket) to complete, before attempting to contact the server using HTTP. This property is only used when the http.proxyHost property is set and the value of java.rmi.server.disableHttp is false. The default value is 15000 milliseconds (15 seconds).

但是這個時候我們需要看清“http.proxyHost ”和“java.rmi.server.disableHttp”這兩個屬性,“This property is only used when......”。

該屬性作用的類是:RMIMasterSocketFactory,有人會問我是怎麼知道的,這個嘛,我說經驗信嗎?“看”個玩笑了。在Socket中設定斷電,就可以看到其中的調用過程。

當時RMIMasterSocketFactory並沒有提供原始碼,也無法查到原始碼。當時我可以看到這個類中的屬性有connectTimeout,當然這個無法斷定connectTimeout對應的就是“sun.rmi.transport.proxy.connectTimeout”,

當時我們看不見原始碼,但是可以得到該類的位元組碼,在getConnectTimeout()方法中可以看到“ ldc <String "sun.rmi.transport.proxy.connectTimeout">”內容,表示它使用了“sun.rmi.transport.proxy.connectTimeout”個字串,還有static{}(“靜態構造方法”,作用是和對象的構造方法相同,區別是該方法值初始化靜態變數)方法中第1223,1224行(把位元組碼內容拷貝當編輯器中)

22 invokestatic sun.rmi.transport.proxy.RMIMasterSocketFactory.getConnectTimeout() : long [308]
25 putstatic sun.rmi.transport.proxy.RMIMasterSocketFactory.connectTimeout : long [259]

(invoke, static sun.rmi.transport.proxy.RMIMasterSocketFactory.getConnectTimeout(),long):執行RMIMasterSocketFactory中的靜態方法getConnectTimeout,傳回值是long型)
(put,static sun.rmi.transport.proxy.RMIMasterSocketFactory.connectTimeout, long):自悟吧。

這兩行表示執行getConnectTimeout()方法,對connectTimeout 靜態屬性進行賦值,(轉為原始碼就是 connectTimeout  = getConnectTimeout()),所以看得出在載入RMIMasterSocketFactory類的時候,就會擷取“sun.rmi.transport.proxy.connectTimeout”屬性中的值對connectTimeout 進行賦值,說以當程式在載入RMIMasterSocketFactory類前調用System.setProperty("sun.rmi.transport.proxy.connectTimeout", "2000");  就可以了。

System.setProperty("sun.rmi.transport.proxy.connectTimeout", "2000");
Naming.lookup( "rmi://xxx.xxx.xxx.xxx:xxxx/xxxxxxxx")

但是這樣還是不行的。因為上面文檔中說了。還有連個屬性沒有賦值了。

好吧,這條路我也算得上放棄了。

後來詳細,其實RMI底層通訊也是使用Socket的,在Socket中connect有連個實現,其中一個就是帶有逾時時間,所有這個時候我們就不實用Naming進行lookup,工具一個自己的Naming不就可以了嘛。

好吧,上面就當放屁了。下面才是正解

Naming.lookup("rmi://xxx.xxx.xxx.xxx:xxxx/xxxxxxxx");
  改為↓↓↓↓↓
Registry registry = LocateRegistry.getRegistry("xxx.xxx.xxx.xxx", xxxx, new RMIClientSocketFactory() {
@Override
public Socket createSocket(String host, int port) throws IOException {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), 2000);
return socket;
}
}); // registry最好是全域一個的(一個IP對應一個registry,也可以是多個,這個只是建議)
registry.lookup("xxxxxxxx");

好了。可以了。至於綁定對象還是用Naming.rebing把,沒有問題。的。。。。。

 

RMI 連線逾時時間設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.