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();
}
}
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值