当前位置: 首页 > ActiveMQ in Action 读书笔记 > 正文

12.3 使用 ActiveMQ流

12.3 ActiveMQ streams

12.3 使用 ActiveMQ流

ActiveMQ streams are an advanced feature that allows you to use an ActiveMQ client asa Java IOStream. ActiveMQ will break an OutputStream into distinct chunks of dataand send each chunk through ActiveMQ as a JMS message. A corresponding ActiveMQJMS InputStream should be used on the consumer side to reassemble the datachunks.

ActiveMQ流 是一个高级特性,允许你在ActiveMQ客户端使用Java 的IOStream.ActiveMQ会将一个OutputStream拆分成不同的数据块,然后将每一个数据库当成JMS消息并发送.在消息消费者端需要使用一个相对应的ActiveMQ JMS InputStream来重新组装收到的数据块.

If you use a queue as the destination for the stream, using more than one consumeron a queue (or an exclusive consumer) is fine because this feature uses messagegroups. This causes messages with the same group ID to be pinned to a singleconsumer. Using more than one producer in this scenario could cause problems withthe message order.

如果使用消息队列作为流的消息目的地,使用多个消息消费者(或者使用排他性消息消费者)比较好,因为这个特性中用到了消息群组.这样会导致具有相同群组ID的消息会被发送到同一个消息消费者.这种场景下使用多个消息生产者会因为消息顺序问题产生问题.

The benefit of using JMS streams is that ActiveMQ will break a stream into manageablechunks and reassemble them for you at the consumer. So it’s possible to transfervery large files using this functionality, as depicted in figure 12.4.

使用JMS 流的好处是ActiveMQ将一个流分割成可管理的数据块并且可以在消息消费者中重新组装这些数据库.因此,使用ActiveMQ流可以传输非常大的文件,如图12.4所示:

To demonstrate using streams, here’s an example of reading a large file and writingit out over ActiveMQ:

下面用一个读取一个大文件并使用ActiveMQ重新获取文件的示例来说明如何使用流:

In the example, we create an ActiveMQConnection and create an OutputStream usinga queue as the destination. We read the file using a FileInputStream, then write theFileInputStream onto the ActiveMQ OutputStream.

在上面的示例代码中,我们创建了一个ActiveMQConnection,并使用一个消息队列创建了一个OutputStream.我们使用FileInputStream读取了一个文件,然后将FileInputStream写入到ActiveMQ的OutputStream中.

Note that we close the ActiveMQ OutputStream when we’ve completed reading thefile. This is important so that the receiving side can determine whether the stream isfinished. It’s recommended that you use a new OutputStream for each file you send.
For completeness, here’s the receiving end of an ActiveMQ stream:

需要注意的是,我们在完成了读取文件后关闭了ActiveMQ的OutputStream.这点很重要,因为关闭了OutputStream后,ActiveMQ流的接收端就可以决定当前接收的流是否结束.推荐你为每个要发送的文件使用新的OutputStream.
为了完整起见,下面列出了ActiveMQ流的接收端代码:

In the example, we create an ActiveMQConnection and from that create an Input-Stream using a queue as a consumer. Note that we use an exclusive consumer byappending “?consumer.exclusive=true” to the name of the queue. We do this toensure that only one consumer will be reading the stream at a time. We read theActiveMQ InputStream and then write it to a FileOutputStream to reassemble thefile on disk. Note that we expect the end of the file to be denoted by the end of thestream (or -1).

在上面的示例代码中,我们创建了一个ActiveMQConnection,并再此基础上创建了一个Input-Stream使用一个队列作为消费者. 需要注意的是,我们在队列名称后面加上了”?consumer.exclusive=true”了创建一个排他性的消息消费者.这样做是为了确保同一时刻只有一个消费者可以读取流.读取了ActiveMQ的InputStream后,将其写入到FileOutputStream中以便重新组装流并写入到磁盘文件中.注意,我们希望通过流的末尾(或者读取到-1)来确定文件的结尾.

You can use streams with topics too—though if a consumer for a topic starts partwaythrough the delivery of a stream, it won’t receive any data that was sent before itwas started.

你也可以在主题中使用流–但,如果消息消费者在流已经开始传送后的中途启动,则该消费者将不会收到任何在它启动之前就已经发送过的任何数据.

ActiveMQ breaks the stream into manageable chunks of data and sends eachchunk of data as a separate message. This means that you have to be careful whenusing them, because if the message consumer should fail partway through reading theInputStream, there’s currently no way to replay the messages already consumed bythe failed message consumer.

ActiveMQ将流分成可管理的数据块然后将所有的数据块作为独立的消息发送.这就意味着你在使用流时必须十分小心.因为如果消息消费者在读取InputStream的中途失效了,则是前期的消息消费者已经读取的消费无法重现了.

ActiveMQ streams are useful for transferring large payloads, though you’ll need tothink about how an application using ActiveMQ streams should handle failure scenarios.There’s an alternative and more robust method of sending large payloads: usingblob messages, which we cover in the next section.

ActiveMQ流 对于传输大尺寸负载来说十分有用,尽管你需要仔细思考,以应对使用ActiveMQ流的应用程序的失效场景.对于发送给大尺寸负载来说还有一种更加健壮的替代方法,即使用二进制消息,我们将在下一节中介绍.

打个赏呗

   微信打赏  支付宝打赏


本文固定链接: https://www.jack-yin.com/coding/translation/activemq-in-action/1754.html | 边城网事

该日志由 边城网事 于2013年12月20日发表在 ActiveMQ in Action 读书笔记 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 12.3 使用 ActiveMQ流 | 边城网事
关键字: , , ,
【上一篇】
【下一篇】

12.3 使用 ActiveMQ流 暂无评论

发表评论

快捷键:Ctrl+Enter