?? 第七章 join manager.htm
字號:
<html>
<head>
<title>新時代軟件教程:操作系統 主頁制作 服務器 設計軟件 網絡技術 編程語言 文字編輯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋體}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>第七章 Join Manager</strong></big></p>
<p>尋找一個lookup service包括一系列普通的步驟。他們隨后的交互也包括了一些一般的步驟。所以<BR>
我們用一個join manager把這些步驟封裝成一個有用的類。
<P>1、 Join Manager
<P>一個服務要使自己可用通常有以下策略:
<pre><code>
(1) 發現可用的服務定位器(service locators)。
(2) 注冊到找到的locator上(Register with all of them)
(3) 一直重新注冊(Keep re-registering till the end of time)
(4) 如果狀態改變了,通知給所有注冊服務的locators。
</code></pre>
這些代碼一旦寫完,就可以從一個服務拷貝到另一個。更好的做法是把他們封裝到一個對象中。甚至<BR>
Sun用一個類JoinManager來做它。它能很好的減少服務端的代碼量。例如option2的服務可以變成:
<pre><code>
package joinmgr;
// import net.jini.discovery.LookupDiscovery;
// import net.jini.discovery.DiscoveryListener;
// import net.jini.discovery.DiscoveryEvent;
// import net.jini.core.lookup.ServiceRegistrar;
// import net.jini.core.lookup.ServiceItem;
// import net.jini.core.lookup.ServiceRegistration;
// import net.jini.core.lease.Lease;
import com.sun.jini.lookup.JoinManager;
import net.jini.core.lookup.ServiceID;
import com.sun.jini.lookup.ServiceIDListener;
import com.sun.jini.lease.LeaseRenewalManager;
/**
* FileClassifierServer.java *
* Created: Wed Mar 17 14:23:44 1999
* @author Jan Newmarch
* @version
*/
public class FileClassifierServer implements ServiceIDListener {
public static void main(String argv[]) {
new FileClassifierServer();
}
public FileClassifierServer() {
/** WE JUNK ALL OF THIS CODE
LookupDiscovery discover = null;
try {
discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);
} catch(Exception e) {
System.err.println(e.toString());
System.exit(1);
}
discover.addDiscoveryListener(this);
// stay around long enough to receive replies
try {
Thread.currentThread().sleep(10000L);
} catch(java.lang.InterruptedException e) {
// do nothing
} */
/** AND REPLACE IT WITH THIS */
JoinManager joinMgr = null;
try {
joinMgr = new JoinManager(new FileClassifierImpl(), null,
this,new LeaseRenewalManager());
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
// stay around long enough to receive replies
try {
Thread.currentThread().sleep(1000000L);
} catch(java.lang.InterruptedException e) {
// do nothing
}
}
public void serviceIDNotify(ServiceID serviceID) {
System.out.println("got service ID " + serviceID.toString());
}
/*
public void discovered(DiscoveryEvent evt) {
ServiceRegistrar[] registrars = evt.getRegistrars();
for (int n = 0; n < registrars.length; n++) {
ServiceRegistrar registrar = registrars[n];
ServiceItem item = new ServiceItem(null, new FileClassifierImpl(), null);
ServiceRegistration reg = null; try {
reg = registrar.register(item, Lease.FOREVER);
} catch(java.rmi.RemoteException e) {
System.err.println("Register exception: " + e.toString());
}
System.out.println("service registered"); } }
public void discarded(DiscoveryEvent evt) { }
*/
} // FileClassifierServer
</code></pre>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -