spring 使用 ActiveMQ,JMS使用

 MS 定義了兩種方式:Quere(點對點);Topic(發佈/訂閱)。

ConnectionFactory 是連接工廠,負責創建Connection。

Connection 負責創建 Session。

Session 創建 MessageProducer(用來發消息) 和 MessageConsumer(用來接收消息)。

Destination 是消息的目的地。

 

下載 apache-activemq-5.3.0。http://activemq.apache.org/download.html,解壓,

然後雙擊 bin/activemq.bat。運行後,可以在 http://localhost:8161/admin 觀察。

也有 demo, http://localhost:8161/demo。把 activemq-all-5.3.0.jar 加入 classpath。

 

一  ActiveMQ的jms寫法 

  1  消息發送類  

    

Java代碼  收藏代碼
  1. public class JMSsenderBean {  
  2.     public static void main(String[] args) throws Exception {    
  3.         //連接工廠。連接工廠(ConnectionFactory)是由管理員創建,並綁定到JNDI樹中。客戶端使用JNDI查找連接工廠,然後利用連接工廠創建一個JMS連接。  
  4.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");    
  5.         // JMS連接。JMS連接(Connection)表示JMS客戶端和服務器端之間的一個活動的連接,是由客戶端通過調用連接工廠的方法建立的    
  6.         Connection connection = connectionFactory.createConnection();  
  7.         //開啓連接  
  8.         connection.start();    
  9.         Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);  
  10.         Destination destination = session.createQueue("my-queue");    
  11.         MessageProducer producer = session.createProducer(destination);    
  12.         for(int i=0; i<3; i++) {    
  13.             MapMessage message = session.createMapMessage();    
  14.             message.setLong("count"new Date().getTime());    
  15.             Thread.sleep(1000);  
  16.             //通過消息生產者發出消息    
  17.             producer.send(message);    
  18.         }    
  19.         session.commit();    
  20.         session.close();    
  21.         connection.close();    
  22.     }    
  23.  }  

 

  2 消息接收類

  

Java代碼  收藏代碼
  1. import javax.jms.Connection;  
  2. import javax.jms.ConnectionFactory;  
  3. import javax.jms.Destination;  
  4. import javax.jms.MapMessage;  
  5. import javax.jms.MessageConsumer;  
  6. import javax.jms.Session;  
  7.   
  8. import org.apache.activemq.ActiveMQConnectionFactory;  
  9. //消息接收類   
  10. public class JMSreceive {  
  11.     public static void main(String[] args) throws Exception {    
  12.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");    
  13.         
  14.         Connection connection = connectionFactory.createConnection();    
  15.         connection.start();    
  16.         
  17.         final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);    
  18.         Destination destination = session.createQueue("my-queue");    
  19.         MessageConsumer consumer = session.createConsumer(destination);    
  20.         /*//listener 方式  
  21.         consumer.setMessageListener(new MessageListener() {  
  22.       
  23.             public void onMessage(Message msg) {  
  24.                 MapMessage message = (MapMessage) msg;  
  25.                 //TODO something....  
  26.                 System.out.println("收到消息:" + new Date(message.getLong("count")));  
  27.                 session.commit();  
  28.             }  
  29.         });  
  30.         Thread.sleep(30000);  
  31.         */    
  32.         int i=0;    
  33.         while(i<3) {    
  34.             i++;    
  35.             MapMessage message = (MapMessage) consumer.receive();    
  36.             session.commit();    
  37.             System.out.println("收到消息:" + new Date(message.getLong("count")));    
  38.         }    
  39.         
  40.         session.close();    
  41.         connection.close();    
  42.     }    
  43. }  

 

 

二 spring集成

  

   1 spring 配置文件

Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  3.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">    
  4.     
  5. <!-- 在非 web / ejb 容器中使用 pool 時,要手動 stop,spring 不會爲你執行 destroy-method 的方法    
  6.     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">    
  7.         <property name="connectionFactory">    
  8.             <bean class="org.apache.activemq.ActiveMQConnectionFactory">    
  9.                 <property name="brokerURL" value="tcp://localhost:61616" />    
  10.             </bean>    
  11.         </property>    
  12.     </bean>    
  13. -->    
  14.     <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">    
  15.         <property name="brokerURL" value="tcp://localhost:61616" />    
  16.     </bean>   
  17.   
  18.     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">    
  19.         <constructor-arg index="0" value="my-queue" />    
  20.     </bean>    
  21.       
  22.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">    
  23.         <property name="connectionFactory" ref="jmsFactory" />    
  24.         <property name="defaultDestination" ref="destination" />    
  25.         <property name="messageConverter">    
  26.             <bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />    
  27.         </property>    
  28.     </bean>    
  29. </beans>   

 

 2   消息發送類

Java代碼  收藏代碼
  1. import java.util.Date;  
  2.   
  3. import javax.jms.JMSException;  
  4. import javax.jms.MapMessage;  
  5. import javax.jms.Session;  
  6. import javax.mail.Message;  
  7.   
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  10. import org.springframework.jms.core.JmsTemplate;  
  11. import org.springframework.jms.core.MessageCreator;  
  12.   
  13. public class JMSsenderBean_Spring {  
  14.     public static void main(String[] args) {    
  15.         ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:spring/applicationContext-jmx.xml");    
  16.         
  17.         JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");    
  18.         
  19.         jmsTemplate.send(new MessageCreator() {    
  20.         
  21.             public MapMessage createMessage(Session session) throws JMSException {   
  22.                   
  23.                 MapMessage mm = session.createMapMessage();    
  24.                 mm.setLong("count"new Date().getTime());  
  25.                   
  26.                 return mm;    
  27.             }    
  28.         
  29.         });    
  30.     }     
  31.  }  

 3  消息接收類

  

Java代碼  收藏代碼
  1. import java.util.Date;  
  2. import java.util.Map;  
  3.   
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  6. import org.springframework.jms.core.JmsTemplate;  
  7.   
  8. public class JMSreceive_Spring {  
  9.     public static void main(String[] args) {    
  10.         ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:spring/applicationContext-jmx.xml");    
  11.         
  12.         JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");    
  13.         while(true) {    
  14.             Map<String, Object> mm =  (Map<String, Object>) jmsTemplate.receiveAndConvert();    
  15.             System.out.println("收到消息:" + new Date((Long)mm.get("count")));  
  16.               
  17.         }    
  18.     }   
  19. }  
發佈了6 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章