Apache Camel JMS:如何使用 JNDI 连接使用 java DSL 发布或订阅队列?
Apache Camel JMS : How to use JNDI connection to publish or Subscribe to a Queue using java DSL?
我正在尝试使用 camel 从 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;
}
}
我正在尝试使用 camel 从 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;
}
}