gpt4 book ai didi

java - 你如何使用jms :listener-container tag in Spring JMS?

转载 作者:行者123 更新时间:2023-11-28 22:12:59 25 4
gpt4 key购买 nike

这里是 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。

感谢您的帮助。

最佳答案

试试这个:

<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>

关于java - 你如何使用jms :listener-container tag in Spring JMS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23482577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com