Java实现CORBA静态绑定(四)

本文主要内容涉及:

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

与POA方式相比,POA-Tie将继承方式,调整为委托方式,降低了软件的耦合度。

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

idlj -fall Hi.idl
idlj -fallTie 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);
			// create a tie, with servant being the delegate.
			HiPOATie tie = new HiPOATie(hiImpl, rootpoa);
			// obtain the objectRef for the tie
			// this step also implicitly activates the object
			Hi href = tie._this(orb);
			// get the root naming context
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			// Use NamingContextExt which is part of the Interoperable
			// Naming Service 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{
	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";
			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
  0000744700000031afabcb00000000207673791300000001000000000000000100000008526f6f74
  504f4100000000080000000100000000140000000000000200000001000000200000000000010001
  00000002050100010001002000010109000000010001010000000026000000020002
>>Hi, neohope !
>>150

Leave a Reply

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

*