首页 > 解决方案 > 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

标签: javaapache-cameljmsdsl

解决方案


这是我在 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;
}

}


推荐阅读