Corba开发之基于ior实现

1、概述:

在之前的文章中均已实现Corba环境配置、Eclipse简单开发实现;

在日常开发中,Corba接口远程调用与实现常用的方式有两种:ior(文件方式)和nameService(命名服务方式);

下面是ior文件方式具体的Service和Client实现代码,由于业务需求是使用Java实现,其他语言的后续会补上。


2、说明:

可互操作对象引用,ior文件就是将server端的实现类转换成一个字符串存到后缀名为ior的文件中,在client端通过读取这个文件中的字符串来获得实现类的对象。


3、Service服务端:

public class Server_AOM {


public static void main(String[] args) {


// 生成一个ORB,并初始化,这个和Server端一样
Properties props = System.getProperties();
//配置发布端口和ip
props.put("org.omg.CORBA.ORBInitialPort", "1050");
props.put("org.omg.CORBA.ORBInitialHost", "172.168.xxx.xxx");


System.out.println("ORB initialised\n");
try {
// Initialize the ORB.
org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, props);


// get a reference to the root POA
org.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");
POA poaRoot = POAHelper.narrow(obj);


// Create policies for our persistent POA
org.omg.CORBA.Policy[] policies = {
// poaRoot.create_lifespan_policy(LifespanPolicyValue.PERSISTENT),
poaRoot.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID),
poaRoot.create_thread_policy(ThreadPolicyValue.ORB_CTRL_MODEL)
};


// Create myPOA with the right policies
POA poa = poaRoot.create_POA("HelloServerImpl_poa",poaRoot.the_POAManager(), policies);


// Create the servant
HelloServerImpl servant = new HelloServerImpl();


// Activate the servant with the ID on myPOA
byte[] objectId = "AnyObjectID".getBytes();
poa.activate_object_with_id(objectId, servant);


// Activate the POA manager
poaRoot.the_POAManager().activate();


// Get a reference to the servant and write it down.
obj = poa.servant_to_reference(servant);


PrintWriter ps = new PrintWriter(new FileOutputStream(new File("server.ior")));
ps.println(orb.object_to_string(obj));
ps.close();


System.out.println("CORBA Server ready...");


// Wait for incoming requests
orb.run();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
Service端重点是读取server.ior文件,


4、Client客户端:
class HelloClientImpl {
private helloapp.Hello target = null;
private org.omg.CORBA.ORB orb = null;


/**
* Constructor for HelloClientImpl
*
* @throws IOException
*/
public HelloClientImpl() throws Exception {
initORB(null);
}


/**
* Constructor for HelloClientImpl
*
* @throws IOException
* @see java.lang.Object#Object()
*/
public HelloClientImpl(String[] args) throws Exception {
initORB(args);
}


/**
* Initialize ORB.
*
* @param args
* @throws IOException
*/
public void initORB(String[] args) throws Exception {
//设置远程调用ip和端口
Properties props = System.getProperties();
props.put("org.omg.CORBA.ORBInitialPort", "1050");
props.put("org.omg.CORBA.ORBInitialHost", "172.168.xxx.xxx");


// Initialize the ORB
orb = org.omg.CORBA.ORB.init((String[])args, props);


// ---- Uncomment below to enable Naming Service access. ----
LineNumberReader input = new LineNumberReader(new FileReader("server.ior"));
String ior = input.readLine();
org.omg.CORBA.Object obj = orb.string_to_object(ior);


target = helloapp.HelloHelper.narrow(obj);
}


/**
* Obtain ORB Interface.
*
* @return
*/
public helloapp.Hello getORBInterface() {
return target;
}


/**
* Shutdown ORB.
*/
public void shutdown() {
orb.shutdown(true);
}


/**
* Test driver for HelloClientImpl.
*
* @param args
*/
public static void main(String[] args) {
try {
HelloClientImpl test = new HelloClientImpl();


String result = test.getORBInterface().sayHello();
System.out.println("result:"+result);
//停止
test.shutdown();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}


你可能感兴趣的:(java,corba,corba,corba,ior,ior)