整合 Apache ActiveMQ Classic 與 JBoss
連線能力 > 容器 > 整合 Apache ActiveMQ Classic 與 JBoss
整合 Apache ActiveMQ Classic 與 JBoss
在企業級 Java 環境中,與應用程式伺服器整合是很常見的情況,尤其是在訊息傳遞方面。ActiveMQ Classic 是一個符合 JMS 1.1 標準、開源、採用 Apache 授權的訊息導向中介軟體 (MOM),它擁有許多、許多超出 JMS 規範的功能。ActiveMQ Classic 提供許多不同的連線點、許多跨語言客戶端以及許多可插拔的傳輸協定,包括與任何 J2EE 1.4 應用程式伺服器的整合。
JBoss 是開源環境中的一個應用程式伺服器。一個非常常見的需求是在 JBoss 中將 ActiveMQ Classic 設定為訊息傳遞基礎架構。雖然關於這個整合已經有一些文件,但本文旨在提供更詳細的說明和解釋。因此,如果您需要將 ActiveMQ Classic 與 JBoss 整合,本文適合您。
本文說明如何設定 JBoss,以將 ActiveMQ Classic 作為其生命週期的一部分啟動,以及如何設定 ActiveMQ Classic 資源配接器,以處理 ActiveMQ Classic 和 JBoss 之間的訊息傳遞和交易。
需求
以下是本文的軟體需求,以及每個軟體的下載連結
雖然本文使用的是 Unix,但安裝和整合可以在任何執行 Sun Java 的平台上運作。建議在開始本文的步驟之前,先下載每個軟體。下載每個軟體後,請繼續執行第一個步驟。
安裝 J2SE 1.5
此過程的第一個步驟是安裝 Java 1.5,並驗證它是否正確執行。使用上面的連結,尋找、下載並安裝適合您平台的正確 Java 版本。一旦 Java 安裝完成並在 PATH 中,請使用以下命令測試它,以確認它是否正確執行
$ java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-112)
Java HotSpot(TM) Client VM (build 1.5.0_06-64, mixed mode, sharing)
根據您的平台和 Java 1.5 的確切組建編號,您的輸出可能會略有不同。只要它是 Java 1.5 且輸出版本資訊,您就應該可以繼續執行下一步。
安裝 Apache Ant
第二步是安裝 Apache Ant。只需使用上面的連結下載它,將它解壓縮到硬碟上的某個位置,然後將 bin
目錄放入 PATH 中。這將讓您可以使用以下命令快速測試它
$ ant -version
Apache Ant version 1.6.2 compiled on July 16 2004
只要您看到上面的版本輸出,Ant 應該就可以使用了。如果您沒有看到版本輸出或收到錯誤,請查閱 Ant 文件或郵件列表存檔以解決問題。請注意,Ant 在整個練習結束時才會使用,以測試整合。
安裝 JBoss 應用程式伺服器
此過程的第三步是安裝 JBoss,並確保它在安裝和設定 ActiveMQ Classic 之前正確執行。下載 JBoss-4.0.4 後,將其解壓縮到可以建立目錄的位置。接下來,使用以下命令執行伺服器
$ cd jboss-4.0.4.GA
$ ./bin/run.sh -c default
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /opt/jboss-4.0.4.GA
JAVA: java
JAVA_OPTS: -server -Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcInterval=3600000
-Dsun.rmi.dgc.server.gcInterval=3600000 -Dprogram.name=run.sh
CLASSPATH: /opt/jboss-4.0.4.GA/bin/run.jar:/lib/tools.jar
=========================================================================
15:34:47,999 INFO [Server] Starting JBoss (MX MicroKernel)...
15:34:48,001 INFO [Server] Release ID: JBoss [Zion] 4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)
15:34:48,004 INFO [Server] Home Dir: /opt/jboss-4.0.4.GA
15:34:48,005 INFO [Server] Home URL: file:/opt/jboss-4.0.4.GA/
15:34:48,007 INFO [Server] Patch URL: null
15:34:48,007 INFO [Server] Server Name: default
15:34:48,007 INFO [Server] Server Home Dir: /opt/jboss-4.0.4.GA/server/default
15:34:48,011 INFO [Server] Server Home URL: file:/opt/jboss-4.0.4.GA/server/default/
15:34:48,012 INFO [Server] Server Log Dir: /opt/jboss-4.0.4.GA/server/default/log
15:34:48,017 INFO [Server] Server Temp Dir: /opt/jboss-4.0.4.GA/server/default/tmp
15:34:48,022 INFO [Server] Root Deployment Filename: jboss-service.xml
...
15:35:17,360 INFO [Server] JBoss (MX MicroKernel) [4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)]
Started in 22s:238ms
JBoss 啟動的前幾行輸出表示正在使用的設定,但最後一行是最重要的。它告訴您 JBoss 已在您的系統上成功啟動。為了確保額外的保證,請在網頁瀏覽器中造訪 https://127.0.0.1:8080/web-console/,以確保您能夠看到 JBoss 網頁主控台。如果您可以看到此主控台,一切都應該準備就緒。
附帶一提,網頁主控台的左側需要正確安裝 Java 外掛程式。這應該在安裝 J2SE 時發生,因此如果它沒有為您正確運作,我建議您查閱一些關於您可能看到的任何錯誤的文件。JBoss 或 ActiveMQ Classic 的正常運作不需要 Java 外掛程式,它僅適用於 JBoss 網頁主控台。
一旦 JBoss 安裝正確,請使用關機指令碼或直接輸入 ctrl-c 來啟動關機掛鉤,以關閉它。一旦它關閉,請繼續執行下一步。
準備整合 Apache ActiveMQ Classic 與 JBoss 應用程式伺服器
第四步是準備您的環境,以便將 ActiveMQ Classic 與 JBoss 整合。如果您尚未執行此操作,請使用上面的連結下載 Apache ActiveMQ Classic。在撰寫本文時,最新發布的版本是 4.0.2-RC4。下載此封存檔後,將其解壓縮到可以建立目錄的位置,最好與 JBoss 解壓縮的位置相同。使用以下命令驗證是否包含 ActiveMQ Classic RAR 檔案
$ cd ./incubator-activemq-4.0.2.tar.gz
$ jar tvf /opt/incubator-activemq-4.0.2/lib/optional/activemq-ra-4.0.2.rar
0 Mon Sep 25 19:00:50 MDT 2006 META-INF/
399 Mon Sep 25 19:00:48 MDT 2006 META-INF/MANIFEST.MF
523 Mon Sep 25 13:07:40 MDT 2006 META-INF/DISCLAIMER.txt
11359 Mon Sep 25 13:07:40 MDT 2006 META-INF/LICENSE.txt
20305 Mon Sep 25 13:07:40 MDT 2006 META-INF/ra.xml
2334 Mon Sep 25 13:07:40 MDT 2006 broker-config.xml
70023 Mon Sep 25 19:00:48 MDT 2006 activemq-ra-4.0.2.jar
1351744 Mon Sep 25 19:00:14 MDT 2006 activemq-core-4.0.2.jar
232107 Mon Sep 25 18:22:48 MDT 2006 activeio-core-3.0-beta4.jar
2143211 Tue Aug 01 11:44:50 MDT 2006 derby-10.1.1.0.jar
1901653 Tue Aug 01 11:47:08 MDT 2006 spring-1.2.6.jar
94713 Tue Aug 01 11:47:22 MDT 2006 xbean-spring-2.2.jar
52915 Tue Aug 01 12:22:16 MDT 2006 commons-logging-1.1.jar
16030 Tue Aug 01 12:22:32 MDT 2006 geronimo-j2ee-management\_1.0\_spec-1.0.jar
329586 Tue Aug 01 12:22:54 MDT 2006 backport-util-concurrent-2.1.jar
358085 Tue Aug 01 12:23:26 MDT 2006 log4j-1.2.12.jar
523 Mon Sep 25 19:00:50 MDT 2006 META-INF/DISCLAIMER.txt
11359 Mon Sep 25 19:00:50 MDT 2006 META-INF/LICENSE.txt
這只是 RAR 檔案的目錄。只有一個原因會導致此操作失敗 - ActiveMQ Classic tarball 或 zip 檔案下載不完整。除此之外,根據您下載的版本,某些程式庫的版本可能會略有不同。
現在回到 JBoss 安裝,並在預設 JBoss 環境的 JBoss deploy 目錄中建立 ActiveMQ Classic 的目錄。以下是執行此任務的命令
$ mkdir /opt/jboss-4.0.4.GA/server/default/deploy/activemq-ra.rar
$ cd /opt/jboss-4.0.4.GA/server/default/deploy/activemq-ra.rar
$ pwd
/opt/jboss-4.0.4.GA/server/default/deploy/activemq-ra.rar
`注意:** 建立目錄不是必需的,但當您剛開始時,這是設定 ActiveMQ Classic RAR 最簡單的方法。這是由於在開發階段,它可以非常輕鬆地變更設定所提供的彈性。另一種方法是,一旦設定足夠穩定,不再需要變更時,將目錄 JAR 化為 RAR 檔案。但在開發期間將所有內容保留在目錄中是最簡單的路徑。
現在將 activemq-ra-4.0.2.rar 解壓縮到目前的工作目錄中
jar xvf /opt/incubator-activemq-4.0.2/lib/optional/activemq-ra-4.0.2.rar
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: META-INF/DISCLAIMER.txt
inflated: META-INF/LICENSE.txt
inflated: META-INF/ra.xml
inflated: broker-config.xml
inflated: activemq-ra-4.0.2.jar
inflated: activemq-core-4.0.2.jar
inflated: activeio-core-3.0-beta4.jar
inflated: derby-10.1.1.0.jar
inflated: spring-1.2.6.jar
inflated: xbean-spring-2.2.jar
inflated: commons-logging-1.1.jar
inflated: geronimo-j2ee-management\_1.0\_spec-1.0.jar
inflated: backport-util-concurrent-2.1.jar
inflated: log4j-1.2.12.jar
inflated: META-INF/DISCLAIMER.txt
inflated: META-INF/LICENSE.txt
以下是該目錄內容的快速清單
$ ls -l
total 12848
drwxr-xr-x 6 bsnyder bsnyder 204 Oct 16 16:27 META-INF
-rw-r--r-- 1 bsnyder bsnyder 232107 Oct 16 16:27 activeio-core-3.0-beta4.jar
-rw-r--r-- 1 bsnyder bsnyder 1351744 Oct 16 16:27 activemq-core-4.0.2.jar
-rw-r--r-- 1 bsnyder bsnyder 70023 Oct 16 16:27 activemq-ra-4.0.2.jar
-rw-r--r-- 1 bsnyder bsnyder 329586 Oct 16 16:27 backport-util-concurrent-2.1.jar
-rw-r--r-- 1 bsnyder bsnyder 2334 Oct 16 16:27 broker-config.xml
-rw-r--r-- 1 bsnyder bsnyder 52915 Oct 16 16:27 commons-logging-1.1.jar
-rw-r--r-- 1 bsnyder bsnyder 2143211 Oct 16 16:27 derby-10.1.1.0.jar
-rw-r--r-- 1 bsnyder bsnyder 16030 Oct 16 16:27 geronimo-j2ee-management\_1.0\_spec-1.0.jar
-rw-r--r-- 1 bsnyder bsnyder 358085 Oct 16 16:27 log4j-1.2.12.jar
-rw-r--r-- 1 bsnyder bsnyder 1901653 Oct 16 16:27 spring-1.2.6.jar
-rw-r--r-- 1 bsnyder bsnyder 94713 Oct 16 16:27 xbean-spring-2.2.jar
現在是時候設定 ActiveMQ Classic 了。
設定 Apache ActiveMQ Classic
第五步是實際設定 ActiveMQ Classic 與 JBoss 整合。請記住,您應該位於以下目錄中
/opt/jboss-4.0.4.GA/server/default/deploy/activemq-ra.rar
您可能已將 JBoss 安裝在 /opt
中,也可能沒有,這並不特別重要。重要的是您位於上面建立的目錄中,以保留解壓縮的 ActiveMQ Classic RAR 檔案的內容。
`注意:** RAR 檔案是資源配接器封存檔 (RAR)。資源配接器是來自 J2EE 連接器架構 (JCA) 的概念,用於與企業資訊系統 (EIS) 介面,即應用程式伺服器外部的系統(例如,關聯式資料庫、大型主機、MOM、會計系統等)。資源配接器通常稱為 J2EE 連接器,與裝置驅動程式的概念非常相似,例如印表機,因為它們包含特定於連線到特定系統的資訊。JCA 的不同之處在於,該連線已在 Java 規範中正式化。因此,JCA 的整體概念是用於連線到任何 EIS,但那是什麼意思?JCA 1.5 透過以下合約提供連線和更多功能
版本 1.0 合約
在連接器架構的 1.0 版中,定義了三個合約來解決上述功能
- 連線管理合約:允許應用程式透過資源配接器連線到 EIS。它還允許應用程式伺服器集區對 EIS 的連線請求。
- 交易管理合約:允許應用程式管理和執行一個或多個 EIS 資源管理器的交易存取。
- 安全性合約:提供對 EIS 的安全存取支援。
版本 1.5 中的新合約
在 J2EE 連接器架構的 1.5 版中,資源配接器必須支援更多合約,因為新的功能和特性已納入規範。資源配接器可以透過實作規範中為每個合約定義的必要介面來支援這四個新合約。
- 生命週期管理合約:讓應用程式伺服器管理資源配接器的生命週期,也就是啟動和關閉功能。
- 工作管理合約:允許資源配接器透過將工作提交給應用程式伺服器執行來完成工作。由於應用程式伺服器為資源配接器執行工作,資源配接器無需擔心執行緒管理。相反,應用程式伺服器會有效率地管理這方面,並且可以在必要時使用執行緒集區。雖然工作管理合約不是必需的(資源配接器可以選擇管理自己的執行緒來處理工作),但強烈建議使用。
- 交易流入合約:允許資源配接器將匯入的交易傳播到應用程式伺服器,以及由 EIS 發起的流入交易完成和當機復原。
- 訊息流入合約:允許資源配接器同步或非同步地將訊息傳遞到應用程式伺服器中的端點,而無論訊息樣式、語意和基礎架構為何。
如需有關 JCA 的更多資訊,請查閱 J2EE 連接器架構文件。
開啟 META-INF/ra.xml
檔案,並尋找以下章節
`META-INF/ra.xml**
<config-property>
<description>
The URL to the ActiveMQ Classic server that you want this connection to connect to. If using
an embedded broker, this value should be 'vm://127.0.0.1'.
</description>
<config-property-name>ServerUrl</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>tcp://127.0.0.1:61616</config-property-value>
<!-- <config-property-value>vm://127.0.0.1</config-property-value> -->
</config-property>
上面的章節用於告知 ActiveMQ Classic RAR ActiveMQ Classic 的位置。預設情況下,VM 內通訊協定會被註解掉,而使用 tcp 通訊協定。這會尋找在 localhost 上任何介面上的 61616 連接埠上執行的 ActiveMQ Classic。如果您不介意透過 TCP 在 JVM 內通訊的效率低下,可以直接讓它保持原樣。但是,建議對嵌入式代理程式使用 vm://
傳輸,因此請註解掉 tcp://
傳輸,並取消註解 vm://
傳輸。以下是範例
`META-INF/ra.xml**
<config-property>
<description>
The URL to the ActiveMQ Classic server that you want this connection to connect to. If using
an embedded broker, this value should be 'vm://127.0.0.1'.
</description>
<config-property-name>ServerUrl</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<!-- <config-property-value>tcp://127.0.0.1:61616</config-property-value> -->
<config-property-value>vm://127.0.0.1</config-property-value>
</config-property>
由於我們將 ActiveMQ Classic 嵌入到 JBoss 內部,因此使用 vm://
傳輸比透過 tcp://
傳輸執行訊息傳遞更有效率。
現在進一步查看 META-INF/ra.xml
檔案,並找到以下章節
`META-INF/ra.xml**
<config-property>
<description>
Sets the XML configuration file used to configure the embedded ActiveMQ Classic broker via
Spring if using embedded mode.
BrokerXmlConfig is the filename which is assumed to be on the classpath unless
a URL is specified. So a value of foo/bar.xml would be assumed to be on the
classpath whereas file:dir/file.xml would use the file system.
Any valid URL string is supported.
</description>
<config-property-name>BrokerXmlConfig</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value></config-property-value>
<!--
<config-property-value>xbean:broker-config.xml</config-property-value>
-->
</config-property>
上面的章節需要變更為取消註解倒數第二行,並移除/取代它上面的空白元素。以下是應如何變更的範例
`META-INF/ra.xml**
<config-property>
<description>
Sets the XML configuration file used to configure the embedded ActiveMQ Classic broker via
Spring if using embedded mode.
BrokerXmlConfig is the filename which is assumed to be on the classpath unless
a URL is specified. So a value of foo/bar.xml would be assumed to be on the
classpath whereas file:dir/file.xml would use the file system.
Any valid URL string is supported.
</description>
<config-property-name>BrokerXmlConfig</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>xbean:broker-config.xml</config-property-value>
</config-property>
這個變更會指示 ActiveMQ Classic RAR 讀取名為 broker-config.xml
的組態檔(檔名前面的 xbean:
只是提示類別去讀取組態檔),該檔案位於 CLASSPATH 上。在此情況下,broker-config.xml
檔案位於 activemq-ra.rar
目錄中。將變更儲存到該檔案,然後開啟 broker-config.xml
檔案。
broker-config.xml
檔案就是 ActiveMQ Classic 的組態檔。此檔案用於組態 ActiveMQ Classic。此檔案的預設內容可以使用,但應自訂以符合您的環境。關於此組態,有幾個值得注意的地方。此檔案中最顯著的部分是 <persistenceAdapter>
元素以及 <transportConnectors>
和 <networkConnectors>
元素,如下所示。
`broker-config.xml**
<beans xmlns="http://activemq.org/config/1.0">
<broker useJmx="true">
...
<persistenceAdapter>
<journaledJDBC journalLogFiles="5" dataDirectory="activemq-data"/>
<!-- To use a different datasource, use th following syntax : -->
<!--
<journaledJDBC journalLogFiles="5" dataDirectory="../data" dataSource="#postgres-ds"/>
-->
</persistenceAdapter>
<transportConnectors>
<!-- prefixing a connector with discovery: causes the connector to be advertiesed over rendezvous -->
<transportConnector uri="tcp://127.0.0.1:61616" discoveryUri="multicast://default"/>
</transportConnectors>
<networkConnectors>
<!-- by default just auto discover the other brokers -->
<networkConnector uri="multicast://default"/>
<!--
<networkConnector uri="static://(tcp://host1:61616,tcp://host2:61616)"/>
-->
</networkConnectors>
</broker>
</beans>
對此檔案的第一個變更是將 brokerName 屬性新增至 broker 元素並提供名稱。
<broker useJmx="true" brokerName="bruce.broker1">
此外,相同的名稱也用於組態的下方,為 <transportConnector>
元素提供名稱。
<transportConnector name="bruce.broker1" uri="tcp://127.0.0.1:61616" discoveryUri="multicast://default"/>
現在,我們要告訴 ActiveMQ Classic 不要初始化 JMX,因為我們將使用 JBoss 現有的 JMX。
<!-- put the following as the first child of the broker tag -->
<managementContext><!-- we'll use an existing one (JBoss) instead of creating one -->
<managementContext createConnector="false"/>
</managementContext>
應該重新組態 <persistenceAdapter>
元素,以將其資料儲存在適當的位置。在 JBoss 上,這很可能是在您使用的伺服器組態的「data」目錄中。我們將使用 XBean 和 Spring 功能動態設定此值,該功能允許我們將系統屬性值注入到組態中。首先,需要啟用此功能。
<!-- put the following as the first child of the beans element -->
<bean xmlns="" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
現在,修改 journaledJDBC
元素的 dataDirectory
屬性,使其為以下內容:${jboss.server.data.dir}/activemq
。
<transportConnectors>
元素用於公告 ActiveMQ Classic Broker 以進行客戶端到 Broker 的通訊,而 <networkConnectors>
元素則用於公告 ActiveMQ Classic Broker 以進行 Broker 到 Broker 的通訊。預設組態是針對兩者都使用 ActiveMQ Classic 多播傳輸。這只是一個簡單的組態,可以讓 ActiveMQ Classic 啟動並執行,因此我們暫時就保持這樣。
`注意:** ActiveMQ Classic 可用的組態選項遠比這裡提到的多得多。上面的組態只足以讓 ActiveMQ Classic 啟動並執行,僅此而已。如需有關 ActiveMQ Classic 組態的詳細資訊,請參閱 ActiveMQ Classic 4.1 XML 參考。
現在,我們只需要啟動 JBoss,以確保它能正確啟動且不會發生錯誤,並使用我們先前用來啟動 JBoss 的相同命令。
$ cd jboss-4.0.4.GA
$ ./bin/run.sh -c default
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /opt/jboss-4.0.4.GA
JAVA: java
JAVA_OPTS: -server -Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcInterval=3600000
-Dsun.rmi.dgc.server.gcInterval=3600000 -Dprogram.name=run.sh
CLASSPATH: /opt/jboss-4.0.4.GA/bin/run.jar:/lib/tools.jar
=========================================================================
15:34:47,999 INFO [Server] Starting JBoss (MX MicroKernel)...
15:34:48,001 INFO [Server] Release ID: JBoss [Zion] 4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)
15:34:48,004 INFO [Server] Home Dir: /opt/jboss-4.0.4.GA
15:34:48,005 INFO [Server] Home URL: file:/opt/jboss-4.0.4.GA/
15:34:48,007 INFO [Server] Patch URL: null
15:34:48,007 INFO [Server] Server Name: default
15:34:48,007 INFO [Server] Server Home Dir: /opt/jboss-4.0.4.GA/server/default
15:34:48,011 INFO [Server] Server Home URL: file:/opt/jboss-4.0.4.GA/server/default/
15:34:48,012 INFO [Server] Server Log Dir: /opt/jboss-4.0.4.GA/server/default/log
15:34:48,017 INFO [Server] Server Temp Dir: /opt/jboss-4.0.4.GA/server/default/tmp
15:34:48,022 INFO [Server] Root Deployment Filename: jboss-service.xml
...
15:35:17,360 INFO [Server] JBoss (MX MicroKernel) [4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)]
Started in 28s:576ms
只要 JBoss 沒有錯誤地啟動,您就可以繼續進行下一步。
組態 JBoss
第六步是組態 JBoss,以便在 JBoss 啟動時初始化並啟動 ActiveMQ Classic。這是使用符合 JBoss JCA DTD 資料來源的 XML 檔案來完成的。與市場上大多數其他 Java 應用程式伺服器一樣,JBoss 架構使用 J2EE 連接器架構來管理任何種類的連線,包括 JDBC、JMS 等,而 JBoss JCA DTD 表示建立 XML 資料來源實例以組態 JBoss JCA 所允許的內容。以下是與 JBoss 一起使用的 XML 資料來源實例範例。
`activemq-jms-ds.xml**
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE connection-factories
PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
"http://www.jboss.org/j2ee/dtd/jboss-ds\_1\_5.dtd">
<connection-factories>
<tx-connection-factory>
<jndi-name>activemq/QueueConnectionFactory</jndi-name>
<xa-transaction/>
<track-connection-by-tx/>
<rar-name>activemq-ra.rar</rar-name>
<connection-definition>javax.jms.QueueConnectionFactory</connection-definition>
<ServerUrl>vm://127.0.0.1</ServerUrl>
<!--
<UserName>sa</UserName>
<Password></Password>
-->
<min-pool-size>1</min-pool-size>
<max-pool-size>200</max-pool-size>
<blocking-timeout-millis>30000</blocking-timeout-millis>
<idle-timeout-minutes>3</idle-timeout-minutes>
</tx-connection-factory>
<tx-connection-factory>
<jndi-name>activemq/TopicConnectionFactory</jndi-name>
<xa-transaction/>
<track-connection-by-tx/>
<rar-name>activemq-ra.rar</rar-name>
<connection-definition>javax.jms.TopicConnectionFactory</connection-definition>
<ServerUrl>vm://127.0.0.1</ServerUrl>
<!--
<UserName>sa</UserName>
<Password></Password>
-->
<min-pool-size>1</min-pool-size>
<max-pool-size>200</max-pool-size>
<blocking-timeout-millis>30000</blocking-timeout-millis>
<idle-timeout-minutes>3</idle-timeout-minutes>
</tx-connection-factory>
<mbean code="org.jboss.resource.deployment.AdminObject" name="activemq.queue:name=outboundQueue">
<attribute name="JNDIName">activemq/queue/outbound</attribute>
<depends optional-attribute-name="RARName">jboss.jca:service=RARDeployment,name='activemq-ra.rar'</depends>
<attribute name="Type">javax.jms.Queue</attribute>
<attribute name="Properties">PhysicalName=queue.outbound</attribute>
</mbean>
<mbean code="org.jboss.resource.deployment.AdminObject" name="activemq.topic:name=inboundTopic">
<attribute name="JNDIName">activemq/topic/inbound</attribute>
<depends optional-attribute-name="RARName">jboss.jca:service=RARDeployment,name='activemq-ra.rar'</depends>
<attribute name="Type">javax.jms.Topic</attribute>
<attribute name="Properties">PhysicalName=topic.inbound</attribute>
</mbean>
</connection-factories>
此 XML 實例會組態 JMS QueueConnectionFactory
和 JMS TopicConnectionFactory
,並透過 JNDI 使它們可用。此檔案中還定義了一些 s,用於指定主題和佇列。此檔案應放入 JBoss 部署目錄中。它的名稱 (*-ds.xml) 將使其在啟動時被 JBoss 部署器選取。說到啟動,一旦此檔案到位,只需啟動 JBoss 伺服器即可執行快速冒煙測試。以下是應該看到的輸出範例。
=========================================================================
JBoss Bootstrap Environment
JBOSS_HOME: /opt/jboss
JAVA: java
JAVA_OPTS: -server -Xms128m -Xmx512m -Dsun.rmi.dgc.client.gcInterval=3600000
-Dsun.rmi.dgc.server.gcInterval=3600000 -Dprogram.name=run.sh
CLASSPATH: /opt/jboss/bin/run.jar:/lib/tools.jar
=========================================================================
22:55:48,686 INFO [Server] Starting JBoss (MX MicroKernel)...
22:55:48,688 INFO [Server] Release ID: JBoss [Zion] 4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)
22:55:48,690 INFO [Server] Home Dir: /opt/jboss-4.0.4.GA
22:55:48,692 INFO [Server] Home URL: file:/opt/jboss-4.0.4.GA/
22:55:48,695 INFO [Server] Patch URL: null
22:55:48,697 INFO [Server] Server Name: default
22:55:48,698 INFO [Server] Server Home Dir: /opt/jboss-4.0.4.GA/server/default
22:55:48,701 INFO [Server] Server Home URL: file:/opt/jboss-4.0.4.GA/server/default/
22:55:48,702 INFO [Server] Server Log Dir: /opt/jboss-4.0.4.GA/server/default/log
22:55:48,704 INFO [Server] Server Temp Dir: /opt/jboss-4.0.4.GA/server/default/tmp
22:55:48,706 INFO [Server] Root Deployment Filename: jboss-service.xml
22:55:49,092 INFO [ServerInfo] Java version: 1.5.0_06,Apple Computer, Inc.
22:55:49,101 INFO [ServerInfo] Java VM: Java HotSpot(TM) Server VM 1.5.0_06-64,"Apple Computer, Inc."
22:55:49,102 INFO [ServerInfo] OS-System: Mac OS X 10.4.8,i386
22:55:49,803 INFO [Server] Core system initialized
22:55:53,379 INFO [WebService] Using RMI server codebase: http://rattlesnake:8083/
22:55:53,400 INFO [Log4jService$URLWatchTimerTask] Configuring from URL: resource:log4j.xml
22:55:54,034 INFO [NamingService] JNDI bootstrap JNP=/0.0.0.0:1099, RMI=/0.0.0.0:1098, backlog=50,
no client SocketFactory, Server SocketFactory=class
org.jboss.net.sockets.DefaultSocketFactory
22:55:58,475 INFO [Embedded] Catalina naming disabled
22:55:58,566 INFO [ClusterRuleSetFactory] Unable to find a cluster rule set in the classpath.
Will load the default rule set.
22:55:58,569 INFO [ClusterRuleSetFactory] Unable to find a cluster rule set in the classpath.
Will load the default rule set.
22:55:59,110 INFO [Http11BaseProtocol] Initializing Coyote HTTP/1.1 on http-0.0.0.0-8080
22:55:59,114 INFO [Catalina] Initialization processed in 545 ms
22:55:59,116 INFO [StandardService] Starting service jboss.web
22:55:59,121 INFO [StandardEngine] Starting Servlet Engine: Apache Tomcat/5.5.17
22:55:59,179 INFO [StandardHost] XML validation disabled
22:55:59,221 INFO [Catalina] Server startup in 105 ms
22:55:59,600 INFO [TomcatDeployer] deploy, ctxPath=/invoker, warUrl=.../deploy/http-invoker.sar/invoker.war/
22:55:59,874 INFO [WebappLoader] Dual registration of jndi stream handler: factory already defined
22:56:00,899 INFO [TomcatDeployer] deploy, ctxPath=/, warUrl=.../deploy/jbossweb-tomcat55.sar/ROOT.war/
22:56:01,700 INFO [TomcatDeployer] deploy, ctxPath=/jbossws, warUrl=.../tmp/deploy/tmp60528jbossws-exp.war/
22:56:01,891 INFO [SubscriptionManager] Bound event dispatcher to java:/EventDispatcher
22:56:02,203 INFO [TomcatDeployer] deploy, ctxPath=/jbossmq-httpil,
warUrl=.../deploy/jms/jbossmq-httpil.sar/jbossmq-httpil.war/
22:56:04,546 INFO [TomcatDeployer] deploy, ctxPath=/web-console,
warUrl=.../deploy/management/console-mgr.sar/web-console.war/
22:56:05,690 INFO [MailService] Mail Service bound to java:/Mail
22:56:07,215 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/activemq-ra.rar/
22:56:07,452 INFO [XBeanXmlBeanDefinitionReader] Loading XML bean definitions from class
path resource [broker-config.xml]
22:56:07,750 INFO [ClassPathXmlApplicationContext] Bean factory for application context
[org.apache.xbean.spring.context.ClassPathXmlApplicationContext;hashCode=13887543]:
org.springframework.beans.factory.support.DefaultListableBeanFactory defining
beans [org.apache.activemq.xbean.XBeanBrokerService]; root of BeanFactory hierarchy
22:56:07,765 INFO [ClassPathXmlApplicationContext] 1 beans defined in application context
[org.apache.xbean.spring.context.ClassPathXmlApplicationContext;hashCode=13887543]
22:56:07,773 INFO [CollectionFactory] JDK 1.4+ collections available
22:56:07,778 INFO [ClassPathXmlApplicationContext] Unable to locate MessageSource with name
'messageSource': using default
[org.springframework.context.support.DelegatingMessageSource@5fee96]
22:56:07,781 INFO [ClassPathXmlApplicationContext] Unable to locate ApplicationEventMulticaster
with name 'applicationEventMulticaster': using default
[org.springframework.context.event.SimpleApplicationEventMulticaster@78c714]
22:56:07,783 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in factory
[org.springframework.beans.factory.support.DefaultListableBeanFactory
defining beans [org.apache.activemq.xbean.XBeanBrokerService]; root of BeanFactory hierarchy]
22:56:08,181 INFO [BrokerService] ActiveMQ 4.0.2 JMS Message Broker (bruce.broker1) is starting
22:56:08,181 INFO [BrokerService] For help or more information please see:
http://incubator.apache.org/activemq/
22:56:09,989 INFO [JDBCPersistenceAdapter] Database driver recognized:
[apache\_derby\_embedded\_jdbc\_driver]
22:56:11,026 INFO [JournalPersistenceAdapter] Journal Recovery Started from: Active Journal:
using 5 x 20.0 Megs at: /opt/jboss-4.0.4.GA/activemq-data/journal
22:56:11,169 INFO [JournalPersistenceAdapter] Journal Recovered: 0 message(s) in transactions recovered.
22:56:11,489 INFO [TransportServerThreadSupport] Listening for connections at: tcp://rattlesnake:61616
22:56:11,491 WARN [MulticastDiscoveryAgent] brokerName not set
22:56:11,519 INFO [TransportConnector] Connector bruce.broker1 Started
22:56:11,522 INFO [NetworkConnector] Network Connector bridge Started
22:56:11,522 INFO [BrokerService] ActiveMQ JMS Message Broker
(bruce.broker1, ID:rattlesnake-59052-1161060967859-1:0) started
22:56:11,556 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/jboss-ha-local-jdbc.rar
22:56:11,599 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/jboss-ha-xa-jdbc.rar
22:56:11,623 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/jboss-local-jdbc.rar
22:56:11,647 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/jboss-xa-jdbc.rar
22:56:11,737 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/jms/jms-ra.rar
22:56:11,847 INFO [RARDeployment] Required license terms exist, view META-INF/ra.xml in
.../deploy/mail-ra.rar
22:56:12,251 INFO [ConnectionFactoryBindingService] Bound ConnectionManager
'jboss.jca:service=ConnectionFactoryBinding,name=activemq/QueueConnectionFactory' to
JNDI name 'java:activemq/QueueConnectionFactory'
22:56:12,258 INFO [ConnectionFactoryBindingService] Bound ConnectionManager
'jboss.jca:service=ConnectionFactoryBinding,name=activemq/TopicConnectionFactory' to
JNDI name 'java:activemq/TopicConnectionFactory'
22:56:12,265 INFO [AdminObject] Bound admin object 'org.apache.activemq.command.ActiveMQQueue'
at 'activemq/queue/outbound'
22:56:12,330 INFO [AdminObject] Bound admin object 'org.apache.activemq.command.ActiveMQTopic'
at 'activemq/topic/inbound'
22:56:13,246 INFO [ConnectionFactoryBindingService] Bound ConnectionManager
'jboss.jca:service=DataSourceBinding,name=DefaultDS' to JNDI name 'java:DefaultDS'
22:56:13,842 INFO [A] Bound to JNDI name: queue/A
22:56:13,845 INFO [B] Bound to JNDI name: queue/B
22:56:13,846 INFO [C] Bound to JNDI name: queue/C
22:56:13,848 INFO [D] Bound to JNDI name: queue/D
22:56:13,850 INFO [ex] Bound to JNDI name: queue/ex
22:56:13,876 INFO [testTopic] Bound to JNDI name: topic/testTopic
22:56:13,879 INFO [securedTopic] Bound to JNDI name: topic/securedTopic
22:56:13,880 INFO [testDurableTopic] Bound to JNDI name: topic/testDurableTopic
22:56:13,883 INFO [testQueue] Bound to JNDI name: queue/testQueue
22:56:13,994 INFO [UILServerILService] JBossMQ UIL service available at : /0.0.0.0:8093
22:56:14,055 INFO [DLQ] Bound to JNDI name: queue/DLQ
22:56:14,375 INFO [ConnectionFactoryBindingService] Bound ConnectionManager
'jboss.jca:service=ConnectionFactoryBinding,name=JmsXA' to JNDI name 'java:JmsXA'
22:56:14,525 INFO [TomcatDeployer] deploy, ctxPath=/jmx-console, warUrl=.../deploy/jmx-console.war/
22:56:14,991 INFO [Http11BaseProtocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080
22:56:15,071 INFO [ChannelSocket] JK: ajp13 listening on /0.0.0.0:8009
22:56:15,082 INFO [JkMain] Jk running ID=0 time=0/25 config=null
22:56:15,108 INFO [Server] JBoss (MX MicroKernel) [4.0.4.GA (build: CVSTag=JBoss\_4\_0\_4\_GA date=200605151000)]
Started in 26s:398ms
請注意來自 ActiveMQ Classic 和 AdminObjects
的啟動訊息,它們建立了 ActiveMQQueue
和 ActiveMQTopic
。這些都表示組態正確,但需要進一步驗證。這會在下一節中介紹。
測試整合
第七個也是最後一個步驟是對整合執行稍微全面的冒煙測試。這可以使用 Apache Ant 透過 ActiveMQ Classic 二進位發行版隨附的範例來完成。其中包含 Ant build.xml 檔案,可輕鬆存取簡單的消費者和簡單的生產者。生產者將用於傳送由消費者接收的訊息。若要繼續進行此測試,只需依照下列步驟操作即可。
- 在第一個終端機中,啟動 JBoss。這裡可以使用上面使用的相同啟動指令碼。
- 在第二個終端機中,使用下列命令來執行 ActiveMQ Classic 消費者。
$ cd /opt/incubator-activemq-4.0.2/examples #note in activemq v5 the directory is just "example" not "examples" $ ant consumer Buildfile: build.xml init: [mkdir] Created dir: /opt/incubator-activemq-4.0.2/example/target/classes [mkdir] Created dir: /opt/incubator-activemq-4.0.2/example/src/ddl compile: [javac] Compiling 6 source files to /opt/incubator-activemq-4.0.2/example/target/classes consumer: [echo] Running consumer against server at $url = tcp://127.0.0.1:61616 for subject $subject = TEST.FOO [java] Connecting to URL: tcp://127.0.0.1:61616 [java] Consuming queue: TEST.FOO [java] Using non-durable subscription [java] We are about to wait until we consume: 10 message(s) then we will shutdown
- 在第三個終端機中,使用下列命令來執行 ActiveMQ Classic 生產者。
$ cd /opt/incubator-activemq-4.0.2/examples $ ant producer Buildfile: build.xml init: compile: producer: [echo] Running producer against server at $url = tcp://127.0.0.1:61616 for subject $subject = TEST.FOO [java] Connecting to URL: tcp://127.0.0.1:61616 [java] Publishing a Message with size 1000 to queue: TEST.FOO [java] Using non-durable publishing [java] Sleeping between publish 0 ms [java] Sending message: Message: 0 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 1 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 2 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 3 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 4 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 5 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 6 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 7 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 8 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Sending message: Message: 9 sent at: Mon Oct 16 23:21:08 MDT 2006 ... [java] Done. [java] connection { [java] session { [java] messageCount{ count: 10 unit: count startTime: 1161062468001 lastSampleTime: 1161062468340 description: Number of messages exchanged } [java] messageRateTime{ count: 10 maxTime: 142 minTime: 1 totalTime: 315 averageTime: 31.5 averageTimeExMinMax: 21.5 averagePerSecond: 31.746031746031747 averagePerSecondExMinMax: 46.51162790697674 unit: millis startTime: 1161062468002 lastSampleTime: 1161062468340 description: Time taken to process a message (thoughtput rate) } [java] pendingMessageCount{ count: 0 unit: count startTime: 1161062468001 lastSampleTime: 1161062468001 description: Number of pending messages } [java] expiredMessageCount{ count: 0 unit: count startTime: 1161062468001 lastSampleTime: 1161062468001 description: Number of expired messages } [java] messageWaitTime{ count: 0 maxTime: 0 minTime: 0 totalTime: 0 averageTime: 0.0 averageTimeExMinMax: 0.0 averagePerSecond: 0.0 averagePerSecondExMinMax: 0.0 unit: millis startTime: 1161062468002 lastSampleTime: 1161062468002 description: Time spent by a message before being delivered } [java] durableSubscriptionCount{ count: 0 unit: count startTime: 1161062468002 lastSampleTime: 1161062468002 description: The number of durable subscriptions } [java] producers { [java] producer queue://TEST.FOO { [java] messageCount{ count: 10 unit: count startTime: 1161062468025 lastSampleTime: 1161062468340 description: Number of messages processed } [java] messageRateTime{ count: 10 maxTime: 142 minTime: 1 totalTime: 315 averageTime: 31.5 averageTimeExMinMax: 21.5 averagePerSecond: 31.746031746031747 averagePerSecondExMinMax: 46.51162790697674 unit: millis startTime: 1161062468025 lastSampleTime: 1161062468340 description: Time taken to process a message (thoughtput rate) } [java] pendingMessageCount{ count: 0 unit: count startTime: 1161062468025 lastSampleTime: 1161062468025 description: Number of pending messages } [java] messageRateTime{ count: 10 maxTime: 142 minTime: 1 totalTime: 315 averageTime: 31.5 averageTimeExMinMax: 21.5 averagePerSecond: 31.746031746031747 averagePerSecondExMinMax: 46.51162790697674 unit: millis startTime: 1161062468025 lastSampleTime: 1161062468340 description: Time taken to process a message (thoughtput rate) } [java] expiredMessageCount{ count: 0 unit: count startTime: 1161062468025 lastSampleTime: 1161062468025 description: Number of expired messages } [java] messageWaitTime{ count: 0 maxTime: 0 minTime: 0 totalTime: 0 averageTime: 0.0 averageTimeExMinMax: 0.0 averagePerSecond: 0.0 averagePerSecondExMinMax: 0.0 unit: millis startTime: 1161062468025 lastSampleTime: 1161062468025 description: Time spent by a message before being delivered } [java] } [java] } [java] consumers { [java] } [java] } [java] }
上面的步驟 1 只是啟動 JBoss。上面的步驟 2 啟動 ActiveMQ Classic 隨附的簡單訊息消費者。上面的步驟 3 啟動 ActiveMQ Classic 隨附的簡單訊息生產者。雖然訊息消費者和訊息生產者是簡單的公用程式,但它們都接受許多參數,使其對於測試 ActiveMQ Classic 組態非常有用。
換句話說,剛剛發生的事情是,訊息生產者將 10 則訊息傳送到 TEST.FOO 目的地,而訊息消費者從 TEST.FOO 目的地接收了 10 則訊息。儘管這是一個簡單的測試,但它確實使用了 ActiveMQ Classic Broker,儘管僅在一台機器上使用。下一步合乎邏輯的步驟是設定完整的 ActiveMQ Classic Broker 網路。
在一個 JBoss 實例中設定一個 Broker 之後,設定另一個 Broker 會變得容易得多,但需要另一台機器或作業系統實例。但這完全是另一篇文章,而且是未來要處理的事情。
結論
這裡示範的是 ActiveMQ Classic 與 JBoss 應用程式伺服器的整合。這種整合非常常見,許多企業都會執行。我希望這對有興趣將 ActiveMQ Classic 與 JBoss 應用程式伺服器整合的人有所幫助。
資源
以下是適用於 Spring 1.x 和 Spring 2.x 的組態。
2007 年 10 月 3 日由 Bruce Snyder
標籤
- 無標籤
- 編輯標籤
預覽
2007 年 10 月 3 日由 Bruce Snyder
標籤
- 無標籤
- 編輯標籤
預覽