第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 ...
随机推荐
- log4jWARN Please initialize the log4j system properly解决办法
原因是没有对log4j这个jar进行文件配置. 要解决这个问题非常简单,建立LOG4J 的配置文件即可.在src 目录下创建配置文件,选择菜单File > New > File,文件名输入 ...
- 利用SSH传输文件
在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地 ...
- Oracle Deadlock / 死锁 处理
Get the directory of alert log sqlplus / as sysdba show parameters udmp SQL> show parameters dump ...
- 自定义scrollview右侧的滑动条
在做这个功能之前我其实是拒绝的,为什么这么说呢,因为我怕麻烦啊!(开玩笑的,怕麻烦就不做程序员了) 很久没有写博客,这次刚好项目中有个有趣的东西,想拿出来分享一下,希望能帮到某些小伙伴. 首先说说需求 ...
- Apache Commons BeanUtils
http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanut ...
- MySQL 显示版本、端口、状态
status select version() show global variables like 'port'
- 7 -- Spring的基本用法 -- 1...2
7.1 Spring简介和Spring4.0的变化 7.1.1 Spring 简介 当使用Spring框架时,必须使用Spring Core Container(即Spring容器),它代表了Spri ...
- js基本类型 引用类型
参考 https://segmentfault.com/a/1190000005794070 http://blog.csdn.net/yummy_go/article/details/5050468 ...
- CSS3--transform
transform:向元素应用2D或3D转换,该属性允许我们对属性旋转,缩放,移动,或倾斜. transfrom:none:定义不进行转换 transfrom:matrix(n,n,n,n,n,n); ...
- Phalapi 中Union和Union All的用法
有时候在进行数据库查询的时候会用到union查询,但是查询的时候会发现两个limit只有一个是有效的,如下查询 select * from table where status = 0 limit 1 ...