第3.2 使用案例1:股票期货stock portfolio 21050917
As mentioned earlier in the chapter, the first use case revolves around a stock portfolio use case for demonstrating publish/subscribe messaging. This example is simple and utilizes a Publisher class for sending stock price messages to a topic, as well as a Consumer class for registering a Listener class to consume messages from topics in an asynchronous manner. These three classes embody the functionality of generating ever-changing stock prices that are published to topics on which the consumer is subscribed.
就象本章前面所提到的,第一个使用案例是围绕这股票期货的循环出现使用这个案例是为了论证发布/订阅消息。这个案例是简单的并利用一个Publisher类向topic发送股票价格的消息,又用注册了Listener类的Consumer类采用异步模式从topic中消费消息,包含着能产生时刻在变换着股票价格功能,这些价格被发布到主题上,而且这些主题是被消费者订阅了。
In this example, stock prices are published to an arbitrary number of topics. The number of topics is based on the number of arguments sent to the Publisher and the Consumer on the command line. Each class will dynamically send and receive to/from the topics (an example is provided next). Take a look at figures 3.2 and 3.3 to see at a high level what the examples seek to achieve.
在这个案例中,股票价格被发布到你输入的任意数量的主题们上,是在启动Publisher和Consumer以命令行的形式输入的这些数量的主题们的,每一个类将动态向主题发送或接受(下一实例提供),预先瞟一眼,在3.2和3.3实例中会以高水平看到怎么完成探索的。
For the sake of this demonstration, two topics will be used. The Publisher class uses a single JMS MessageProducer to send 1,000 fictitious stock price messages in blocks of 10, randomly distributed across the topics named in the command-line argument. After it sends 1,000 messages, it shuts down. The Consumer class creates one JMS MessageConsumer per topic and registers a JMS MessageListener for each topic. Because this example demonstrates publish/subscribe, the Consumers must be online to consume messages being sent by the Publisher, because durable consumers aren’t used in the basic stock portfolio example. The next step is to actually run the example so that you can see them in action.
为了验证这个范例,我使用了2个topics, Publisher类使用了单独一个JMS 消息消费者发送了1000个股票行市的消息,一块10个消息,间隔1000毫秒,随机分布产生的由类启动时命令行输入产生的交叉主题,发送1000个消息后,生产者关闭。消费者类产生一个JMS消息消费者,对于每一个主题都会注册一个JMS消息监听者。因为这个实例是验证发布/订阅,消费之必须在线去消费由Publisher发布过来的消息,因在这个最基本的股票期货实例中将没有使用持久性的消费者,(这一点的理解应是消费者必须先启动类得以说明),下一步是实际运行这个例子以便让你看到怎样运行的。
3.2.1 运行股票期货stock portfolio实例
基本步骤:
1、启动ActiveMQ
2、运行Consumer类
3、运行Publisher类
步骤是简单的,代码也是,唯一需要你注意的是:Consumer类的启动应该是早于Publisher类的,目的是为了等待接受所有刚发布的消息。原因是在这个实例中发布/订阅消息和主题们是抓不住消息的,除非消费者在这里设置了持久性的订阅。那么让我们随着这个股票期货的实例开始学习吧。
第一个任务就是执行启动ActiveMQ,这个会吧?不会就无语了,趁早歇歇睡觉吧。以下是我的实例理解图:
以下说明有助于对程序的理解:
1、发布者只有1个,2个消费者,消息1对多模式。
2、这个Publisher在发送完10个主体消息后,就关闭连接了。
3、主题Topic创建的过程,固定格式:
① 由总的一个会话来创建不同名字的发布主题Topic。
② 创建一个由自定义的能包装好自己需要数据的一个映射消息。
③ 最终由生产者把这个映射消息发送走,完成一个主题发布。
这里需要强调的发布者是不同于消费者,对于每一条消息,发布者都需要再为这条消息重新定制destination和发布主题,即时desination和发布主题同前一个消息都相同。所以,在我的程序中,发布者创建了10个发布主题,同时也是发送了10个消息。而在消费者,仅仅是创建了(订阅了)了2个主题,这一点在实质上是不同的。
对于发布者,其实最终也是生产者,生产者是动作的执行者,只不过它发的消息已经不再普通的消息,这个消息会进入ActiveMQ的JMS 消息域了,即进入的是队列还是主题,这样的消息更像是具有了路由功能的更高级的消息。反正我是这样理解的,不知道对不对?
这个程序的缺点:在这里我只是为了验证发布/订阅模式,发布的主题将不会抓住消息,除非消费者设置了一个持久性的订阅。从程序运行来看,发布者一发布消息,在线的多个消费者们马上就会得到这些订阅的消息,而不在线的消费者即时马上在线,过去的发布主题已经逝去了,得不到了。这在以后会慢慢讲到。
第3.2 使用案例1:股票期货stock portfolio 21050917的更多相关文章
- ADF_ADF Faces系列4_ADF数据可视化组件简介之建立BarChart/Gauge/ExportExcel
2013-05-01 Created By BaoXinjian
- httpCookie与Cookie安全
Web 应用程序使用的 Cookie 个人认为这里设置的cookie与访问cookie的安全性关联大一点,配置节如下 <httpCookies domain="String" ...
- pandas应用之分组因子暴露和分位数分析
pandas应用之分组因子暴露和分位数分析 首先感谢原书作者Mes McKinney和batteryhp网友的博文, 俺在此基础上继续探索python的神奇功能. 用A股的实际数据, 以书里的代码为蓝 ...
- javascript中for/in循环及使用技巧
JavaScript 支持不同类型的循环: for - 循环代码块一定的次数 for/in - 循环遍历对象的属性 while - 当指定的条件为 true 时循环指定的代码块 do/while - ...
- 【转】Spring websocket 使用
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html https://spr ...
- [转] Are You Making a Big Mistake in This Volatile Market?
Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...
- ActiveMQ笔记——技术点汇总
目录 · Introduction to ActiveMQ · Installing ActiveMQ · Message-oriented middleware · JMS specificatio ...
- 《ActiveMQ in Action》【PDF】下载
内容介绍TheApache ActiveMQ message broker is an open source implementation ofthe Java Message Service sp ...
- activemq 实战 四 传输连接器-Transport connectors 4.2
In order to exchange messages, producers and consumers (clients) need to connect to the broker. This ...
随机推荐
- Map拷贝 关于对象深拷贝 浅拷贝的问题
问题:map拷贝时发现数据会变化. 高能预警,你看到的下面的栗子是不正确的,后面有正确的一种办法,如果需要看的话的,请看到底,感谢各同学的提醒,已做更正,一定要看到最后 先看例子: ...
- 使用自定义标签模拟jstl的<c:for each>标签
一.自定义标签的基本编写 下面编写一个自定义标签,它可以输出当前的时间. 1.编写标签类 类可以通过继承SimpleTagSupport类实现一个标签类编写.父类为我们提供了一些编写自定义标签的快捷的 ...
- 生成多sitemap文件
Thinkphp生成多sitemap文件 我们知道sitemap对于seo的重要性,很多介绍只生成一个文件sitemap.xml ,但是如果网站内容比较多,就要生成多个sitemap文件,因为搜索引擎 ...
- ajax 跨域访问
后台方法添加 HttpServletResponse response=ServletActionContext.getResponse(); response.addHeader("Acc ...
- KEGG数据库
参考:KEGG数据库中文教程 - 博奥 &[学习笔记]KEGG数据库 - 微信 学习一个技能最主要的事情你必须知道,那就是能通过它来做什么? KEGG数据库里面有什么? 如何查询某一特定的代 ...
- node.js基础 1之 Querystring参数处理小利器
在处理查询字符串中很有用(⊙o⊙)哦~~~ querystring.stringify(obj,sign1,sign2)//将对象转化成url中query部分的形式 参数:1.要转化的对象 2.链接符 ...
- MySQL数据库常用命令
1.连接mysql数据库:mysql -u用户名 -p密码; 2.创建数据库:create database 数据库名称; 3.删除数据库:drop database 数据库名称; 4.使用数据库:u ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- CProgressCtrl进度条
CProgressCtrl进度条 使用方法总结 标签: CProgressCtrlmfc 2016-03-03 09:19 762人阅读 评论(0) 收藏 举报 分类: MFC(11) 版权声明: ...
- java中使用sql的like关键字
String sql = "select * from userinfo where nickname like ?"; PreparedStatement ps = conn.p ...