1. 程式人生 > >spring整合apache activemq實現訊息傳送的三種方式程式碼配置例項

spring整合apache activemq實現訊息傳送的三種方式程式碼配置例項

我們專案中傳送事件告警要用到訊息佇列,所以學習了下activemq,整理如下:

activemq的介紹就不用說了,官網上大家可以詳細的看到。

1.下載並安裝activemq:地址http://activemq.apache.org/activemq-590-release.html,我下面的例子用的是5.9.0的版本。下載後解壓就完成安裝了。進入解壓目錄的bin目錄,選擇windows位數(32/64),啟動activemq.bat就可以開啟activemq服務了。登陸http://localhost:8161/admin就可以進入mq的介面了,官方預設登入名/密碼是:admin/admin,也可以在conf/jetty-realm.properties中自行修改,activemq介面如下:

簡單介紹下導航欄:

Queues:佇列方式訊息。

Topics:主題方式訊息。

Subscribers:訊息訂閱監控查詢。

Connections:檢視連結數,分別可以檢視xmpp、ssl、stomp、openwire、ws和網路連結。

Network:網路連結數監控。

Scheduled:沒有用到,不太清楚。

Send:傳送訊息資料

2.傳送和接受訊息的步驟:

 a.傳送訊息

(1)建立連線使用的工廠類JMS ConnectionFactory

(2)使用管理物件JMS ConnectionFactory建立連線Connection,並啟動

(3)使用連線Connection 建立會話Session

(4)使用會話Session和管理物件Destination建立訊息生產者MessageSender

(5)使用訊息生產者MessageSender傳送訊息

b.接收訊息

(1)建立連線使用的工廠類JMS ConnectionFactory

(2)使用管理物件JMS ConnectionFactory建立連線Connection,並啟動

(3)使用連線Connection建立會話Session

(4)使用會話Session和管理物件Destination建立訊息接收者MessageReceiver

(5)使用訊息接收者MessageReceiver接受訊息,需要用setMessageListener將MessageListener介面繫結到MessageReceiver,訊息接收者必須實現了MessageListener介面,需要定義onMessage事件方法。

3.spring的整合:spring整合所需要的jar包我已經在pom.xml中配置了,大家可以看看都需要哪些jar。當然下載的activemq的lib下也有這些jar。spring的整合比較簡單,只需在spring的配置檔案中配置訊息模板JmsTemplete就可以了,具體如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <!-- 連線池  -->
    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">  
        <property name="connectionFactory">  
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">  
                <property name="brokerURL" value="tcp://localhost:61616" />  
            </bean>  
        </property>  
    </bean>  
      
    <!-- 連線工廠 -->
    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
        <property name="brokerURL" value="tcp://localhost:61616" />  
    </bean>  
    
    <!-- 配置訊息目標 -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
        <constructor-arg index="0" value="com.zuidaima.spring" />  
    </bean>  
 
    <!-- 訊息模板 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
        <property name="connectionFactory" ref="activeMQConnectionFactory" />  
        <property name="defaultDestination" ref="destination" />  
        <property name="messageConverter">  
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter" />
        </property>  
    </bean>  
</beans>

4.專案執行截圖:

JMS

其他幾種方式我就不一一截圖了,請具體執行專案檢視。

5.開發環境:win7 32位+eclipse kepler + jdk7 + maven