关闭

CORBA中传递对象 java语言

2249人阅读 评论(1) 收藏 举报

CORBA的IDL数据类型定义了一系列的基本数据类型,但是如果需要传递对象可能就有些问题了。虽然IDL文件中的interface其实就是传递对象的一种方法,我的理解就是把要传递的对象的属性分成基本数据类型传递过去,但是如果传递的对象结构变化了,还需要修改IDL文件。所以想找到一个直接传递对象的方法,研究了CORBA定义的数据类型,最好的办法就是像很多连接传输对象的方法一样,传递byte数组。

建一个目录,名为Hello(名字随意起),然后在这个目录中建两个子目录,分别为server,client

在两个目录中各建一个People.java文件,内容如下:

import java.io.*;
class People implements Serializable
{
 private String name;
 private int age;

 public People()
 {
  name = "CY";
  age = 27;
 }

 public People(String str, int i)
 {
  name = str;
  age = i;
 }

 public String getName()
 {
  return name;
 }
 
 public int getAge()
 {
  return age;
 }

}

在server目录建一个Hello.idl文件,内容如下:

module HelloApp
{
  interface Hello
  {
  string sayHello();
  oneway void shutdown();
  };
};

同样在server目录建HelloServer.java文件,内容如下:

// A server for the Hello object

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import java.util.Properties;

public class HelloServer {

  public static void main(String args[]) {
    try{
      // create and initialize the ORB
      ORB orb = ORB.init(args, null);

      // get reference to rootpoa & activate the POAManager
      POA rootpoa =
        (POA)orb.resolve_initial_references("RootPOA");
      rootpoa.the_POAManager().activate();

      // create servant and register it with the ORB
      HelloImpl helloImpl = new HelloImpl();
      helloImpl.setORB(orb);

      // get object reference from the servant
      org.omg.CORBA.Object ref =
        rootpoa.servant_to_reference(helloImpl);


      // and cast the reference to a CORBA reference
      Hello href = HelloHelper.narrow(ref);
  
      // get the root naming context
      // NameService invokes the transient name service
      org.omg.CORBA.Object objRef =
          orb.resolve_initial_references("NameService");
      // Use NamingContextExt, which is part of the
      // Interoperable Naming Service (INS) specification.
      NamingContextExt ncRef =
        NamingContextExtHelper.narrow(objRef);

      // bind the Object Reference in Naming
      String name = "Hello1";
      NameComponent path[] = ncRef.to_name( name );
      ncRef.rebind(path, href);

      System.out.println
        ("HelloServer ready and waiting ...");

      // wait for invocations from clients
      orb.run();
    }
 
      catch (Exception e) {
        System.err.println("ERROR: " + e);
        e.printStackTrace(System.out);
      }
  
      System.out.println("HelloServer Exiting ...");
 
  } //end main
} // end class

在server目录下建立HelloImpl.java文件,内容如下:

// The servant -- object implementation -- for the Hello
// example.  Note that this is a subclass of HelloPOA, whose
// source file is generated from the compilation of
// Hello.idl using j2idl.

import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import java.util.Properties;
import java.io.*;

class HelloImpl extends HelloPOA //必须继承这个类,在helloApp目录中已自动生成
{
 private ORB orb;
  private byte[] bytes;
 public void setORB(ORB orb_val)
 {
  orb = orb_val;
 }

 // implement sayHello() method
 public byte[] sayHello()
 {
  return (ObjectToByte(new People("Chen Yi",27)));
 }

 // implement shutdown() method
 public void shutdown()
 {
  orb.shutdown(false);
 }
 public byte[] ObjectToByte(java.lang.Object obj)
 {

  try {
   //object to bytearray
   ByteArrayOutputStream bo = new ByteArrayOutputStream();
   ObjectOutputStream oo = new ObjectOutputStream(bo);
   oo.writeObject(obj);

   bytes = bo.toByteArray();

   bo.close();
   oo.close();

   
  }
  catch(Exception e) {
   System.out.println("translation"+e.getMessage());
   e.printStackTrace();
  }
  return(bytes);
 }

} //end class

在client目录下建立HelloClient.java文件,内容如下:

// A sample Java IDL object client application.
import HelloApp.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;

public class HelloClient
{
 static Hello helloImpl;
 String [] x=new String[6];
 static People people;
 static java.lang.Object obj;
 public static void main(String args[]){
  try{
   // create and initialize the ORB
   ORB orb = ORB.init(args, null);

   System.out.println("ORB initialised/n");

   // get the root naming context
   org.omg.CORBA.Object objRef =
   orb.resolve_initial_references("NameService");

   // Use NamingContextExt instead of NamingContext,
   // part of the Interoperable naming Service.
   NamingContextExt ncRef =
   NamingContextExtHelper.narrow(objRef);

   // resolve the Object Reference in Naming
   String name = "Hello1";
   helloImpl =
   HelloHelper.narrow(ncRef.resolve_str(name));

      people = (People)(ByteToObject(helloImpl.sayHello()));

   System.out.println
   ("Obtained a handle on server object: "
   + helloImpl);
   System.out.println(people.getName());
   helloImpl.shutdown();

  }
  catch (Exception e) {
   System.out.println("ERROR : " + e) ;
   e.printStackTrace(System.out);
  }
 } //end main

 private static java.lang.Object ByteToObject(byte[] bytes){
  try {
  //bytearray to object
  ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
  ObjectInputStream oi = new ObjectInputStream(bi);

  obj = oi.readObject();

  bi.close();
  oi.close();
  }
  catch(Exception e) {
   System.out.println("translation"+e.getMessage());
   e.printStackTrace();
  }
  return obj;
 }
} // end class

下面是单机版编译步骤

1. 编译server中IDL文件,在命令行执行   idlj -fall HelloApp.idl

2. 编译server中.java文件  javac *.java

3. 将server中编译IDL文件后产生的文件目录HelloApp复制到client目录中

4. 编译client中.java文件  javac *.java

5. 运行orbd  start orbd -ORBInitialPort 1050

6. 运行server  start java HelloServer -ORBInitialPort 1050 -ORBInitialHost localhost

7. 运行client  java HelloClient -ORBInitialPort 1050 -ORBInitialHost localhost

运行结果,会在client命令行中看到打印出人名.如果需要进一步测试,可以在HelloImpl中修改new People(str,i)的参数也可以在client中调用People的其他方法.

多机版运行方法
编译方法同单机版
1. 运行orbd   start orbd -ORBInitialPort 1050 -ORBInitialHost servermachinename

2. 运行server   java HelloServer -ORBInitialPort 1050

3. 运行client   java HelloClient -ORBInitialHost nameserverhost -ORBInitialPort 1050

注意 nameserverhost 是服务器的IP地址

另外idlj命令的参数也可以修改, -fall是生成server和client的映射, -fserver是生成server映射, -fclient是生成client映射.如果用后面两个参数需要将.idl文件分别放在两个目录中

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2356136次
    • 积分:22477
    • 等级:
    • 排名:第337名
    • 原创:300篇
    • 转载:187篇
    • 译文:20篇
    • 评论:519条
    最新评论
    个人链接