【发布时间】:2020-07-20 19:54:42
【问题描述】:
我正在尝试使用来自 ActiveMQ 主题的消息。下面是代码:
@Configuration
@EnableJms
public class Config {
@Value("${activemq.broker-url}")
private String brokerURL;
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerURL);
activeMQConnectionFactory.setTrustAllPackages(true);
return activeMQConnectionFactory;
}
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(){
DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
defaultJmsListenerContainerFactory.setConnectionFactory(activeMQConnectionFactory());
defaultJmsListenerContainerFactory.setPubSubDomain(true);
return defaultJmsListenerContainerFactory;
}
我的消费者代码:
@Component
@EnableJms
public class Consumer {
@JmsListener(destination = "xml.inbound.topic", containerFactory = "jmsListenerContainerFactory")
public void Processor(final Message xmlMessage) {
}
}
我得到的例外是:
通过字段 'jmsTemplate' 表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/investmentbank/equityfeedsprocessingrevised/config/Config.class] 中定义名称为“jmsTemplate”的 bean 创建时出错:通过工厂方法进行 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.jms.core.JmsTemplate]:工厂方法“jmsTemplate”抛出异常;嵌套异常是 java.lang.ClassCastException:类 org.springframework.jms.config.DefaultJmsListenerContainerFactory 无法转换为类 javax.jms.ConnectionFactory(org.springframework.jms.config.DefaultJmsListenerContainerFactory 和 javax.jms.ConnectionFactory 位于未命名的模块中加载程序“应用程序”)
我在这里做错了什么?为什么会出现异常?
我也试过了:
@Bean
public DefaultMessageListenerContainer jmsListenerContainerFactory() {
DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
dmlc.setConnectionFactory(activeMQConnectionFactory());
dmlc.setPubSubDomain(true);
return dmlc;
}
我在这里得到的例外是:
通过字段 'jmsTemplate' 表达的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/investmentbank/equityfeedsprocessingrevised/config/Config.class] 中定义名称为“jmsTemplate”的 bean 创建时出错:通过工厂方法进行 bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.jms.core.JmsTemplate]:工厂方法“jmsTemplate”抛出异常;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/investmentbank/equityfeedsprocessingrevised/config/Config.class] 中定义名称为“jmsListenerContainerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:需要属性“destination”或“destinationName”
只需添加我的 JmsTemplate 代码如下所示:
@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(activeMQConnectionFactory());
jmsTemplate.setPubSubDomain(true);
return jmsTemplate;
}
我已经使用 Apache Camel(代码)实现了 JMS 主题发布器:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
CamelContext _ctx = new DefaultCamelContext();
_ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
_ctx.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
from("file:src/main/resources?fileName=data-sample_2.csv")
.process(new MyTransformRevised1())
.to("file:src/main/resources/?fileName=emp.xml")
.split(body().tokenizeXML("equityFeeds", null)).streaming().to("jms:topic:xml.inbound.topic");
}
});
我可以在我的主题名称的“消息入队”列中看到消息的数量。
出了什么问题?我在网上阅读了各种帖子,但无法解决问题。请帮助解决问题。我无法阅读有关 ActiveMQ 主题的消息。让我知道我是否缺少某些信息。
【问题讨论】:
标签: java spring-boot apache-camel activemq jms-topic