今天研究了一天的ActiveMQ, 可是一上午无论如何测试,都停留在只能发送文件的链接,而不是文件数据本身,ActiveMQ需要有第三方的数据占存机制来保存数据。
在下午,与一位CSDN上的activeMQ高手郑广鹏一起研究一下,找了一些资料,终于把这一块研究出来一半,需要还有问题,但问题集中在了RESTful ,jetty的问题上了,尝试传输了一个30MB大的视频文件,成功。
下面是我找到的资料的链接:
http://www.nabble.com/A-problem-with-blobmessage-on-activemq-5.0-td15523989s2354.html
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQQueue;
public class Sending {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long a1 = System.currentTimeMillis();
/*
* First you must tell how the Blob repository can be found.
* Use ActiveMQ 5.1.0+ as broker because the fileserver webapp works there out of the box.
*/
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
“tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8161/fileserver/“);
ActiveMQConnection conn = null;
ActiveMQSession session = null;
try {
conn = (ActiveMQConnection)cf.createConnection();
session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = new ActiveMQQueue(”Queue”);
MessageProducer producer = session.createProducer(destination);
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!
* very important. If it is set to true (default) the uploader is lost in translation ![]()
* !!!!!!!!!!!!!!!!!!!!!!!!!
*/
conn.setCopyMessageOnSend(false);
File file = File.createTempFile(”amq-data-file-”, “.dat”);
// lets write some data
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append(”Hello World!”);
writer.close();
/*
* i have used an simple ascii file instead of the picture you want to use
*/
BlobMessage message = session.createBlobMessage(file);
/*
* should only work if you receive the message
*/
//System.out.println(message.getInputStream());
producer.send(message);
/*
* After all you can see a new file under $Brokerlocation$/webapps/fileserver/
*/
} catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
} catch (JMSException ex) {
}
}
a1 = System.currentTimeMillis() - a1;
System.out.println(a1);
}
}


Leave a reply