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

12.4 使用 二进制消息

12.4 Blob messages

12.4 使用 二进制消息

ActiveMQ introduced the concept of blob messages so that users can take advantage ofActiveMQ message delivery semantics (transactions, load balancing, and smart routing)in conjunction with very large messages. A blob message doesn’t contain the databeing sent, but is a notification that a blob (binary large object) is available. The blobitself is transferred out of bounds, by either FTP or HTTP. In fact, an ActiveMQBlobMessage only contains the URL to the data itself, with a helper method to grab anInputStream to the real data. Let’s work through an example.

ActiveMQ引入了 二进制消息 的概念,这样用户可以将消息分发的语义(传输连接,负载均衡和智能路由)同超大尺寸消息结合起来.二进制消息并不包含要发送的数据,而是通知要发送的二进制数据(大尺寸二进制对象)已经准备完成了.二进制对象本身是在消息之外传输的,通过FTP或者HTTP传输.事实上,ActiveMQ的二进制消息仅包含二进制数据的URL,通过一个助手方法可以抓取InputStream进而获取真正的二进制数据.下面让我们通过实例来讲解.

First we look at how to create a blob message. In this example we’ll assume that alarge file already exists on a shared website, so we have to create a blob message tonotify any interested consumers that it exists, as shown:

首先,我们来看看如何创建一个 二进制消息 .在下面这个例子中,我们假设在一个共享的站点上存在一个大尺寸的文件.因此,我们需要创建一个 二进制消息 通过所有感兴趣的消费者该文件已经存在了,代码如下所示:

import org.apache.activemq.BlobMessage;
...
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory =new ActiveMQConnectionFactory(brokerURI);
Connection connection = connectionFactory.createConnection();
connection.start();
 
ActiveMQSession session = (ActiveMQSession)
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue destination = session.createQueue(QUEUE_NAME);
 
MessageProducer producer = session.createProducer(destination);
BlobMessage message = session.createBlobMessage(new URL("http://example.com/bigfile.dat"));
producer.send(message);

In the example, we create a JMS connection, and from that an ActiveMQ session whichhas methods to support blob messages. We create a blob message from the URL of thefile on our shared site (http://example.com) and send the blob message on a wellknownqueue (QUEUE_NAME).
Here’s the corresponding message consumer for blob messages:

上面的示例代码中,我们创建了一个JMS连接,并且ActiveMQ的session的一个方法支持创建 二进制消息 .我们使用共享站点(http://example.com)上一个文件的URL创建了一个二进制消息,并将这个 二进制消息 发送到一知名的消息队列(QUEUE_NAME)上.
下面是相应的消息消费者接收 二进制消息 代码:

import org.apache.activemq.BlobMessage;
...
// destination of our Blob data
FileOutputStream out = new FileOutputStream("blob.dat");
String brokerURI = ActiveMQConnectionFactory.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory =new ActiveMQConnectionFactory(brokerURI);
Connection connection = (ActiveMQConnection)
connectionFactory.createConnection();
connection.start();
Session session =connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue destination = session.createQueue(QUEUE_NAME);
MessageConsumer consumer = session.createConsumer(destination);
BlobMessage blobMessage = (BlobMessage) consumer.receive();
InputStream in = blobMessage.getInputStream();
 
// now write the file from ActiveMQ
byte[] buffer = new byte[1024];
 
while (true) 
{
	int bytesRead = in.read(buffer);
	if (bytesRead == -1)
	{
		break;
	}
	out.write(buffer, 0, bytesRead);
	}
out.close();

In the example we create a message consumer on our well-known queue(QUEUE_NAME). We assume that all messages sent to this queue are of typeorg.apache.activemq.BlobMessage. A blob message has a helper method to get anInputStream to the remote URL that the message producer created the blob messagewith. We grab the InputStream and use it to read the remote file and write it to a localdisk, called blob.dat.

在上面的示例代码中,我们为公共消息队列(QUEUE_NAME)创建了消息消费者.我们假设发送到这个队列的消息都是org.apache.activemq.BlobMessage类型的.二进制消息有一个辅助方法可以获取一个基于远程URL的InputStream,该URL也是消息生产者创建消息时使用的URL.通过这InputStream就可以读取远程文件并写入到本地磁盘文件blob.dat.

Using blob messages is more robust than stream messages, as each one is anatomic unit of work. But they do rely on an external server being available for storageof the actual data—in this example a file.

使用 二进制消息 比使用流消息更加健壮,尽管二者都是自动工作的,但是两者都依赖外部服务器来存储实际要发送的数据–在本例中数据是文件.

赞 赏

   微信赞赏  支付宝赞赏


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

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

12.4 使用 二进制消息 暂无评论

发表评论

快捷键:Ctrl+Enter