---
title: "JMS 使用 ActiveMQ 传送文件"
author: "cfnet"
date: "2010-04-27T17:02:24Z"
tags: []
categories: ["编程语言"]
slug: "jms-activemq"
draft: false
lastmod: "2010-04-27T17:02:24Z"
id: 654320
url: "https://cfnet.iteye.com/blog/654320"
---

# JMS 使用 ActiveMQ 传送文件

##### (2010年04月11日) 发表于 [Java博客](http://www.blogjava.net/)

这里使用的 MQ 中间件是开源的 ActiveMQ，我们没有采用 BytesMessage 来按字节传送文件，而是 ActiveMQ 为我们提供了 [org.apache.activemq.BlobMessage](http://activemq.apache.org/maven/5.3.1/activemq-core/apidocs/org/apache/activemq/BlobMessage.html)，可以用它来传送大对象。 [org.apache.activemq.ActiveMQSession](http://activemq.apache.org/maven/5.3.1/activemq-core/apidocs/org/apache/activemq/ActiveMQSession.html) 中有以下几个创建 BlobMessage 对象的方法：

createBlobMessage(URL url)  
createBlobMessage(URL url, boolean deletedByBroker)  
createBlobMessage(File file)  
createBlobMessage(InputStream in)

接收到 BlobMessage 消息后，可以调用其 getInputStream() 方法获得数据，然后写成磁盘文件，文件名、文件大小等可通过 Message 的 getXxxProperty("Property.Name") 取的。

注意，传输入文件的时候，发送方创建 ConnectionFactory 时的 brokerURL 需要指定 jms.blobTransferPolicy.uploadUrl 或者jms.blobTransferPolicy.defaultUploadUrl 属性为 ActiveMQ 中 fileserver 应用的 URI，即指定传输 BlogMessage 的  [BlobTransferPolicy](http://activemq.apache.org/maven/activemq-core/apidocs/org/apache/activemq/blob/BlobTransferPolicy.html) 策略，参看 [Configuring the BLOB Transfer Policy](http://activemq.apache.org/blob-messages.html)。

先上图看真相，包括执行 FileSender 时选择文件，FileReciever 在收到文件时会弹出保存文件的对话框，还有控制台的打印信息。

 

![](http://www.blogjava.net/images/blogjava_net/framework/jms/activemq_jms_file.jpg)

具体来看例子，使用 P2P 队列的方式，你也可以用发布/订阅的方式，只是代码中创建的 Destination 类型不同。

**1. 启动 ActiveMQ**

在 [http://activemq.apache.org/activemq-531-release.html](http://activemq.apache.org/activemq-531-release.html) 下载  ActiveMQ，然后解压，执行其中的 bin/activemq.bat，控制台可以看到：

 INFO | ActiveMQ Console at http://0.0.0.0:8161/admin  
 INFO | Initializing Spring root WebApplicationContext  
 INFO | Successfully connected to tcp://localhost:61616  
 INFO | Camel Console at http://0.0.0.0:8161/camel  
 INFO | ActiveMQ Web Demos at http://0.0.0.0:8161/demo  
 INFO | RESTful file access application at http://0.0.0.0:8161/fileserver  
 INFO | Started SelectChannelConnector@0.0.0.0:8161

表示启动成功了，假设运行 ActiveMQ 机器的 IP 地址是 10.80.38.10。

**2. 编写发送文件的程序 FileSender.java**

**3. 编写接收文件的程序 FileReceiver.java**

**4. 运行程序**

先执行 FileReceiver 程序来监听消息  
再执行发送程序 FileSender，将会提示你选择一个要传送的文件(上图左边)，确定后就会把文件发送到 ActiveMQ 服务器上  
接收端 FileReceiver 监听到有文件传过来的消息后，会自动弹出保存文件的对话框，要你选择保存位置(上图右边)，文件名能保持一致。确定后就开始接收文件，存到指定的位置。

如果设置 producer.setDeliveryMode(DeliveryMode.PERSISTENT); 消息持久性的话，发送方传文件的时候，接收方可以不在线，文件会暂存在 ActiveMQ 服务器上，等到接收程序上线后仍然可以收到发过来的文件。

参考：1. [A problem with blobmessage on activemq 5.0](http://old.nabble.com/A-problem-with-blobmessage-on-activemq-5.0-td15523989s2354.html)  
        2. [Apache ActiveMQ &gt; Features &gt; Message Features &gt; Blob Messages](http://activemq.apache.org/blob-messages.html)  
        3. [Apache ActiveMQ &gt; Features &gt; Message Dispatching Features &gt; Async Sends](http://activemq.apache.org/async-sends.html)

**\[版权声明]**本站内文章，如未特别注明，均系原创或翻译之作，本人 Unmi 保留一切权利。本站原创及译作未经本人许可，不得用于商业用途及传统媒体。网络媒体可随意转载，或以此为基础进行演译，但务必以链接形式注明原始出处和作者信息，否则属于侵权行为。另对本站转载他处文章，俱有说明，如有侵权请联系本人，本人将会在第一时间删除侵权文章。及此说明，重之之重。