將 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 是開源領域中的其中一個應用程式伺服器。一個非常常見的需求是將 ActiveMQ Classic 設定為 JBoss 內部的訊息傳遞基礎架構。雖然關於此整合的一些文件,但本文旨在提供更詳細的說明和解釋。因此,如果您需要將 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 Web 主控台。如果您可以看到此主控台,表示一切都準備就緒。

附帶一提,Web 主控台的左側需要正確安裝 Java 外掛程式。這應該在安裝 J2SE 時完成,因此如果它沒有正確運作,我建議您查閱一些關於您可能看到的任何錯誤的文件。JBoss 或 ActiveMQ Classic 的正常運作不需要 Java 外掛程式,它僅用於 JBoss Web 主控台。

正確安裝 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 部署目錄中為 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 的最簡單方法。這是因為在開發階段,它可以在設定方面提供非常大的彈性,讓您可以非常輕鬆地進行變更。另一種做法是在設定穩定到不再需要變更時,將該目錄壓縮成 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 啟動的流入交易完成和損毀復原。
  • 訊息流入合約:允許資源配接器同步或非同步地將訊息傳遞到應用程式伺服器中的端點,而不論訊息樣式、語意和基礎架構為何。

引述自 J2EE 連接器架構 1.5 中的新增功能

如需有關 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 的位置。預設情況下,會註解掉 In-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 的啟動訊息,它們會建立 ActiveMQQueueActiveMQTopic。這些都表示組態正確,但需要進一步驗證。這將在下一節中說明。

測試整合

第七步也是最後一步是執行更全面的整合冒煙測試。這可以使用 Apache Ant,透過 ActiveMQ Classic 二進位發行版隨附的範例完成。其中包含 Ant build.xml 檔案,可輕鬆存取簡單的消費者和簡單的生產者。生產者將用於傳送消費者接收的訊息。若要繼續進行此測試,只需按照以下步驟操作:

  1. 在第一個終端機中,啟動 JBoss。此處可以使用與上述相同的啟動腳本。
  2. 在第二個終端機中,使用以下指令執行 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
    
  3. 在第三個終端機中,使用以下指令執行 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 組態非常有用。

總而言之,剛才發生的是訊息生產者向 TEST.FOO 目的地傳送了 10 則訊息,而訊息消費者則從 TEST.FOO 目的地接收了 10 則訊息。儘管這是一個簡單的測試,但它確實使用了 ActiveMQ Classic Broker,即使只在一台機器上使用。接下來的合理步驟是設定完整的 ActiveMQ Classic Broker 網路。

在一個 JBoss 執行個體中設定一個 Broker 後,設定另一個 Broker 會容易得多,但需要另一台機器或作業系統執行個體。但這又是另一篇文章,而且是改天要處理的事情。

結論

這裡示範的是 ActiveMQ Classic 與 JBoss 應用程式伺服器的整合。這種整合非常常見,並且由許多企業執行。我希望這對有興趣將 ActiveMQ Classic 與 JBoss 應用程式伺服器整合的人們有所幫助。如果您有任何疑問或對 ActiveMQ Classic 相關的諮詢服務感興趣,請聯絡我們以取得更多資訊。

資源

以下是適用於 Spring 1.x 和 Spring 2.x 的組態:

檔案

已修改

檔案 amq-spring-1.2.6.tgz

2007 年 10 月 3 日,由 Bruce Snyder 提供

標籤

預覽

檔案 amq-spring-2.0.tgz

2007 年 10 月 3 日,由 Bruce Snyder 提供

標籤

預覽

全部下載

Apache、ActiveMQ、Apache ActiveMQ、Apache 羽毛標誌和 Apache ActiveMQ 專案標誌是 The Apache Software Foundation 的商標。版權所有 © 2024,The Apache Software Foundation。根據 Apache License 2.0 授權。