您的位置:首页 > 其它

发布一个JINI服务

2011-03-11 15:11 113 查看
这一篇文章讲解如何发布一个JINI的服务。

(参考的书是:JINI EXAMPLE BY EXAMPLE)

1.提供一个服务接口

首先,任何JINI服务都需要一个服务接口,表示这个服务能做什么。我们这里使用的是一个货币转换的服务,例如输入数量(单位为美元)和国家,表示将美元转换为某个国家的货币。

这个接口的代码如下:

package example.chapter2;
public interface CurrencyConvertServiceInterface {
public float convert(float amount,String country);
}


这里有几个注意,在eclipse中编辑时,需要导入的2个外部库为:jini2_1/lib/jini-core.jar;jini2_1/lib/jini-ext.jar;

2.创建一个服务的代理

服务接口只是发布出来给客户看的,里面没有任何实现,有实现的在JINI里称为服务代理。服务代理需要实现serializable接口。

服务代理类的代码如下:

package example.chapter2;
import java.io.Serializable;
import java.util.Hashtable;
public class CurrencyConvertServiceProxy implements Serializable,
CurrencyConvertServiceInterface {
Hashtable<String,Float> exchangerate = new Hashtable<String,Float>();

public CurrencyConvertServiceProxy() {
super();
}

//initial the convert rate of country
public void initRates(){
exchangerate.put("UK", new Float(0.72));
exchangerate.put("Sweden", new Float(200));
}

//add a new rate
public void addRates(String country,float rate){
exchangerate.put(country, new Float(rate));
}

@Override
public float convert(float amount, String country) {
Float xchrate = exchangerate.get(country);
if (xchrate != null){
return amount*(xchrate.floatValue());
}else
return -1;
}
}


这里初始化了2个国家的转化货币的比率。

3.发布服务

package example.chapter2;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Hashtable;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceRegistration;
import net.jini.discovery.DiscoveryEvent;
import net.jini.discovery.DiscoveryListener;
import net.jini.discovery.LookupDiscoveryManager;

//wrapper class that handles publishing the service item.
public class CurrencyConvertService implements Runnable {
//10 minute lease
protected final int LEASE_TIME = 10*60*1000;
protected HashMap registrations = new HashMap();
protected ServiceItem item;
protected LookupDiscoveryManager discoverymanager;

class Listener implements DiscoveryListener{
//called only when we explicitly discard a lookup service.
@Override
public void discarded(DiscoveryEvent ev) {
ServiceRegistrar[] deadregs = ev.getRegistrars();
for (int i=0 ; i<deadregs.length ; i++){
registrations.remove(deadregs[i]);
}
}
//called when we find a new lookup service.
@Override
public void discovered(DiscoveryEvent ev) {
System.out.println("discovered a lookup service!......");
ServiceRegistrar[] newregs = ev.getRegistrars();
for (int i=0 ; i<newregs.length ; i++){
if(!registrations.containsKey(newregs[i])){
registerWithLookup(newregs[i]);
}
}
}
private synchronized void registerWithLookup(ServiceRegistrar registrar) {
ServiceRegistration registration = null;

try {
registration = registrar.register(item, LEASE_TIME);
} catch (RemoteException e) {
System.out.println("Could not register!");
e.printStackTrace();
return;
}

//if this is our first registration ,use the service ID return to US.
if(item.serviceID == null){
item.serviceID = registration.getServiceID();
System.out.println("set ServiceID to :" + item.serviceID);
}

registrations.put(registrar, registration);

}

}

public CurrencyConvertService() throws IOException {
//create an instance of the service proxy
//第一步:为此服务创建一个代理对象,此对象实现CurrencyConvertServiceInterface
CurrencyConvertServiceProxy proxyobj = createCurrencyProxy();
//create a service item to be added to the lookup service
//第二步:将此代理对象放入一个能添加到lookup服务中的ServiceItem中
item = new ServiceItem(null, proxyobj, null);
//set a security manager.
//第三步:配置一个安全管理器
if(System.getSecurityManager() == null){
System.setSecurityManager(new RMISecurityManager());
}
//create a discovery Listener
DiscoveryListener myListener = new Listener();

//Search for the public group,which is named by the empty String.
//第四步:查找一个lookup服务,使用空字符串来查找一个公共的共同体
LookupLocator look = new LookupLocator("jini://zz439");
discoverymanager = new LookupDiscoveryManager(new String[]{""}, new LookupLocator[]{look}, myListener);
}

private CurrencyConvertServiceProxy createCurrencyProxy() {
CurrencyConvertServiceProxy proxyobj = new CurrencyConvertServiceProxy();
proxyobj.initRates();
return proxyobj;
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Started .....");
try {
CurrencyConvertService hws = new CurrencyConvertService();
new Thread(hws).start();
} catch (IOException e) {
System.out.println("cann't create service....");
e.printStackTrace();
}

}

@Override
public void run() {
while(true){
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


发布服务需要首先发现一个查找服务。在JINI里用LookupDiscoveryManager类来进行寻找一个发现服务。

寻找到发现服务后,用一个Listener来进行工作。

当找到发现服务后,Listener调用discovery方法,在discovery方法里,进行服务的注册。

服务注册使用类初始化的时候建立的服务代理的对象。

4.测试

进入源码下的bin目录,执行代码如下:

C:/Documents and Settings/Administrator/workspace/JiniExample1/bin>java -cp D:/j

ini2_1/lib/jini-core.jar;D:/jini2_1/lib/jini-ext.jar;D:/jini2_1/lib/sun-util.jar

;.; -Djava.security.policy=C:/policy -Djava.rmi.server.codebase=http://zz439:808

1/ example.chapter2.CurrencyConvertService

Started .....

discovered a lookup service!......

set ServiceID to :2e68d56d-cebb-4d61-bcd8-bc99ce09a039

可以看到,服务已经被注册上了,而且在窗口界面上也可以看到一个新的服务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: