ICE发布者/订阅者的一个最简单例子

IceStorm服务

启动命令

$ /Library/Developer/Ice-3.5.1/bin/icebox --Ice.Config=config.icebox

config.icebox

#
# The IceBox server endpoint configuration. This endpoint is only used
# to communicate with the IceBox ServiceManager object (such as when
# using iceboxadmin to shutdown the server).
#
# The IceStorm service has its own endpoints (see config.service).
#
IceBox.ServiceManager.Endpoints=tcp -h localhost -p 9998#
# The IceStorm service. The service is configured using a separate
# configuration file (see config.service).
#
IceBox.Service.IceStorm=IceStormService,35:createIceStorm --Ice.Config=config.service

config.service

#
# The IceStorm service instance name.
#
IceStorm.InstanceName=DemoIceStorm#
# This property defines the endpoints on which the IceStorm
# TopicManager listens.
#
IceStorm.TopicManager.Endpoints=default -h localhost -p 10000#
# This property defines the endpoints on which the topic
# publisher objects listen. If you want to federate
# IceStorm instances this must run on a fixed port (or use
# IceGrid).
#
IceStorm.Publish.Endpoints=tcp -h localhost -p 10001:udp -h localhost -p 10001

Slice

Clock.ice

module Demo
{interface Clock
{void tick(string time);
};};

发布者

config.pub

#
# This property is used by the clients to connect to IceStorm.
#
TopicManager.Proxy=DemoIceStorm/TopicManager:default -h localhost -p 10000

Publisher.cpp

int
Publisher::run(int argc, char* argv[])
{IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast(communicator()->propertyToProxy("TopicManager.Proxy"));//// Retrieve the topic.//
    string topicName = "time";IceStorm::TopicPrx topic;topic = manager->create(topicName);// ortopic = manager->retrieve(topicName);//// Get the topic's publisher object, and create a Clock proxy//
    Ice::ObjectPrx publisher = topic->getPublisher();ClockPrx clock = ClockPrx::uncheckedCast(publisher);while(true){clock->tick(IceUtil::Time::now().toDateTime());IceUtil::ThreadControl::sleep(IceUtil::Time::seconds(1));}
}

订阅者

config.sub

#
# This property is used to configure the endpoints of the clock
# subscriber adapter. These endpoints are where the client receives
# topic messages from IceStorm.
#
Clock.Subscriber.Endpoints=tcp:udp#
# This property is used by the clients to connect to IceStorm.
#
TopicManager.Proxy=DemoIceStorm/TopicManager:default -h localhost -p 10000

Subscriber.cpp

class ClockI : public Clock
{
public:virtual voidtick(const string& time, const Ice::Current&){cout << time << endl;}
};int
Subscriber::run(int argc, char* argv[])
{string topicName = "time";IceStorm::TopicManagerPrx manager = IceStorm::TopicManagerPrx::checkedCast(communicator()->propertyToProxy("TopicManager.Proxy"));IceStorm::TopicPrx topic;topic = manager->retrieve(topicName);// ortopic = manager->create(topicName);Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapter("Clock.Subscriber");//// Add a servant for the Ice object. If --id is used the identity// comes from the command line, otherwise a UUID is used.//// id is not directly altered since it is used below to detect// whether subscribeAndGetPublisher can raise AlreadySubscribed.//
    Ice::Identity subId;Ice::ObjectPrx subscriber = adapter->add(new ClockI, subId);//// Activate the object adapter before subscribing.//
    adapter->activate();IceStorm::QoS qos;qos["retryCount"] = retryCount;qos["reliability"] = "ordered";topic->subscribeAndGetPublisher(qos, subscriber);shutdownOnInterrupt();communicator()->waitForShutdown();topic->unsubscribe(subscriber);
}

转载于:https://www.cnblogs.com/leaf-w/p/3786387.html

ICE提纲之demo/IceStorm/clock(发布者/订阅者)相关推荐

  1. Redis 学习笔记十 发布者订阅者模式与生产者消费者模式

    消息队列有两种场景 生产者消费者:一个消息只能有一个消费者 发布者订阅者:一个消息可以被多个消费者收到 redis从2.0版本开始支持pub/sub. 而Producer/Consumer是借助于re ...

  2. [js高手之路]设计模式系列课程-发布者,订阅者重构购物车

    发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...

  3. 『设计模式』80年代的人们就已经领悟了设计模式-- 发布者/订阅者模式 (包括发布者/订阅者模式和观察者模式的区别)

    23种设计模式+额外常用设计模式汇总 (持续更新) 发布-订阅模式 在软件架构中,发布订阅是一种消息范式,消息的发送者(称为发布者)不会将消息直接发送给特定的接收者(称为订阅者).而是将发布的消息分为 ...

  4. Vue2 MVVM 双向绑定(数据劫持+发布者-订阅者模式)

    参考文献:https://www.cnblogs.com/libin-1/p/6893712.html https://juejin.im/post/5b2f0769e51d45589f46949e ...

  5. eventbus多个订阅_番石榴的EventBus –简单的发布者/订阅者

    eventbus多个订阅 在查看Google的Guava库版本10的最新添加内容时,我注意到EventBus的添加. 这是发布-订阅样式消息传递系统的轻量级实现. 这类似于JMS提供的发布-订阅模型, ...

  6. 番石榴的EventBus –简单的发布者/订阅者

    在查看Google的Guava Libraries 版本10的最新版本时,我注意到EventBus的添加. 这是发布-订阅样式消息传递系统的轻量级实现. 这类似于JMS提供的发布-订阅模型,但是消息保 ...

  7. 委托,了解发布者订阅者模型

    一.发布者订阅者模型图解. 从图中我们看有一个部分组成? 发布者 订阅者 事件绑定 事件信息 3,4部分容易混为一谈,我们这里做一个例子用于理解: 要是实现A->B货物的运输,需要什么呢?需要一 ...

  8. 发布者订阅者模式之JAVA实现

    发布者订阅者模式之JAVA实现   1.发布者接口 package com.shoshana.publishsubscribe; public interface IPublisher<M> ...

  9. 基于发布者/订阅者模式的小媛逛超市

    基于发布者/订阅者模式的小媛逛超市 一.概述 (1) 角色对应: 发布者=小媛,订阅者=超市. (2) 过程简介: 小明来到超市购物,超市统计小媛购物的种类数量. 二.代码结构 三.解决方案结构 四. ...

最新文章

  1. Installation error: INSTALL_FAILED_CONTAINER_ERROR
  2. 九 Deepin配置ssh访问gitee
  3. c++ 调用system 不显示黑框_Java回调的四种写法:反射+直接调用+接口调用+Lambda表达式...
  4. Fetch发送网络请求
  5. 【收藏】HTML颜色参考
  6. python写csv文件按升序排列_用python给csv里的数据排序的具体代码
  7. 用python画简单的图案-使用 Python Turtle 设计简单而又美丽的图形
  8. Linux文件系统格式EXT3,EXT4和XFS的区别
  9. html点菜系统显示几号桌,如果一定要坐你会坐几号桌 1到4号桌你会如何选择
  10. Robocup3D优化任务布置
  11. matlab 最优资产组合,基于MATLAB的最优投资组合问题.pdf
  12. python神经网络反向传播_神经网络及反向传播算法
  13. 网站社交登录之微博登录
  14. 记录论文中用到的伪代码
  15. 对于币圈的情况,除了内心没有别
  16. 危鸡之夜服务器维护,2018热门对战竞技游戏大盘点
  17. code review的思考和实践
  18. 由“要读书,不要屯书”想到的
  19. 怎样将mpg格式的视频文件转换成mp4格式
  20. 面向征信的区块链模式设计与应用研究

热门文章

  1. OpenShift 4 - 通过DaemonSet在指定Node上运行守护程序
  2. OpenShift 4 - 直接将Web应用当成Serverless运行
  3. .net core精彩实例分享 -- 字符串处理
  4. qt判断读入的字符串是否含有英文_重复的子字符串
  5. 直方图 帕累托图_工具讲解 | 用Excel绘制帕累托图
  6. py导入包异常跳出_python~异常处理及包
  7. 需要用sq语句 修改大批量用户的密码_网站文章seo优化及修改已收录文章建议
  8. 翻牌抽奖 php,PHP+jQuery开发简单的翻牌抽奖实例
  9. oracle有没有mysql if_Oracle中没有 if exists(...)
  10. 服务器系统分区 是啥,服务器系统盘分区