idea下ActiveMQ+Spring的簡單demo

本文使用的是intellij idea工具。spring

下面主要介紹一個簡單的activeMQ的demo。apache

本文主要是spring風格的實現。網絡

先來看下包結構:jsp

 

 

 

demo包含一個生產者,一個消費者,一個包含main方法的ActiveMaDemo類執行程序,還有一個自定義的類MyMessage做爲消息傳遞的類型。全部的bean配置都放在beans.xml。tcp

各個類的代碼以下:ide

 1 public class Producer {
 2     private JmsTemplate template;
 3     private Destination destination;
 4 
 5     public void produce(MyMessage message){
 6         template.convertAndSend(destination, message);
 7     }
 8 
 9     public JmsTemplate getTemplate() {
10         return template;
11     }
12 
13     public void setTemplate(JmsTemplate template) {
14         this.template = template;
15     }
16 
17     public Destination getDestination() {
18         return destination;
19     }
20 
21     public void setDestination(Destination destination) {
22         this.destination = destination;
23     }
24 }
 1 public class Consumer {
 2     private int num;
 3 
 4     public void consume(MyMessage message) {
 5         System.out.println(num+"號消費"+message);
 6     }
 7 
 8     public int getNum() {
 9         return num;
10     }
11 
12     public void setNum(int num) {
13         this.num = num;
14     }
15 }
 1 public class MyMessage implements Serializable {
 2     private static final long serialVersionUID = -4360789240260037588L;
 3 
 4     private String text;
 5 
 6     public String getText() {
 7         return text;
 8     }
 9 
10     public void setText(String text) {
11         this.text = text;
12     }
13 
14     public String toString(){
15         return text;
16     }
17 }
 1 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
 2 import org.springframework.context.ApplicationContext;
 3 
 4 public class ActiveMqDemo {
 5 
 6     public static void main(String[] args){
 7         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 8         Producer producer = (Producer) context.getBean("producer");
 9         for(int i=0;i<10;i++){
10             MyMessage myMessage = new MyMessage();
11             System.out.println("生產第"+i+"條消息!");
12             myMessage.setText("第"+i+"條消息!");
13             producer.produce(myMessage);
14         }
15     }
16 }

pom須要的依賴以下:函數

<dependency>
          <groupId>org.apache.activemq</groupId>
          <artifactId>activemq-all</artifactId>
          <version>5.14.5</version>
</dependency>

接下來看下配置文件工具

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5        http://www.springframework.org/schema/beans/spring-beans.xsd
 6        http://activemq.apache.org/schema/core
 7        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
 8 
 9     <!--ActiveMQ 鏈接工廠方法1-->
10     <!--<amq:connectionFactory id="jmsConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" />-->
11 
12     <!-- ActiveMQ 鏈接工廠方法2 -->
13     <!-- 真正能夠產生Connection的ConnectionFactory,由對應的 JMS服務廠商提供-->
14     <!-- 若是鏈接網絡:tcp://ip:61616;未鏈接網絡:tcp://localhost:61616 以及用戶名,密碼-->
15     <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
16         <property name="brokerURL" value="tcp://localhost:61616" />
17         <property name="userName" value="admin" />
18         <property name="password" value="admin" />
19         <property name="trustedPackages">
20             <list>
21                 <value>demo</value>
22             </list>
23         </property>
24     </bean>
25 
26     <!--定義消息隊列方法1-->
27     <!--<amq:queue name="destination" physicalName="queue" />-->
28 
29     <!--定義消息隊列方法2-->
30     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
31         <constructor-arg>
32             <value>queue</value>
33         </constructor-arg>
34     </bean>
35 
36     <!-- 定義JmsTemplate-->
37     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
38         <property name="connectionFactory" ref="jmsConnectionFactory"/>
39     </bean>
40 
41     <!--定義生產者-->
42     <bean id="producer" class="demo.Producer">
43         <property name="template" ref="jmsTemplate" />
44         <property name="destination" ref="destination"/>
45     </bean>
46 
47     <!--定義消費者-->
48     <bean id="consumer" class="demo.Consumer">
49         <property name="num" value="1"/>
50     </bean>
51 
52     <!--定義監聽器-->
53     <bean id="mqListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
54         <constructor-arg ref="consumer" />
55         <property name="defaultListenerMethod" value="consume" />
56     </bean>
57 
58     <!--定義監聽器容器-->
59     <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
60         <property name="connectionFactory" ref="jmsConnectionFactory" />
61         <property name="destination" ref="destination" />
62         <property name="messageListener" ref="mqListener" />
63     </bean>
64 </beans>

其中 jmsConnectionFactory 的 trustedPackages 屬性用來把我自定義的MyMessage添加到白名單,由於ActiveMQ不認識這個類型~, 同時,在自定義傳遞消息的類時要注意實現序列化,否則會報錯。this

接下來運行 ActiveMqDemo 的main函數就好了,結果以下:idea

 

 同時,打開http://localhost:8161/admin/queues.jsp,咱們也能夠看到ActiveMQ的消息產生和消費狀況。

完!

相關文章
相關標籤/搜索