gpt4 book ai didi

java - Apache Camel JMS : How to use JNDI connection to publish or Subscribe to a Queue using java DSL?

转载 作者:行者123 更新时间:2023-12-01 16:19:00 24 4
gpt4 key购买 nike

我正在尝试使用 Camel 从 JBoss EAP 7.0 连接到 JMS 队列。我使用的是 Java DSL 而不是 Spring。如何获取 JNDI 条目并创建连接来监听或分派(dispatch)消息?
下面是我通常用来连接到 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 不能为空

最佳答案

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

}

关于java - Apache Camel JMS : How to use JNDI connection to publish or Subscribe to a Queue using java DSL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62319661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com