轉自:http://www.g4studio.org/thread-880-1-1.html
ActiveMQ與Spring線程池的整合在企業級開發中,很多系統間的通信,特別是與外部系統間的通信,往往都是異步的,JMS便是J2EE應用程序中用於處理異步消息傳遞的接口。爲了提高對外部消息的相應,服務器程序中往往利用線程技術來處理接收的消息,線程池的意義在於對這樣的一個並行處理機制進行性能上的優化。爲了迅速切入正體,這裏就不多涉及JMS的內容與池的概念。僅對如何進行ActiveMQ與Spring線程池整合做較爲詳細的描述。
引用:
ActiveMQ與Spring線程池整合實例
CSDN資源下載 僅有47k
整合步驟
這裏我按照配置流程逐點來描述進行配置的方法。其中MQ整合配置的過程中各bean之間的關係比較多,也比較暈,我用橙紅色將他們標記出來,關注標記的幾點是十分重要的。
- 讓Spring支持ActiveMQ的配置語法
首先我們需要在applicationContext.xml中引入ActiveMQ的配置語法。
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:amq="http://activemq.org/config/1.0"
- 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.xsd
- http://activemq.org/config/1.0 http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd">
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.org/config/1.0" 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.xsd http://activemq.org/config/1.0 http://people.apache.org/reposit ... ubator-SNAPSHOT.xsd">
由於是SnapShot版本,那個XSD有部分錯誤,我們這裏使用的是自行修改過的XSD,之後將activemq-core-4.1-incubator-SNAPSHOT.xsd文件放入META-INF文件夾下。並且新建spring的自定義scheam的配置文件spring.schemas:
- http://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd=/activemq-core-4.1-incubator-SNAPSHOT.xsd
http\://people.apache.org/repository/org.apache.activemq/xsds/activemq-core-4.1-incubator-SNAPSHOT.xsd=/activemq-core-4.1-incubator-SNAPSHOT.xsd這樣在應用程序上下文中就可以解析ActiveMQ的配置語法了。
注:實例中是3個文件的配置方法,只不過這3個文件是由activemq-core-4.1-incubator-SNAPSHOT.xsd分解開來的,spring.schemas中也需要將這3個文件都配置進去。
- 配置ActiveMQ基礎部件
爲了使ActiveMQ能夠正常運行起來,我們需要在ApplicationContext.xml中進行配置。
- //配置ActiveMQ Broker
- <amq:broker useJmx="false" persistent="false">
- <amq:destinations>
- <amq:queue id="jms.log" physicalName="pams.amq" />
- </amq:destinations>
- <amq:transportConnectors>
- <amq:transportConnector uri="tcp://localhost:61616" />
- </amq:transportConnectors>
- </amq:broker>
- //配置ConnectionFactory
- <amq:connectionFactory id="jmsConnectionFactory"
- brokerURL="vm://localhost:61616" />
- //配置Queue
- <amq:queue name="destination" physicalName="pams.amq" />
//配置ActiveMQ Broker<amq:broker useJmx="false" persistent="false"> <amq:destinations> <amq:queue id="jms.log" physicalName="pams.amq" /> </amq:destinations> <amq:transportConnectors> <amq:transportConnector uri="tcp://localhost:61616" /> </amq:transportConnectors> </amq:broker>//配置ConnectionFactory <amq:connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost:61616" />//配置Queue <amq:queue name="destination" physicalName="pams.amq" />在這裏使用了內嵌JVM這種最簡單的模式,這樣在Spring初始化時ActiveMQ便加載了。
對於ActiveMQ的配置,我們還需要配置消息生產者、消息消費者和消息轉換者等等部分。不過,在此之前我們需要先將線程池配置進來,之後再進行ActiveMQ餘下的配置。
- 配置Spring線程池
對於線程池的配置,我們需要配置一個線程池執行器。首先看下配置代碼:
- <!-- ThreadPool Executor -->
- <bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
- <!-- <property name="threadFactory" ref="threadFactory" /> -->
- <property name="corePoolSize" value="2" />
- <property name="maxPoolSize" value="4"/>
- <property name="queueCapacity" value="500"/>
- <property name="keepAliveSeconds" value="300"/>
- <FONT color=red>
- <property name="rejectedExecutionHandler">
- <bean class="java.util.concurrent.ThreadPoolExecutor$DiscardOldestPolicy"/>
- </property>
- </FONT>
- </bean>
<!-- ThreadPool Executor --> <bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- <property name="threadFactory" ref="threadFactory" /> --> <property name="corePoolSize" value="2" /> <property name="maxPoolSize" value="4"/> <property name="queueCapacity" value="500"/> <property name="keepAliveSeconds" value="300"/> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$DiscardOldestPolicy"/> </property> </bean>這裏同樣僅做了一個簡單的配置,需要注意的是對rejectedExecutionHandler屬性的配置,這裏使用了JDK1.5中用於被拒絕任務的處理機制,在JDK1.5中有4種處理機制,都可以在這裏以這種方式配置使用。
- 消息轉換器的配置
完成線程池執行器的配置後,我們需要配置一個消息轉換器(Converter),這樣在生產和消費時可以直接發送Order對象,而不是JMS的Message對象。
- //消息轉換器
- <bean id="resourceMessageConverter" class="com.jmspool.jms.ResourceMessageConverter" />
//消息轉換器<bean id="resourceMessageConverter" class="com.jmspool.jms.ResourceMessageConverter" />
- jmsTemplate的配置
之後我們需要配置一個由Spring提供的Template,其綁定ConnectionFactory與消息轉換器(Converter)。這樣在進行消息的生產時我們可以直接使用Spring提供的Template中的方法。
- <!-- JMS ActiveMQ Config-->
- <bean id="jmsTemplate"
- class="org.springframework.jms.core.JmsTemplate">
- //連接工廠
- <property name="connectionFactory">
- <bean
- class="org.springframework.jms.connection.SingleConnectionFactory">
- <property name="targetConnectionFactory"
- ref="jmsConnectionFactory" />
- </bean>
- </property>
- //消息轉換器
- <property name="messageConverter"
- ref="resourceMessageConverter" />
- </bean>
<!-- JMS ActiveMQ Config--> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> //連接工廠 <property name="connectionFactory"> <bean class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="jmsConnectionFactory" /> </bean> </property> //消息轉換器 <property name="messageConverter" ref="resourceMessageConverter" /> </bean>
- 消息生產者
消息生產者用於生產消息,也就是將消息發送出去。在這裏我們使用上面配置好的JmsTemplate來發送發送消息。這個類的代碼我就不貼出來了,大家可以在實例中查看。配置消息生產者需要JmsTemplate與消息隊列(Queue)。
- <bean id="resourceMessageProducer"
- class="com.jmspool.jms.ResourceMessageProducer" >
- <property name="template">
- <ref bean="jmsTemplate"></ref>
- </property>
- <property name="destination">
- <ref bean="destination"></ref>
- </property>
- </bean>
<bean id="resourceMessageProducer" class="com.jmspool.jms.ResourceMessageProducer" > <property name="template"> <ref bean="jmsTemplate"></ref> </property> <property name="destination"> <ref bean="destination"></ref> </property> </bean>
- 消息接收處理者
消息接收者(MDP)使用Spring的MessageListenerAdapter,指定負責處理消息的POJO及其方法名,綁定消息轉換器Converter。
- <bean id="resourceMessageListener"
- class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
- <constructor-arg>
- <ref bean="resourceMessageConsumer"></ref>
- </constructor-arg>
- <property name="defaultListenerMethod" value="addResource" />
- <property name="messageConverter" ref="resourceMessageConverter" />
- </bean>
<bean id="resourceMessageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> <constructor-arg> <ref bean="resourceMessageConsumer"></ref> </constructor-arg> <property name="defaultListenerMethod" value="addResource" /> <property name="messageConverter" ref="resourceMessageConverter" /> </bean>
- 監聽容器的配置
監聽容器的任務是負責調度MDP, 綁定connectionFactory,Queue和MDP。更重要的一點,這裏也是將線程執行器與ActiveMQ整合的切入點。先看下配置文件:
- <bean id="resourcelistenerContainer"
- class="org.springframework.jms.listener.SimpleMessageListenerContainer">
- <property name="connectionFactory" ref="jmsConnectionFactory" />
- <property name="autoStartup" value="true"/>
- <property name="concurrentConsumers" value="6"/>
- <property name="destination" ref="destination"/>
- <property name="messageListener" ref="resourceMessageListener"/>
- <property name="sessionTransacted" value="true"/>
- <FONT color=red><property name="taskExecutor" ref="threadPoolExecutor"/> </FONT>
- </bean>
<bean id="resourcelistenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="autoStartup" value="true"/> <property name="concurrentConsumers" value="6"/> <property name="destination" ref="destination"/> <property name="messageListener" ref="resourceMessageListener"/> <property name="sessionTransacted" value="true"/> <property name="taskExecutor" ref="threadPoolExecutor"/> </bean>這裏通過taskExecutor屬性將已經配置好的線程執行器threadPoolExecutor與JMS(ActiveMQ)整合起來。
- 消息消費者的配置
消息消費者是消息的最終處理部分,開始文章裏面說說的整合線程的目的,也就是爲了使得處理者能夠並行的處理接收到的消息。同樣這裏爲了簡潔也不將代碼貼出來。配置時需要綁定線程池執行器。
- <bean id="resourceMessageConsumer"
- class="com.jmspool.jms.ResourceMessageConsumer" abstract="false"
- lazy-init="default" autowire="default" dependency-check="default">
- <property name="threadPoolExecutor">
- <ref bean="threadPoolExecutor"></ref>
- </property>
- </bean>
<bean id="resourceMessageConsumer" class="com.jmspool.jms.ResourceMessageConsumer" abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="threadPoolExecutor"> <ref bean="threadPoolExecutor"></ref> </property> </bean>至此,我們的整合工作就完成了。在上述文中僅僅簡單的描述了配置的步驟,對於原理及各類的屬性意義並沒有詳細的描述。因爲網上有不少這樣的文章。
寫在最後:
我們配置的所有步驟其實都是圍繞着Spring展開的,雖然我們完成的是一個ActiveMQ與Spring線程池的整合實例,但是如果換做其他MQ中間件,此法同樣是適用的。因爲我們整合的層面是在Spring上。