Install ConnectionFactory for Camel JMS Producer: camel-jms Vs camel-sjms
Ciao, my main requirement is to have a route where I can post a message and that gets put on the JMS queue. The camel context runs in a JavaEE 6 container, namely JBoss AS 7.1.1, so it's HornetQ for the JMS that ships with it; I am starting the context with bootstrap singleton, but I am not using camel-cdi. So far I have used component camel-jms
, but now I am trying to switch to camel-sjms
if possible because no spring.
My question is how to properly configure the ConnectionFactory for camel-sjms
in this JavaEE scenario please?
With camel-jms, I could put this in the endpoint url as easy as .to("jms:myQueue?connectionFactory=#ConnectionFactory")
. With camel-sjms, I feel like I need to instantiate the SJMSComponent myself, set a connectionFactory and set that instance in the camel context before running it.
I have the code below for an example camel-jms vs camel-sjms and I would like to know if I did the right setup of the ConnectionFactory. Thank you.
For
camel-jms
this it was done as:
@Singleton
@Startup
public class CamelBootstrap {
private CamelContext camelContext;
private ProducerTemplate producerTemplate;
public CamelContext getCamelContext() {
return camelContext;
}
public ProducerTemplate getProducerTemplate() {
return producerTemplate;
}
@PostConstruct
public void init() throws Exception {
camelContext = new DefaultCamelContext();
camelContext.addRoutes(new MyCamelRoutes());
camelContext.start();
producerTemplate = camelContext.createProducerTemplate();
}
}
Nothing fancy, and in MyCamelRoutes
I could do the route setup using:
.to("jms:myQueue?connectionFactory=#ConnectionFactory")
For
camel-sjms
now, I need to change the bootstrap syntax with:
@Singleton
@Startup
public class CamelBootstrap {
@Resource(mappedName="java:/ConnectionFactory")
private ConnectionFactory connectionFactory;
private CamelContext camelContext;
private ProducerTemplate producerTemplate;
public CamelContext getCamelContext() {
return camelContext;
}
public ProducerTemplate getProducerTemplate() {
return producerTemplate;
}
@PostConstruct
public void init() throws Exception {
camelContext = new DefaultCamelContext();
SjmsComponent sjms = new SjmsComponent();
sjms.setConnectionFactory(connectionFactory);
camelContext.addComponent("sjms", sjms);
camelContext.addRoutes(new MyCamelRoutes());
camelContext.start();
producerTemplate = camelContext.createProducerTemplate();
}
}
and note that @Resource
for connectionFactory it is passed as a reference to the SjmsComponent instance that is passed in camelContext. And then in MyCamelRoutes
I could use sjms when setting up the route using:
.to("sjms:myQueue")
Obviously, the code works correctly in both scenarios, but as far as I understand, the ConnectionFactory configuration is quite prone to performance issues if not done correctly, so I prefer to ask if I am porting in correctly
camel-sjms
for my JavaEE scenario. Thanks again
source to share
Performance issues are likely to happen if you don't cache / pool JMS resources. Caching is usually configured by transferring the ConnectionFactory to some Caching ConnectionFactory library or by passing the control to the application server.
Camel SJMS includes a built-in pool. However, if you have a container-managed resource to handle JMS connections, you should probably consider using it. SJMS has some capability to deal with this problem, ConncetionResource instead of ConnectionFactory .
source to share