⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 第四章 服务注册.htm

📁 分多章节详细介绍了Jini技术及相关开发环境
💻 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>第四章 服务注册</strong></big></p>

<p>一个服务要在Lookup Server上注册自己,要用类ServiceRegistrar的方法register():
<pre><code>
package net.jini.core.lookup;

public Class ServiceRegistrar {
    public ServiceRegistration register(ServiceItem item,
                                       long leaseDuration)
                               throws java.rmi.RemoteException;
}</code></pre>

第二个参数要求lookup service保持该注册服务的时间。该请求并不被认同(honored):lookup service可以完全拒绝,也可以只是允许比这短的时间。第一个参数是类型
<pre><code>
 
   package net.jini.core.lookup;
   public Class ServiceItem {
     public ServiceID serviceID;
     public java.lang.Object service;
     public Entry[] attributeSets;    
     public ServiceItem(ServiceID serviceID,
                        java.lang.Object service,
                        Entry[] attrSets);
   }</code></pre>

<P>1、ServiceItem

    服务将使用构造器生成该类的对象。当服务第一次注册时,其中的serviceID设为null。当lookup service注册服务时,它将重新设一个非null值。在后来的注册或重新注册时,就使用该非null值。
    在后来的注册或重新注册时,就使用该非null值。  
<P>第二个参数是被注册的服务对象。该对象将被序列化并送到service locator储存。以后当客户要求服务时,该对象就会被传给客户。为了避免原始服务和被送到lookup service上的服务对象混淆,我应该使用术语输出服务对象(exported service object),尽管该术语在正式的Jini文档中没有使用。关于输出服务对象有几点要注意的:
<P>&nbsp;&nbsp;&nbsp; 对象必须被序列化。有些对象例如Swing's JtextArea目前不能被序列化,所以不能用。
<P>&nbsp;&nbsp;&nbsp; 对象在服务的JVM中生成。而且当它运行时在客户的JVM。对于实际的服务可能需要一个代理。
<P>第三个参数是一组描述该服务附加信息的Entry对象。如果没有附加的信息,该参数为null。

<P>2.Registration
     
服务调用register()来注册本身。这要抛出一个必须捕捉的例外java.rmi.RemoteException。
返回值是类型ServiceRegistration
<P>3.ServiceRegistration

该对象有service locator建立,通过RMI返回到service端运行。它扮演的是一个代理对象,该对象控制在lookup service上运行的输出服务对象的状态。该对象维持一个serviceID,它是用来标志lookup service上的输出服务对象。服务(service)能通过getServiceID()得到它来重用。
其他方法如下:
<pre><code>
   void addAttributes(Entry[] attrSets);
   void modifyAttributes(Entry[] attrSetTemplates, Entry[] attrSets);
   void setAttributes(Entry[] attrSets);
</code></pre>
这些方法能够用来改变存在lookup service上的属性(attributes)

服务一直处于等待状态,如果输出服务对象能完成服务的全部任务,该实例就结束;如果输出服务对象担当代理,
并且需要和服务通信,它就休眠,以便能继续存在。如果当时间到了的时候,服务需要继续存在,就要重新注册。
下面这个程序中的服务什么都没做: 
<pre><code>
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceRegistration;
import java.io.Serializable;
/**
     *SimpleService.java 
     * 
     *
     * Created: Fri Mar 12 22:34:53 1999 *
     * @author Jan Newmarch 
     *@version 
     */
public class SimpleService implements Serializable {    
    static public void main(String argv[]) { 
       new SimpleService(); 
    }   
 
   public SimpleService() {
     	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);
	}
	// register ourselves as service, with no serviceID
	// or set of attributes	
    ServiceItem item = new ServiceItem(null, this, null);
	ServiceRegistration reg = null;
	try {
	    reg = registrar.register(item, 10000000L);
	} catch(java.rmi.RemoteException e) {
	    System.err.println("Register exception: " + e.toString());
	}
	// we can exit here if the exported service object can do
	// everything, or we can sleep if it needs to communicate 
	// to us or we need to re-register later
     }
   } // SimpleService
</code></pre>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -