君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理

jms提供者为ActiveMQ

 

Java代码  收藏代码
  1. import java.util.Map;  
  2. import java.util.UUID;  
  3.   
  4. import javax.jms.JMSException;  
  5. import javax.jms.Message;  
  6. import javax.jms.Session;  
  7. import javax.jms.TextMessage;  
  8.   
  9. import org.apache.activemq.command.ActiveMQQueue;  
  10. import org.slf4j.Logger;  
  11. import org.slf4j.LoggerFactory;  
  12. import org.springframework.beans.factory.annotation.Autowired;  
  13. import org.springframework.jms.core.JmsTemplate;  
  14. import org.springframework.jms.core.MessageCreator;  
  15. import org.springframework.stereotype.Component;  
  16.   
  17. /** 
  18.  * mq通用类 
  19.  *  
  20.  * @author Fu Wei 
  21.  *  
  22.  */  
  23. @Component  
  24. public class ActiveMQQueueCommon {  
  25.     private static final Logger LOG = LoggerFactory.getLogger(ActiveMQQueueCommon.class);  
  26.   
  27.     @Autowired  
  28.     private JmsTemplate jmsTemplate;  
  29.   
  30.     /** 
  31.      * 异步发送 不支持特定消息 
  32.      *  
  33.      * @param reqQueue 
  34.      * @param text 
  35.      */  
  36.     public void asyncSend(ActiveMQQueue reqQueue, final String text) {  
  37.         LOG.debug("发送的XML文内容:{}", text);  
  38.         final String correlationId = UUID.randomUUID().toString();  
  39.         jmsTemplate.send(reqQueue, new MessageCreator() {  
  40.             public Message createMessage(Session session) throws JMSException {  
  41.                 TextMessage msg = session.createTextMessage(text);  
  42.                 msg.setJMSCorrelationID(correlationId);  
  43.                 return msg;  
  44.             }  
  45.         });  
  46.     }  
  47.   
  48.     /** 
  49.      * 异步发送,关联消息id 
  50.      *  
  51.      * @param reqQueue 
  52.      * @param text 
  53.      * @param propertyName 
  54.      * @param propertyValue 支持一个特定消息 
  55.      */  
  56.     public void asyncSend(ActiveMQQueue reqQueue, final String text, final String propertyName,  
  57.             final String propertyValue) {  
  58.         LOG.debug("发送的XML文内容:{}", text);  
  59.         final String correlationId = UUID.randomUUID().toString();  
  60.         jmsTemplate.send(reqQueue, new MessageCreator() {  
  61.             public Message createMessage(Session session) throws JMSException {  
  62.                 TextMessage msg = session.createTextMessage(text);  
  63.                 msg.setJMSCorrelationID(correlationId);  
  64.                 msg.setStringProperty(propertyName, propertyValue);  
  65.                 return msg;  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     /** 
  71.      * 异步发送,关联消息id 
  72.      *  
  73.      * @param reqQueue 
  74.      * @param text 
  75.      * @param propertyName 
  76.      * @param propertyMap 选择器参数 
  77.      */  
  78.     public void asyncSend(ActiveMQQueue reqQueue, final String text, final String propertyName,  
  79.             final Map<String, String> propertyMap) {  
  80.         LOG.debug("发送的XML文内容:{}", text);  
  81.         final String correlationId = UUID.randomUUID().toString();  
  82.         jmsTemplate.send(reqQueue, new MessageCreator() {  
  83.             public Message createMessage(Session session) throws JMSException {  
  84.                 TextMessage msg = session.createTextMessage(text);  
  85.                 msg.setJMSCorrelationID(correlationId);  
  86.                 if (propertyMap != null && propertyMap.size() > 0) {  
  87.                     for (Map.Entry<String, String> map : propertyMap.entrySet()) {  
  88.                         msg.setStringProperty(map.getKey(), map.getValue());  
  89.                     }  
  90.                 }  
  91.                 return msg;  
  92.             }  
  93.         });  
  94.     }  
  95.   
  96.     /** 
  97.      * 同步发送,不带消息特定属性 
  98.      *  
  99.      * @param reqQueue 
  100.      * @param resQueue 
  101.      * @param messText 
  102.      * @param timeout 
  103.      * @return 
  104.      * @throws JMSException 
  105.      */  
  106.     public String syncSend(ActiveMQQueue reqQueue, final ActiveMQQueue resQueue, final String messText, long timeout) {  
  107.         return syncSend(reqQueue, resQueue, messText, timeout, null);  
  108.     }  
  109.   
  110.     /** 
  111.      * 同步发送,带消息特定属性 
  112.      *  
  113.      * @param reqQueue 
  114.      * @param resQueue 
  115.      * @param messText 
  116.      * @param timeout 
  117.      * @param propertyName 
  118.      * @param propertyValue 
  119.      * @return 
  120.      * @throws JMSException 
  121.      */  
  122.     public String syncSend(ActiveMQQueue reqQueue, final ActiveMQQueue resQueue, final String messText, long timeout,  
  123.             final Map<String, String> propertyMap) {  
  124.         LOG.debug("转发的消息:{}, 超时时间:{}", messText, timeout);  
  125.         final String correlationId = UUID.randomUUID().toString();  
  126.         jmsTemplate.send(reqQueue, new MessageCreator() {  
  127.             public Message createMessage(Session session) throws JMSException {  
  128.                 TextMessage msg = session.createTextMessage(messText);  
  129.                 msg.setJMSReplyTo(resQueue);  
  130.                 msg.setJMSCorrelationID(correlationId);  
  131.                 // 添加消息特定属性  
  132.                 if (propertyMap != null && propertyMap.size() > 0) {  
  133.                     for (Map.Entry<String, String> map : propertyMap.entrySet()) {  
  134.                         msg.setStringProperty(map.getKey(), map.getValue());  
  135.                     }  
  136.                 }  
  137.                 return msg;  
  138.             }  
  139.         });  
  140.         jmsTemplate.setReceiveTimeout(timeout * 1000);  
  141.         TextMessage recvMsg = (TextMessage) jmsTemplate.receiveSelected(resQueue, "JMSCorrelationID = '"  
  142.                 + correlationId + "'");  
  143.         String recvMessText = null;  
  144.         try {  
  145.             recvMessText = recvMsg.getText();  
  146.         } catch (JMSException e) {  
  147.             LOG.error("jms错误", e);  
  148.         }  
  149.         LOG.debug("propertyMap: {}, 返回的信息:{}", propertyMap, recvMessText);  
  150.         return recvMessText;  
  151.     }  
  152. }  
 http://virusfu.iteye.com/blog/1336522
posted on 2013-07-01 13:31  刺猬的温驯  阅读(305)  评论(0)    收藏  举报