用RMI和CORBA开发JAVA分布式程序(二)

来源:互联网 发布:靠谱前程网络 编辑:程序博客网 时间:2024/05/16 04:50
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

CORBA

 

CORBAOMG组织针对企业应用上的分布式程序开发标准。重要的一点是CORBA仅仅是个规范。CORBA应用以ORB而知名。市场上已经出现了几个CORBA产品如VISIBROKEORBIX等。JAVAIDL是在JDK13及以上版本核心包的另一个应用。

CORBA设计的与平台和语言无关(注:与WEB SERVICE类似),因此CORBA能运行在任何平台上,能应用在任何网络里,能用任何支持IDL接口的语言编写。

 

RMI类似,CORBA对象也需要用接口描述说明。但是CORBA接口需要用一种和C++类似的IDL来描述,需要指出的是IDL不是一种程序语言。

 

CORBA应用起步

 

开发CORBA应用程序主要有以下几个步骤:

1 定义IDL接口

2 IDL转换成JAVA

3 应用接口

4 开发服务器端

5 开发客护端

6 运行名称服务器,服务器和客户程序

 

我们现在一步一步地讲述用CORBA开发文件传输的应用,和RMI相似。我们将用到JAVAIDL,在JDK13包里有。

 

定义接口

 

 定义接口的时候,我们需要考虑服务器支持的操作类型。在文件传输应用里,需要用到一个下载文件的方法。例5有了这个接口FileInterface。客户端将调用这个方法下载文件。

IDL里的sequence和数组比较接近,但它不是个固定的长度。

注意方法downloadFileString 参数类型有三种传输模式:in(从客户端到服务器),out(从服务器端到客户端)inout(双向)

 

Code Sample 5: FileInterface.idl

interface FileInterface {
   typedef sequence<octet> Data;
   Data downloadFile(in string fileName);
};

 

接口定义完成后,你可以用JDK13带的IDLJ编译器编译生成JAVA

Idlj编译器带有一些参数,如-f参数 可以生成你指定的客护端代码或服务端骨干码,或两者。在这个例子里,我们将在两台机器里,所以我们用-fserver,-fclient生成服务器端,客户端JAVA代码。

下面我们编译接口生成服务器端代码,用下面命令:

prompt> idlj -fserver FileInterface.idl

这个命令将生成Skeleton,holder,helper和其它类。生成类_FileInterfaceImplBase,我们将利用这个类的接口来实现应用。

 

应用接口

 

下面我们将实现下载方法。

Code Sample 6: FileServant.JAVA

import JAVA.io.*; 
 
public class FileServant extends _FileInterfaceImplBase {
   public byte[] downloadFile(String fileName){
      File file = new File(fileName);
      byte buffer[] = new byte[(int)file.length()];
      try {
         BufferedInputStream input = new
           BufferedInputStream(new FileInputStream(fileName));
         input.read(buffer,0,buffer.length);
         input.close();
      } catch(Exception e) {
         System.out.println("FileServant Error: "+e.getMessage());
         e.printStackTrace();
      }
      return(buffer); 
   }
}

 

服务器端开发

 

下一步我们开发服务器端。包括以下几个步骤:

1 初始化ORB

2 创建一个FileServant对象

3 CORBA名称服务里注册对象名

4 打印一个状态信息

5 等待客户端请求

 

Code Sample 7: FileServer.JAVA

import JAVA.io.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
 
public class FileServer {
   public static void main(String args[]) {
      try{
         // create and initialize the ORB
         ORB orb = ORB.init(args, null);
         // create the servant and register it with the ORB
         FileServant fileRef = new FileServant();
         orb.connect(fileRef);
         // get the root naming context
         org.omg.CORBA.Object objRef =
            orb.resolve_initial_references("NameService");
         NamingContext ncRef = NamingContextHelper.narrow(objRef);
         // Bind the object reference in naming
         NameComponent nc = new NameComponent("FileTransfer", " ");
         NameComponent path[] = {nc};
         ncRef.rebind(path, fileRef);
         System.out.println("Server started....");
         // Wait for invocations from clients
         JAVA.lang.Object sync = new JAVA.lang.Object();
         synchronized(sync){
            sync.wait();
         }
      } catch(Exception e) {
         System.err.println("ERROR: " + e.getMessage());
         e.printStackTrace(System.out);
      }
   }

}

 

一旦FileServer有了ORB(对象请求代理),它就能注册CORBA服务。它用OMG制定的COS命名服务注册。它从命名服务根开始,返回一个生成的CORBA对象。为了引用

NamingContext对象,必须narrowed down一个合适的类型。如下做:

NamingContext ncRef = NamingContextHelper.narrow(objRef);

对象ncRef object is 现在就是 org.omg.CosNaming.NamingContext 的实例.你可以用它来注册一个代用绑定方法明明服务的CORBA服务。

开发客户端

 

下一步是开发一个客户端应用,获得命名服务,访问和查找别的服务。当得到FileTransfer服务后,就可以调用下载方法了。

 

Code Sample 8: FileClient

import JAVA.io.*;
import JAVA.util.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
 
public class FileClient {
   public static void main(String argv[]) {
      try {
         // create and initialize the ORB
         ORB orb = ORB.init(argv, null);
         // get the root naming context
         org.omg.CORBA.Object objRef =
            orb.resolve_initial_references("NameService");
         NamingContext ncRef = NamingContextHelper.narrow(objRef);
         NameComponent nc = new NameComponent("FileTransfer", " ");      
         // Resolve the object reference in naming
         NameComponent path[] = {nc};
         FileInterfaceOperations fileRef =
            FileInterfaceHelper.narrow(ncRef.resolve(path));
 
         if(argv.length < 1) {
            System.out.println("Usage: JAVA FileClient filename");
         }
 
         // save the file
         File file = new File(argv[0]);
         byte data[] = fileRef.downloadFile(argv[0]);
         BufferedOutputStream output = new
           BufferedOutputStream(new FileOutputStream(argv[0]));
         output.write(data, 0, data.length);
         output.flush();
         output.close();
      } catch(Exception e) {
         System.out.println("FileClient Error: " + e.getMessage());
         e.printStackTrace();
      }
   }
}

 

运行应用

 

最后一步是运行应用。有几个步骤:

1 运行CORBA命名服务。用命令tnameserv.来做。却省端口是900,如果900不能用,你可以用另一端口2500,命令如下:

prompt> tnameserv -ORBinitialPort 2500

 

2 运行服务器 当在却省端口时

prompt> JAVA FileServer

当在其它端口如2500

prompt> JAVA FileServer -ORBInitialPort 2500

 

3 生成客户端干码

prompt> idlj -fclient FileInterface.idl

 

4 运行客户端,假设在2500端口

prompt> JAVA FileClient hello.txt -ORBInitialPort 2500

hello.txt是要下载的文件

 

如果命名服务在4500端口

prompt> JAVA FileClient hello.txt -ORBInitialHost gosling -ORBInitialPort 4500

 

我们还可以在ORB初始化指定一些参数:如

ORB orb = ORB.init(argv, null);

指定CORBA服务器,和命名端口如下:

Properties props = new Properties();  
props.put("org.omg.CORBA.ORBInitialHost", "gosling");  
props.put("orb.omg.CORBA.ORBInitialPort", "2500");  
ORB orb = ORB.init(args, props);  

 

测试

 

在文件传输应用中,客户端需要先知道要传输文件的名字,服务器端没有列表显示文件名的方法,你可以增加方法扩展应用。当然,你也可以开发UI客户端代替命令行。当客户端启动后,它调用服务器端的方法显示文件列表,就可以让用户选择文件来下载它。如下图:

Figure 1: GUI-based File Transfer Client

 

CORBARMI比较

 

显而易见,RMICORBA简单,开发者不需要熟悉IDL。通常,CORBARMI有下列不同:

接口定义不同,CORBAIDL,而RMIJAVA

       CORBA的参数有输入,输出类型。

       CORBA设计与语言无关。是不同语言开发系统的桥梁。

       CORBA对象不能垃圾收集。象我们提到的那样,CORBA与语言无关。象C++不支持垃圾收集那样。这是CORBA的缺点。一旦我们创建CORBA对象,它将一直存在,知道你亲自去处它。而RMI是自动收集。

 

结论

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

原创粉丝点击