简体   繁体   中英

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

I am trying to connect to JMS queue from JBoss EAP 7.0 using camel. I am using Java DSL not Spring. How can I get the JNDI entries and create a connection to listen or dispatch a message?
Below are the code segment I am using to connect to ActiveMQ in general!

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");

I have tried adding code like below:

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
    */
}

But this code is giving me error like: connectionFactory must not be null

Here is what I am doing in 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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM