Java实现CORBA静态绑定(三)

本文主要内容涉及:

  • CORBA基本架构
  • IDL文件编写
  • POA示例实现
  • POA+TIE示例实现
  • ImplBase示例实现
  • ImplBase+TIE示例实现
  • Persistent示例实现

POA(Portable Object Adapter)提供了一组服务器端的接口以及操作,包括:跟踪侍服对象的实例化以及内存空间信息、服务对象的激活以及反激活、对象引用的创建、对象生命周期的管理等。

JDK提供了工具,可以直接生成stubs及skeletons接口代码:

idlj -fall Hi.idl

编写server端代码:

import HiCorba.*;
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;

class HiImpl extends HiPOA {
	private ORB orb;
	public void setORB(ORB orb_val) {
		orb = orb_val;
	}
	// implement sayHiTo() method
	public String sayHiTo(String someone) {
		return "\nHi, "+someone+" !"+"\n";
	}
	// implement add() method
	public int add(int numa, int numb) {
		return numa+numb;
	}
	// implement shutdown() method
	public void shutdown() {
		orb.shutdown(false);
	}
}

public class HiServer {
	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 = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootpoa.the_POAManager().activate();
			// create servant and register it with the ORB
			HiImpl hiImpl = new HiImpl();
			hiImpl.setORB(orb);
			// get object reference from the servant
			org.omg.CORBA.Object ref = rootpoa.servant_to_reference(hiImpl);
			Hi href = HiHelper.narrow(ref);
			// get the root naming context
			// NameService invokes the 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 = "Hi";
			NameComponent path[] = ncRef.to_name( name );
			ncRef.rebind(path, href);
			System.out.println("HiServer 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("HiServer Exiting ...");
	}
}

编写client端代码:

import HiCorba.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class HiClient
{
	static Hi hiImpl;
	public static void main(String args[])
	{
		try{
			// create and initialize the ORB
			ORB orb = ORB.init(args, null);
			// get the root naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			// Use NamingContextExt instead of NamingContext. This is
			// part of the Interoperable naming Service.
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			// resolve the Object Reference in Naming
			String name = "Hi";
			hiImpl = HiHelper.narrow(ncRef.resolve_str(name));
			System.out.println("Obtained a handle on server object: " + hiImpl);
			System.out.println(hiImpl.sayHiTo("neohope"));
			System.out.println(hiImpl.add(70, 80));
			hiImpl.shutdown();
		} catch (Exception e) {
			System.out.println("ERROR : " + e) ;
			e.printStackTrace(System.out);
		}
	}
}

编译代码:

javac *.java HiCorba/*.java

测试,在三个shell或cmd窗口中依次运行:

#shell01
orbd -ORBInitialPort 1900
#shell02
java HiServer -ORBInitialPort 1900
>>HiServer ready and waiting ...
>>HiServer Exiting ...
#shell03
java HiClient -ORBInitialPort 1900 -ORBInitialHost localhost
>>Obtained a handle on server object: IOR:000000000000001349444c3a4869436f7262612f
  48693a312e300000000000010000000000000086000102000000000d3139322e3136382e35362e31
  0000732000000031afabcb000000002076625c7400000001000000000000000100000008526f6f74
  504f4100000000080000000100000000140000000000000200000001000000200000000000010001
  00000002050100010001002000010109000000010001010000000026000000020002
>>Hi, neohope !
>>150

Leave a Reply

Your email address will not be published. Required fields are marked *

*