java - Apache Camel JMS:如何使用 JNDI 连接使用 java DSL 发布或订阅队列?
问题描述
我正在尝试使用骆驼从 JBoss EAP 7.0 连接到 JMS 队列。我使用的是 Java DSL 而不是 Spring。如何获取 JNDI 条目并创建连接以收听或发送消息?
以下是我ActiveMQ
通常用来连接的代码段!
CamelContext context = new DefaultCamelContext();
context.addComponent("activemq", ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
context.addRoutes(new RouteBuilder() {
public void configure() {
from("direct:writeQueue").to("activemq:queue:FOO");
}
});
context.start();
Thread.sleep(2000);
ProducerTemplate producer = context.createProducerTemplate();
producer.sendBody("activemq:queue:FOO", "Test Message");
我尝试添加如下代码:
CamelContext context = null;
@Resource(mappedName = "java:/jboss/exported/jms/queue/TestQ")
private ConnectionFactory connectionFactory;
public void testJMS() throws Exception {
context = new DefaultCamelContext();
JmsComponent component = new JmsComponent();
component.setConnectionFactory(connectionFactory);
context.addComponent("jms", component);
/*
Routing Section
*/
}
但是这段代码给了我这样的错误:connectionFactory must not be null
解决方案
这是我在 JBoss 中所做的... Wildfly:
public class ComponentFactory {
private static final int JMS_POOL_SIZE = 5;
@Resource(mappedName = "java:/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Produces
@ApplicationScoped
@Named("jms")
public SjmsComponent jmsComponent() {
SjmsComponent component = new SjmsComponent();
ConnectionResource pool = new ConnectionFactoryResource(JMS_POOL_SIZE, connectionFactory);
component.setConnectionResource(pool); // Use built-in Wildfly pool
return component;
}
}
推荐阅读
- python-3.x - 基于布尔值在熊猫中将多列合并为一列
- python - 灰度医学图像的图像处理
- java - 如何在 Google 幻灯片之类的应用中使用 Cast SDK
- java - 无法将 UUID 插入嵌入式数据库
- ruby-on-rails - 我的闪光警报有时会消失,但并非总是如此
- javascript - d3 TypeError: a is undefined
- java - Error while token management process in lambda (Java runtime) "Unable to load credentials from service endpoint: com.amazonaws.SdkClientException"
- c# - Regex to optionally match 3 digits at the end of file name
- python - 如何从我的项目中完全卸载 django-taggit?
- java - 如何使用 Apache Directory API 进行排序搜索