(:◎)≡会飞的章鱼

导航

第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 消息域了,即进入的是队列还是主题,这样的消息更像是具有了路由功能的更高级的消息。反正我是这样理解的,不知道对不对?

这个程序的缺点:在这里我只是为了验证发布/订阅模式,发布的主题将不会抓住消息,除非消费者设置了一个持久性的订阅。从程序运行来看,发布者一发布消息,在线的多个消费者们马上就会得到这些订阅的消息,而不在线的消费者即时马上在线,过去的发布主题已经逝去了,得不到了。这在以后会慢慢讲到。

    

posted on 2015-09-18 08:08  会飞的章鱼  阅读(619)  评论(0编辑  收藏  举报