在所有新的 Spring 2.0 特性和改進中,我必須承認訊息驅動的 POJO 是我個人最喜歡的一個。我感覺很多其他 Spring 使用者也會有同感。
在這裡我提供一個快速介紹。還有很多內容可以展示,我將在後續帖子中跟進。不過現在,這應該能為你提供足夠的資訊,以便使用一些真正的基於 POJO 的非同步 JMS 快速上手!我希望你們和我一樣對此感到興奮 ;)
先決條件
你的 classpath 中需要包含以下 JAR 檔案。我也列出了我使用的版本(任何 spring-2.x 版本應該都可以。事實上我剛剛在兩分鐘前把 RC3 放了進去)
- activemq-core-3.2.2.jar
- concurrent-1.3.4.jar
- geronimo-spec-j2ee-managment-1.0-rc4.jar
- commmons-logging-1.0.4.jar
- log4j-1.2.9.jar
- jms-1.1.jar
- spring-2.0-rc3.jar
設定環境
首先,我們需要設定環境。我將使用 ActiveMQ,但更換提供者的影響將僅限於修改此檔案。我將此檔案命名為“shared-context.xml”,因為正如你很快會看到的,我將為 JMS 通訊的兩端匯入這些 bean 定義。以下是“共享”的 bean 定義:連線工廠和兩個佇列(一個用於請求,一個用於回覆)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="requestQueue" class="org.activemq.message.ActiveMQQueue">
<constructor-arg value="requestQueue"/>
</bean>
<bean id="replyQueue" class="org.activemq.message.ActiveMQQueue">
<constructor-arg value="replyQueue…