Corba 用IOR方式实现通信

2023-10-06 22:20
文章标签 实现 方式 通信 ior corba

本文主要是介绍Corba 用IOR方式实现通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码地址:https://github.com/Foolegend/corbatest

1.创建一个idl接口文件(HelloServer.idl)如下

interface HelloServer{void sayHello(in string name);
};

利用idlj -fall HelloServer.idl进行编译,会得到下面几个文件

2.创建一个HelloServerImpl实现类,实现接口中定义的功能

package com.fod.service;public class HelloServerImpl extends HelloServerPOA{@Overridepublic void sayHello(String name) {System.out.println("Hello ," + name + ".");}
}

3.创建服务端corba启动类HelloTestService如下

package com.fod.service;import org.omg.CORBA.ORB;
import org.omg.CORBA.Policy;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.ThreadPolicyValue;import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;public class HelloTestService {public static void main(String[] args) {// 生成一个ORB,并初始化,这个和Server端一样Properties props = System.getProperties();//配置发布端口和ipprops.put("org.omg.CORBA.ORBInitialPort", "1050");props.put("org.omg.CORBA.ORBInitialHost", "192.168.0.106");System.out.println("ORB initialised\n");try {// Initialize the ORB.ORB orb = ORB.init(args, props);// get a reference to the root POAorg.omg.CORBA.Object obj = orb.resolve_initial_references("RootPOA");POA poaRoot = POAHelper.narrow(obj);// Create policies for our persistent POAPolicy[] 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 policiesPOA poa = poaRoot.create_POA("HelloServerPOA", poaRoot.the_POAManager(), policies);// Create the servantHelloServerImpl servant = new HelloServerImpl();// Activate the servant with the ID on myPOAbyte[] objectId = "AnyObjectID".getBytes();poa.activate_object_with_id(objectId, servant);// Activate the POA managerpoaRoot.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 requestsorb.run();}catch(Exception ex) {ex.printStackTrace();}}
}

4.创建corba客户端操作类如下

package com.fod.service;import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.Properties;class HelloClientImpl {private HelloServer target = null;private org.omg.CORBA.ORB orb = null;/*** Constructor for HelloClientImpl**/public HelloClientImpl() throws Exception {initORB(null);}/*** Constructor for HelloClientImpl* @see java.lang.Object#Object()*/public HelloClientImpl(String[] args) throws Exception {initORB(args);}/*** Initialize ORB.** @param args*/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", "192.168.0.106");// Initialize the ORBorb = 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();
//此处的ior串是你环境上server.ior文件中的一串字符org.omg.CORBA.Object obj = orb.string_to_object("IOR:000000000000001449444c3a48656c6c6f5365727665723a312e3000000000010000000000000096000102000000000a3132372e302e312e3100b49100000048afabcb00000000206fe9875200000001000000000000000200000008526f6f74504f41000000000f48656c6c6f536572766572504f4100000000000b416e794f626a656374494414000000020000000100000020000000000001000100000002050100010001002000010109000000010001010000000026000000020002");target = HelloServerHelper.narrow(obj);}/*** Obtain ORB Interface.** @return*/public HelloServer 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();test.getORBInterface().sayHello("lifa");//停止test.shutdown();}catch(Exception ex) {ex.printStackTrace();}}
}

5.启动服务端,然后启动客户端,就会看到服务端控制台打印了Hello, lifa

这篇关于Corba 用IOR方式实现通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/154253

相关文章

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page