【问题标题】:Apache Camel JMS : How to use JNDI connection to publish or Subscribe to a Queue using java DSL?Apache Camel JMS:如何使用 JNDI 连接使用 java DSL 发布或订阅队列?
【发布时间】:2020-06-11 07:58:29
【问题描述】:

我正在尝试使用骆驼从 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

【问题讨论】:

    标签: java apache-camel jms dsl


    【解决方案1】:

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

    }

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 2011-10-09
      • 2019-05-19
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多