3
votes

Ciao, my basic requirement is to have a route where I can send a message and this is put on a JMS Queue. The camel context run in a JavaEE 6 container namely JBoss AS 7.1.1 so it's HornetQ for JMS which ships with it; I start the context via bootstrap singleton but I don't use the camel-cdi. So far I've been using camel-jms component, but now I'm looking to migrate to the camel-sjms if possible because springless.

My question is: what is the proper way to configure the ConnectionFactory for camel-sjms in this JavaEE scenario, please?

With the camel-jms I could put this in the endpoint URL, as simple as .to("jms:myQueue?connectionFactory=#ConnectionFactory"). With the camel-sjms instead it seems to me that I need to create an instance of the SJMSComponent myself, set the connectionFactory, and set this instance in the camel context before starting it.

I have code below for the camel-jms Vs camel-sjms case, and I would like to know if I "migrated" the setting of the ConnectionFactory correctly. Thanks.


For camel-jms this 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 special, and in the MyCamelRoutes I could do route configuration using:

.to("jms:myQueue?connectionFactory=#ConnectionFactory")


For camel-sjms now I have to modify the bootstrap singleton 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 please notice @Resource for the connectionFactory this is passed as a reference to the SjmsComponent instance, which is passed to the camelContext. And then in the MyCamelRoutes I could use the sjms while do route configuration using:

.to("sjms:myQueue")


The code seems to work correctly in both scenario, but as I understand the configuration of the ConnectionFactory is quite susceptible of performance issue if not done correctly, therefore I prefer to ask if I migrated to the camel-sjms correctly for my JavaEE scenario. Thanks again
1

1 Answers

0
votes

Performance issues are likely to happend if you don't do caching/pooling of JMS resources. Caching is typically configured by wrapping a ConnectionFactory in some Caching ConnectionFactory library - or by handing over the control to the application server.

Camel SJMS includes built-in pooling. However, if you have a container managed resource to handle JMS connections, you should probably consider using it. SJMS has some facilities to deal with that, ConncetionResource instead of ConnectionFactory.