activemq5.2发送和接收BlobMessage简单实例

 

  1. package com.work.activemq;  
  2.   
  3. /** 
  4.  * @author wangmingjie 
  5.  * @date 2009-7-29上午09:00:48 
  6.  */  
  7.   
  8. import java.io.File;  
  9. import java.io.IOException;  
  10.   
  11. import javax.jms.DeliveryMode;  
  12. import javax.jms.Destination;  
  13. import javax.jms.JMSException;  
  14. import javax.jms.MessageProducer;  
  15. import javax.jms.Session;  
  16.   
  17. import org.apache.activemq.ActiveMQConnection;  
  18. import org.apache.activemq.ActiveMQConnectionFactory;  
  19. import org.apache.activemq.ActiveMQSession;  
  20. import org.apache.activemq.BlobMessage;  
  21. import org.apache.activemq.command.ActiveMQQueue;  
  22.   
  23. public class BlobMessageSendTest {  
  24.     
  25.     private String user = ActiveMQConnection.DEFAULT_USER;      
  26.      
  27.     private String password = ActiveMQConnection.DEFAULT_PASSWORD;      
  28.      
  29. //    private String url = ActiveMQConnection.DEFAULT_BROKER_URL;  
  30.     private String url = "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8161/fileserver/";  
  31.      
  32.     private String subject = "Blob Queue";  
  33.      
  34.     private Destination destination = null;      
  35.      
  36.     private ActiveMQConnection connection = null;      
  37.      
  38.     private ActiveMQSession session = null;      
  39.      
  40.     private MessageProducer producer = null;      
  41.      
  42.     // 初始化      
  43.     private void initialize() throws JMSException, Exception {      
  44.         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  url);      
  45.         connection = (ActiveMQConnection)connectionFactory.createConnection();      
  46.   /* 
  47.    * !!!!!!!!!!!!!!!!!!!!!!!!! very important. If it is set to true 
  48.    * (default) the uploader is lost in translation ;) 
  49.    * !!!!!!!!!!!!!!!!!!!!!!!!! 
  50.    */  
  51.         connection.setCopyMessageOnSend(false);          
  52.         session = (ActiveMQSession)connection.createSession(false, Session.AUTO_ACKNOWLEDGE);      
  53.         destination = session.createQueue(subject);  
  54. //        destination = session.createTopic(subject);  
  55.         producer = session.createProducer(destination);      
  56.         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);      
  57.     }      
  58.      
  59.     // 发送消息      
  60.     public void produceMessage(File file) throws JMSException, Exception {      
  61.         initialize();      
  62.         BlobMessage msg = session.createBlobMessage(file);   
  63.           
  64.         connection.start();      
  65.         System.out.println("Producer:->Sending message: " + file.getName());      
  66.         producer.send(msg);      
  67.         System.out.println("Producer:->Message sent complete!");      
  68.     }      
  69.      
  70.     // 关闭连接      
  71.     public void close() throws JMSException {      
  72.         System.out.println("Producer:->Closing connection");      
  73.         if (producer != null)      
  74.             producer.close();      
  75.         if (session != null)      
  76.             session.close();      
  77.         if (connection != null)      
  78.             connection.close();      
  79.     }      
  80.       
  81.       
  82.       
  83.  /** 
  84.   * 测试成功,能够发送到服务器上。 
  85.   * @param args 
  86.   * @throws IOException 
  87.   */  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值