## 12.4 使用 二进制消息

### 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:

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);
InputStream in = blobMessage.getInputStream();

// now write the file from ActiveMQ
byte[] buffer = new byte[1024];

while (true)
{
{
break;
}
}
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.

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.

微信赞赏  支付宝赞赏