Ice系列——HelloIce
来源:互联网 发布:阿里云 健康管理平台 编辑:程序博客网 时间:2024/06/05 03:21
HelloIce: 开发环境Zeroc3.5.1,Eclipse
(1) 定义slice文件 hello.ice
module demo{ interface HelloIce{ string hello(); }; };
Eclipse 自动生成转换后的Java文件。
(2) 服务实现 Impl
package com.demo.ice;import Ice.Current;import demo._HelloIceDisp;public class HelloIceImpl extends _HelloIceDisp{ @Override public String hello(Current __current) { // TODO Auto-generated method stub return "Hello Ice"; }}
(3) HelloIce服务器Server实现
package com.demo.ice;public class HelloIceServer { public static void main(String[] args){ int status =0; Ice.Communicator ic=null; try{ //初始化Communicator连接对象,负责处理客户端的连接,可以进行设置,如客户端连接数量 ic = Ice.Util.initialize(args); // 创建Adapter对象,理解为一个适配器,只要连接这个endpoints 都返回一个adapter对象,广义的 Ice.ObjectAdapter adapter = ic.createObjectAdapterWithEndpoints("HelloAdapter", "default -p 10000"); // 实例化一个服务对象,具体的服务对象 HelloIceImpl servant = new HelloIceImpl(); HelloIceImpl servant1 = new HelloIceImpl(); //将服务对象添加到adapter中,并设置ID进行关联,很多个服务对象,必须确定是哪一个对象 adapter.add(servant, Ice.Util.stringToIdentity("HelloService")); adapter.add(servant1, Ice.Util.stringToIdentity("HelloService1")); // 启动adapter adapter.activate(); //服务保持监听状态,直到退出 System.out.println("Server started");// if(ic.) ic.waitForShutdown(); }catch(Exception e){ e.printStackTrace(); status=1; }finally{ if (ic!=null){ ic.destroy(); } } System.exit(status); }}
(4) HelloIce Client实现
package com.demo.ice;import demo.HelloIcePrx;import demo.HelloIcePrxHelper;public class HelloIceClient { public static void main(String[] args) { int status =0; Ice.Communicator ic=null; try{ //初始化Communicator连接对象,负责处理客户端的连接,可以进行设置,如客户端连接数量 ic = Ice.Util.initialize(args); // 客户端代理proxy对象,访问确定的endpoints,返回的对象是广义的 Ice.ObjectPrx base = ic.stringToProxy("HelloService:default -p 10000"); // 类似于强制转换, 转换为具体的对象。 HelloIcePrx proxy = HelloIcePrxHelper.checkedCast(base); if(proxy==null) { throw new Error("Invalid proxy"); } // 实际上是调用服务器端的方法。 String retMsg = proxy.hello(); System.out.println(retMsg); }catch(Exception e){ e.printStackTrace(); status=1; }finally { if (ic!= null){ ic.destroy(); } } System.exit(status); }}
(5)运行结果
server:
Server started
Client
Hello Ice
(6) 代码组织图片
实现过程中的一些代码已有注释,具体的分析在后面章节中进行。
阅读全文
1 0
- Ice系列——HelloIce
- Ice系列--Ice Registry(1)
- Ice系列--IceBox(1)
- Ice系列--IceBox(2)
- Ice系列--IceGrid Node
- 学习ICE——2
- ICE(一)——安装
- ICE(三)——概述
- Ice自学第一步——Windows下安装Ice和设置Ice的环境变量
- Ice自学第一步——Windows下安装Ice和设置Ice的环境变量
- ICE重新学习之——IcePatch
- PHP的中间件——ICE
- ICE学习——异步1
- ZOJ - 3611——Ice Valley
- ZeroC—ICE中间件初步Demo
- ZeroC—ICE分布式数据同步Demo
- ZeroC—ICE分布式数据同步Demo
- webrtc系列专题之trickle ice
- 基于安全的一些思考--竞态条件
- 邮件
- C++ 的MFC 和ATL 及COM 是什么?
- Django学习03---urls.py路由用法与实例
- git 上传命令
- Ice系列——HelloIce
- 等差数列 2,5,8,11,14。。。。 输入:正整数N >0 输出:求等差数列前N项和
- 基于安全的一些思考--随机性与密码学
- Python学习随笔四:使用str,dic和set
- 2.点线面
- PB用OLE创建Excel后,退出程序后excel.exe进程还在
- 封装简单动画函数-由简到完善
- 将一个英文语句以单词为单位逆序排放。
- Java 中强应用和弱引用