activeMQ文件传输

这个方法还有待研究,目前还有如下几个疑点: 
1. ActiveMQ 报出这样的信息:

Java代码   收藏代码
  1. INFO | Usage Manager memory limit (1048576) reached for topic://EXCHANGE.FILE. Producers will be throttled to the rate at which messages are removed from this  
  2. destination to prevent flooding it. See http://activemq.apache.org/producer-flow-control.html for more info  

 

2. 这种以异步方式传送资料,能保证客户端能以正确的顺序接收到文件段麽?

 

使用ActiveMQ传送文件,发送端必须将文件拆成一段一段,每段封装在独立的Message中,逐次发送到客户端。例如下面的例子,Producer通过发送命令,告诉文件传送的开始,发送中,结束。客户端接收到这些命令之后,就知道如何接收资料了。

客户端收到内容后,根据命令将内容合并到一个文件中。  

 

Java代码   收藏代码
  1. package org.apache.activemq.exchange.file;  
  2.   
  3. import java.io.BufferedOutputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import javax.jms.Connection;  
  8. import javax.jms.ConnectionFactory;  
  9. import javax.jms.Destination;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.Message;  
  12. import javax.jms.MessageConsumer;  
  13. import javax.jms.Session;  
  14. import javax.jms.StreamMessage;  
  15.   
  16. import org.apache.activemq.ActiveMQConnectionFactory;  
  17.   
  18. public class Consumer {  
  19.   
  20.     /** 
  21.      * @param args 
  22.      */  
  23.     public static void main(String[] args) throws JMSException, IOException {  
  24.         ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
  25.   
  26.         Connection connection = factory.createConnection();  
  27.         connection.start();  
  28.   
  29.         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  30.   
  31.         Destination destination = session.createTopic("EXCHANGE.FILE");  
  32.   
  33.         MessageConsumer consumer = session.createConsumer(destination);  
  34.   
  35.         boolean appended = false;  
  36.         try {  
  37.             while (true) {  
  38.                 Message message = consumer.receive(5000);  
  39.                 if (message == null) {  
  40.                     continue;  
  41.                 }  
  42.   
  43.                 if (message instanceof StreamMessage) {  
  44.                     StreamMessage streamMessage = (StreamMessage) message;  
  45.                     String command = streamMessage.getStringProperty("COMMAND");  
  46.                       
  47.                     if ("start".equals(command)) {  
  48.                         appended = false;  
  49.                         continue;  
  50.                     }  
  51.   
  52.                     if ("sending".equals(command)) {  
  53.                         byte[] content = new byte[4096];  
  54.                         String file_name = message.getStringProperty("FILE_NAME");  
  55.                         BufferedOutputStream bos = null;  
  56.                         bos = new BufferedOutputStream(new FileOutputStream("c:/" + file_name, appended));  
  57.                         if (!appended) {  
  58.                             appended = true;  
  59.                         }  
  60.                         while (streamMessage.readBytes(content) > 0) {  
  61.                             bos.write(content);  
  62.                         }  
  63.                         bos.close();  
  64.                         continue;  
  65.                     }  
  66.   
  67.                     if ("end".equals(command)) {  
  68.                         appended = false;  
  69.                         continue;  
  70.                     }  
  71.                 }  
  72.             }  
  73.         } catch (JMSException e) {  
  74.             throw e;  
  75.         } finally {  
  76.             if (connection != null) {  
  77.                 connection.close();  
  78.             }  
  79.         }  
  80.   
  81.     }  
  82.   
  83. }  

 

发送端将文件分包,逐次发送到客户端  

Java代码   收藏代码
  1. package org.apache.activemq.exchange.file;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. import javax.jms.Connection;  
  8. import javax.jms.ConnectionFactory;  
  9. import javax.jms.Destination;  
  10. import javax.jms.JMSException;  
  11. import javax.jms.MessageProducer;  
  12. import javax.jms.Session;  
  13. import javax.jms.StreamMessage;  
  14.   
  15. import org.apache.activemq.ActiveMQConnectionFactory;  
  16.   
  17. public class Publisher {  
  18.   
  19.     public static String FILE_NAME = "01.mp3";  
  20.       
  21.     public static void main(String[] args) throws JMSException, IOException {  
  22.         ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");  
  23.         Connection connection = factory.createConnection();  
  24.         connection.start();  
  25.         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  26.         Destination destination = session.createTopic("EXCHANGE.FILE");          
  27.         MessageProducer producer = session.createProducer(destination);  
  28.         long time = System.currentTimeMillis();  
  29.           
  30.         //通知客户端开始接受文件  
  31.         StreamMessage message = session.createStreamMessage();  
  32.         message.setStringProperty("COMMAND""start");  
  33.         producer.send(message);  
  34.           
  35.         //开始发送文件  
  36.         byte[] content = new byte[4096];  
  37.         InputStream ins = Publisher.class.getResourceAsStream(FILE_NAME);  
  38.         BufferedInputStream bins = new BufferedInputStream(ins);  
  39.         while (bins.read(content) > 0) {  
  40.             //  
  41.             message = session.createStreamMessage();  
  42.             message.setStringProperty("FILE_NAME", FILE_NAME);  
  43.             message.setStringProperty("COMMAND""sending");  
  44.             message.clearBody();  
  45.             message.writeBytes(content);  
  46.             producer.send(message);  
  47.         }  
  48.         bins.close();  
  49.         ins.close();  
  50.           
  51.         //通知客户端发送完毕  
  52.         message = session.createStreamMessage();  
  53.         message.setStringProperty("COMMAND""end");  
  54.         producer.send(message);  
  55.           
  56.         connection.close();  
  57.           
  58.         System.out.println("Total Time costed : " + (System.currentTimeMillis() - time) + " mili seconds");  
  59.     }  
  60. }  

猜你喜欢

转载自blog.csdn.net/cs123456789dn/article/details/18268429