第二章 定位Lookup Service
客户通过查询某个Lookup Service来定位服务。为了实现这一点,首先必须找到lookup service。另一方面,一个服务要在lookup service上注册,首先也要找到lookup service。所以客户和服务的第一步都是要查找lookup service。查找lookup service有两种方法: unicast(唯一)和broadcast(广播).
1、一对一查找(Unicast discovery)
当你已经知道了lookup
service所在的机器,就可以用一对一的查找。一般用在你知道lookup
service的准确地址。编程使用包net.jini.core.discovery中的类LookupLocator ,它有两个构造函数如下:
package net.jini.core.discovery;
public Class LookupLocator {
LookupLocator(java.lang.String url)
throws java.net.MalformedURLException;
LookupLocator(java.lang.String host,int port);
}
第一个构造函数中的URL必须是这种形式的 "jini://host/" 或者
"jini://host:port/"。如果不指定端口号(port),缺省端口是4160。下面的程序有一些有效或无效的host/URLs生成对象。
import net.jini.core.discovery.LookupLocator;
/**
* InvalidLookupLocator.java * *
* Created: Tue Mar 9 14:14:06 1999
* @author Jan Newmarch
* @version
*/
public class InvalidLookupLocator {
static public void main(String argv[]) {
new InvalidLookupLocator();
}
public InvalidLookupLocator() {
LookupLocator lookup; // this is valid
try {
lookup = new LookupLocator("jini://localhost");
System.out.println("First lookup creation succeeded");
} catch(java.net.MalformedURLException e) {
System.err.println("First lookup failed: " + e.toString()); }
// this is probably an invalid URL,
// but the URL is syntactically okay
try {
lookup = new LookupLocator("jini://ABCDEFG.org");
System.out.println("Second lookup creation succeeded");
} catch(java.net.MalformedURLException e) {
System.err.println("Second lookup failed: " + e.toString()); }
// this IS a malformed URL
try {
lookup = new LookupLocator("A:B:C://ABCDEFG.org");
System.out.println("Third lookup creation succeeded");
} catch(java.net.MalformedURLException e) {
System.err.println("Third lookup failed: " + e.toString()); }
// this is valid
lookup = new LookupLocator("localhost", 80);
System.out.println("Fourth lookup creation succeeded");
}
} // InvalidLookupLocator
查找是通过类LookupLocator的方法getRegistrar()来实现的,该方法返回类
ServiceRegistrar的一个对象实例。
public ServiceRegistrar getRegistrar()
throws java.io.IOException,
java.lang.ClassNotFoundException
程序实例如下:
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
/**
*UnicastRegistrar.java
* *
*Created: Fri Mar 12 22:34:53 1999
*
*@author Jan Newmarch
*@version
*/
public class UnicastRegistrar {
static public void main(String argv[])
{
new UnicastRegistrar(); }
public UnicastRegistrar() {
LookupLocator lookup = null;
ServiceRegistrar registrar = null;
try {
lookup = new LookupLocator("jini://localhost");
} catch(java.net.MalformedURLException e) {
System.err.println("Lookup failed: " + e.toString());
System.exit(1);
}
try { registrar = lookup.getRegistrar();
} catch (java.io.IOException e) {
System.err.println("Registrar search failed: " + e.toString());
System.exit(1);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("Registrar search failed: " + e.toString());
System.exit(1); }
// the code takes separate routes from here for client or service }
} // UnicastRegistrar
2、广播式查找(Broadcast discovery)
如果lookup
service的位置不知道,那就要用广播式查找了。我们要使用在包net.jini.discovery 中的类LookupDiscovery
。它只有一个构造函数如下:
LookupDiscovery(java.lang.String[] groups)
该构造函数的参数可以有三种不同的 情况:2.1 DiscoveryListener
广播式查找是在网上全面搜索,网上能收到信息的Lookup
Service都应该响应请求。这种查找是很费时间的,因为一般不知道能响应的lookup service的数目。为了处理这种不确定性,对象
LookupDiscovery可以注册一个监听器来监听响应信息。
public void addDiscoveryListener(DiscoveryListener l)
该监听器必须实现接口DiscoveryListener
package net.jini.discovery;public
abstract interface DiscoveryListener {
public void discovered(DiscoveryEvent e);
public void discarded(DiscoveryEvent e);
}
不论什么时候,lookup
service一被发现就调用方法discovered()。API建议该方法应该立刻返回,不要做任何远程调用。
而且对于服务方它可以很方便的注册服务,而客户也可以用它很方便的查找可用的服务,并调用该服务。该操作最好用一个单独的线程来执行。其他问题包括:DiscoveryListener一建立,广播就开始了。然后一个监听器就加入该discovery对象中。如果在listener加入之前,有了响应会发生什么情况?规范保证这些响应会被存起来不会丢失。相反,如果长时间没有响应来怎么办?这不能简单的退出,
只能等到有响应才行。处理方法只一,如果应用有GUI界面,可以由用户停止;另一种方法是sleep 1000秒。当lookup
service放弃时,调用方法discarded()。
2.2 DiscoveryEvent
方法discover()的参数是DiscoveryEvent对象。
package net.jini.discovery;
public Class DiscoveryEvent {
public net.jini.core.lookup.ServiceRegistrar[] getRegistrars();
}
它有一个公共方法getRegistrars(),该方法返回ServiceRegistrar对象数组。程序实例如下:
import net.jini.discovery.LookupDiscovery;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.DiscoveryEvent;
import net.jini.core.lookup.ServiceRegistrar;
/** * MulticastRegistrar.java * *
*Created: Fri Mar 12 22:49:33 1999
* @author Jan Newmarch
* @version
*/
public class MulticastRegistrar implements DiscoveryListener {
static public void main(String argv[]) {
new MulticastRegistrar();
}
public MulticastRegistrar() {
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(1000000L);
} catch(java.lang.InterruptedException e) {
// do nothing
}
}
public void discovered(DiscoveryEvent evt) {
ServiceRegistrar[] registrars = evt.getRegistrars();
for (int n = 0; n < registrars.length; n++) {
ServiceRegistrar registrar = registrars[n];
// the code takes separate routes from here for client or service }
}
public void discarded(DiscoveryEvent evt) { }
}// MulticastRegistrar
3、ServiceRegistrar
ServiceRegistrar是一个被每一个lookup
service实现的抽象类。该类实际实现的细节和这没有关系。类ServiceRegistrar的作用是担当lookup
service的代理。该代理运行在客户或服务中。在Jini中,这是第一个从一个Java进程移到另一个进程中的对象。使用RMI,该对象被从lookup
service移到找到该lookup
service的应用中。从那时起,它就做为应用地址空间中的一个对象运行,同时该应用对它进行正常的方法调用。当需要的时候,它可以和它的lookup
service通信,但是这种通信不需要应用显式的调用RMI方法。该对象有两个主要的方法,其中一个是服务用来注册的:
public ServiceRegistration register(ServiceItem item,
long leaseDuration)
throws java.rmi.RemoteException
另一个是客户用来定位某个特定的服务:
public java.lang.Object lookup(ServiceTemplate tmpl)
throws java.rmi.RemoteException;
public ServiceMatches lookup(ServiceTemplate tmpl,
int maxMatches)
throws java.rmi.RemoteException;
一个服务要注册一个对象(就是一个类的实例),和该对象的属性集合。例如,打印机可以指定能否处理Postscript文档。服务可以注册本身,或注册一个实现其行为的代理。注意:已注册的对象可以通过RMI传递。最后当它运行时,可以离它开始建立的地方很远。客户使用所知道的一些服务特性来寻找服务。