Tomcat8 和 ActiveMQ:名为 'someQueue' 的 Bean 必须是 [javax.jms.Queue] 类型,但实际上是 [org.apache.activemq.comma



在Tomcat 8 (8.5.4)上配置ActiveMQ (5.14.0)以暴露Spring (3.2.8)应用程序的JMS队列时,我面临着著名的BeanNotOfRequiredTypeException例外。

几个类似问题所建议的修复(改变AOP和代理行为)没有帮助:在应用这些更改之后,同样的错误又出现了:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'notificationService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'someQueue' must be of type [javax.jms.Queue], but was actually of type [org.apache.activemq.command.ActiveMQQueue]

即使ActiveMQQueue是有效的Queue型。


当前配置:

Spring应用程序通过JNDI查找队列:
<jee:jndi-lookup jndi-name="jms/someQueue" id="someQueue" />

队列在web.xml文件中引用:

<resource-ref>
    <description>Factory</description>
    <res-ref-name>jms/someConnectionFactory</res-ref-name>
    <res-type>javax.jms.QueueConnectionFactory</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
    <description>Queue</description>
    <res-ref-name>jms/someProcessQueue</res-ref-name>
    <res-type>javax.jms.Queue</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

在服务器端,在Tomcat server.xml文件中将队列配置为GlobalNamingResources,如下:

<Resource name="jms/someConnectionFactory" 
        auth="Container" 
        type="org.apache.activemq.ActiveMQConnectionFactory" 
        factory="org.apache.activemq.jndi.JNDIReferenceFactory" 
        description="JMS Connection Factory" 
        brokerURL="vm://localhost" 
        brokerName="LocalActiveMQBroker" 
        useEmbeddedBroker="true"
    />
<Resource name="jms/someQueue"
        auth="Container"
        type="org.apache.activemq.command.ActiveMQQueue"
        description="JMS queue"
        factory="org.apache.activemq.jndi.JNDIReferenceFactory"
        physicalName="SOME.QUEUE"
    />  

这是目前应用的最小配置。同样的方法已经应用于数据源和邮件服务(server.xml + web.xml + JNDI),工作良好,但是在队列配置上失败。

问题(s):为什么Spring一直认为它是错误的类型?在Tomcat 8上设置ActiveMQ并通过JNDI公开队列是否需要进一步的配置?

是不是你在服务器端和客户端声明了两种不同的类型?

//On the Tomcat side
type="org.apache.activemq.command.ActiveMQQueue"
//In the web.xml of your app
<res-type>javax.jms.Queue</res-type>

为了更好地解释:你在这里向下转换:你在你的应用程序web.xml中声明一个队列接口作为类型,你会收到实现它的东西->向下转换
它应该相反,或者将服务器类型设置为queue

问题解决了:是类路径问题

应用与问题中报告的完全相同的配置,并通过简单地从已部署的war中删除javax.jms-api库,不再有异常。

应用程序的pom.xml文件(实际上是它的war模块):

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>javax.jms-api</artifactId>
            <version>2.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注意:与javax.mail-api库相同。

这个冲突是由于使用了Tomcat lib文件夹中丢失的activemq-all-5.14.0.jar,它也带来了javax.jms-api库,并且在运行时也作为应用程序类路径的一部分。将应用程序依赖项更改为范围provided,将其从最终的war文件中删除,从而避免了部署和应用程序启动时的冲突。

最新更新