關於ActiveMQ在MyEclipse中出現無法讀取schema文件的問題解決方法

licanqiu1985發表於2010-09-19
由於專案要使用到郵件功能,老大就叫我用ActiveMQ來管理郵件的傳送,其好處網上百度,這裡不說了,我想說說網上有好多號稱可以執行的Spring+ActiveMQ的例子,下載下來後發現每個例子的**applicationContext.xml都在MyEclipse中出現錯誤(紅點提示---看到就頭痛了),幾乎試驗過所有的中文解決方法了,還是沒法解決,大家都可以從英文提示那裡知道IDE找不到schema文件,無法讀取jar包裡的activemq.xsd檔案所引發。
找了一天的方法,還是那樣,紅點依舊,很沮喪,本人經受不起打擊,這次的打擊不小了。中文的方法無法解決,沒辦法,只好去試試英文的。本人英文不太好,只能死馬當活馬醫了,很快就發現了個解決方法,原來之前試驗過的一箇中文方法的方向是正確的,不過不知道是那篇中文文章的作者手誤還是不負責 :twisted: ,可以肯定他沒有試驗過就釋出的,不然怎麼還是出現紅點錯誤,而且一個錯誤,網上千千萬萬篇文章都是那樣的複製、黏貼轉載。給不少的新手帶來了障礙。

以下是從英文網站複製過來的----將一些認為是重點的翻譯一下

Newer versions of ActiveMQ have support for XML name space schema declarations to allow easier configuration of container beans. Adding the following XMLNS declaration to the beans container will allow you to use ActiveMQ’s amq name space.

XML:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:amq="http://activemq.apache.org/schema/core"
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-2.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">

<amq:broker start="true" brokerName="bs-localhost">
<amq:networkConnectors>
<!−− more configurations −−>
</amq:networkConnectors>
</amq:broker>

</bean>

When the application is deployed (in Tomcat) it will work as expected, however if you try to run it under Eclipse the container fails to start with the message:

org.xml.sax.SAXParseException: schema_reference.4:
Failed to read schema document 'http://activemq.apache.org/schema/core/activemq-core.xsd',
because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c:
The matching wildcard is strict,
but no declaration can be found for element 'amq:broker'. </xsd>

The amq:broker maybe replaced with amq:connectionFactory depending on which name elements you have actually used.


[color=red][b]In order for Eclipse to understand the amq name space you must add xbean support for ActiveMQ. This is achieved by adding xbean-spring-3.4.jar to your dynamic web project.[/b][/color]
[b][color=green]在專案中新增xbean-spring-3.4.jar是為了讓Eclipse能知道ActiveMQ的amq的名稱空間[/color][/b]


According to the ActiveMQ website the XSD declaration will be automatically fished out of the jar and used to help Eclipse give you features like command completion for the XML schema. In fact what happens, is that Eclipse lights up like a Christmas tree moaning that it can’t find the schema declaration. Below is a set of instructions to tell Eclipse how to associate the activemq-core.xsd url with the activemq-core.xsd found in the jar file which is shipped as part of ActiveMQ.

1. Select Windows->Preferences from the Eclipse menu bar.
2. Select XML->XML Catalog from the left hand configuration tree.
3. Highlight User Specified Entries and select Add.
4. The Add XML Catalog Entry windows appears.
5. In the Location enter the path to the activemq-all jar, then add !/activemq.xsd to the end of the jar name. e.g. jar:file:[b][color=red]C:/Java/libs/activemq/apache-activemq-5.2.0/activemq-all-5.2.0.jar!/activemq.xsd[/color][/b]
[b][color=green]以上這段中C:/Java/libs/activemq/apache-activemq-5.2.0/activemq-all-5.2.0.jar!/activemq.xsd的C:/Java/libs/activemq/apache-activemq-5.2.0替換為自己的acticemq-all.jar的絕對路徑,activemq-all-5.2.0.jar改成自己對應的包名便可以。中文方法都是寫<path for activemq>\,什麼也不說明下,嚴重鄙視他們,很不負責[/color][/b]
6. Under KeyType enter Namespace Name
7. and under Key enter http://activemq.apache.org/schema/core
8. then click Ok.
9. Now we need to do the same for the other name space file, so hit Add again.
10. In the Location enter the path to the activemq-all jar, then add !/activemq.xsd to the end of the jar name. e.g. jar:file:C:/Java/libs/activemq/apache-activemq-5.2.0/activemq-all-5.2.0.jar!/activemq.xsd
11. Under KeyType enter Schema Location
12. and under Key enter http://activemq.apache.org/schema/core/activemq-core.xsd
13. Click Ok, Ok.

The Spring IDE should do its thing and the red error markers will disappear.

相信大家都應該解決困擾很久的問題,我也還要繼續學習ActiveMQ的使用,很高興與大家分享經驗、交流技術

相關文章