12.4 使用二进制消息
12.4 Blob messages
12.4 使用二进制消息
?
ActiveMQ introduced the concept of blob messages so that users can take advantage of
ActiveMQ message delivery semantics (transactions, load balancing, and smart routing)
in conjunction with very large messages. A blob message doesn’t contain the data
being sent, but is a notification that a blob (binary large object) is available. The blob
itself is transferred out of bounds, by either FTP or HTTP. In fact, an ActiveMQ
BlobMessage only contains the URL to the data itself, with a helper method to grab an
InputStream 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 a
large file already exists on a shared website, so we have to create a blob message to
notify 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 which
has methods to support blob messages. We create a blob message from the URL of the
file on our shared site (http://example.com) and send the blob message on a wellknown
queue (QUEUE_NAME).
?
上面的示例代码中,我们创建了一个JMS连接,并且ActiveMQ的session的一个方法支持创建二进制消息.
我们使用共享站点(http://example.com)上一个文件的URL创建了一个二进制消息,并将这个二进制消息
发送到一知名的消息队列(QUEUE_NAME)上.
?
Here’s the corresponding message consumer for blob messages:
下面是相应的消息消费者接收二进制消息代码:
?
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 type
org.apache.activemq.BlobMessage. A blob message has a helper method to get an
InputStream to the remote URL that the message producer created the blob message
with. We grab the InputStream and use it to read the remote file and write it to a local
disk, 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 an
atomic unit of work. But they do rely on an external server being available for storage
of the actual data—in this example a file.
?
使用二进制消息比使用流消息更加健壮,尽管二者都是自动工作的,但是两者都依赖外部服务器
来存储实际要发送的数据--在本例中数据是文件.