你如何在Spring JMS中使用jms:listener-container标签?

时间:2014-05-05 21:57:27

标签: java spring tomcat

Spring / JMS新手在这里。谢谢你的耐心......

我按照这篇博客文章来设置我的JMS制作人:

http://bsnyderblog.blogspot.com/2010/02/using-spring-jmstemplate-to-send-jms.html

它似乎工作正常...我使用jconsole附加到我的ActiveMQ进程显示它实际上在做某事。所以现在我正在尝试将消费者添加到同一个项目中。现在,我只是想让它听取它自己的jms消息并处理它们。

所以我关注这篇博文:

http://bsnyderblog.blogspot.com/2010/02/using-spring-to-receive-jms-messages.html

似乎非常直截了当。但是,当我部署到Tomcat 8时,我收到此错误:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for element [listener-container]
  Offending resource: ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

所以,这是我的dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">

    <!-- set up all the controllers -->
    <context:component-scan base-package="springjmstest.controller"/>

    <!-- A connection to ActiveMQ -->
    <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="tcp://localhost:61616"/>

    <!-- A cached connection to wrap the ActiveMQ connection -->
    <bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
          p:targetConnectionFactory-ref="jmsConnectionFactory"
          p:sessionCacheSize="10"/>

    <!-- A destination in ActiveMQ -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="jobs"/>
    </bean>

    <!-- A JmsTemplate instance that uses the cached connection and destination -->
    <bean id="producerTemplate" class="org.springframework.jms.core.JmsTemplate"
          p:connectionFactory-ref="cachedConnectionFactory"
          p:defaultDestination-ref="destination"/>

    <!-- Register the listener for JMS messages on the "jobs" queue -->
    <bean id="simpleMessageListener" class="springjmstest.consumer.SimpleMessageListener">
        <jms:listener-container container-type="default" connection-factory="jmsConnectionFactory" acknowledge="auto">
            <jms:listener destination="jobs" ref="simpleMessageListener" method="onMessage"/>
        </jms:listener-container>
    </bean>
</beans>

我一直在搞乱xmlns和xsi:schemaLocation属性,在线查看示例,但我什么都没得到。 (事实上​​,我从谷歌那里得到的与我一样的错误的唯一参考是同一篇博文中的评论。他似乎暗示他通过弄乱这些属性来修复它,但他从未真正说过他做了什么解决它。)

这是我的听众课程:

public class SimpleMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        TextMessage msg = (TextMessage) message;
        try {
            String text = msg.getText();
            System.out.println("processing message: " + text);
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
    }
}

IntelliJ似乎对我设置的方式感到满意(正确链接到类等),但Tomcat 8不是(不是它一直是这样吗?;)

所以现在我开始怀疑我是否只有依赖问题。这是我的pom.xml中的依赖项标记:

<dependencies>
    <!-- web dependencies -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-core</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.0.3.RELEASE</version>
    </dependency>

    <!-- ActiveMQ -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-client</artifactId>
        <version>5.9.1</version>
    </dependency>
</dependencies>

如果重要,我正在使用Java 8.

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

试试这个:

<bean id="jmsProducerTemplate" class="org.springframework.jms.core.JmsTemplate"
      p:connectionFactory-ref="connectionFactory"/>

<jms:listener-container container-type="default" 
                        connection-factory="connectionFactory"
                        acknowledge="auto">

<jms:listener destination="YOURQUEUENAME" ref="theListenerClassYouAreUsing" />

</jms:listener-container>