1
votes

I want to create a class that sends a message to a messaging queue using AMQP protocol such as ActiveMQ or RabbitMQ, but without including any specific jar/file/library to those MQ providers.

Every example I see on the web uses one of the above technologies. I thought I was able to connect to a queue strictly using JMS? How can I de-couple my messaging technology with my Producer/Consumer classes so that I could switch out ActiveMQ with RabbitMQ without recompiling the code? Is this even possible?

/frustrated @ 7:00 PM :(

Thanks!

3
JMS is a specification; ActiveMQ and RabbitMQ are JMS provider implementations that do the actual sending/receiving of messages. You can't run JMS code without a provider. @BevynQ's answer externalizes the connection factory so you can control which one to load with a config parameter, which is what you want. - raffian
Exactly. I know I'll need a provider, I just don't want to be coupled (compiled) to one. This is integrated into another application and it's not decided which provider they'll go with yet. Probably one of those two main ones. - dlite922

3 Answers

2
votes

Take a look at this: Implementing vendor-independent JMS solutions. Disclosure: I wrote it, a while ago, but I think you will find it addresses your question from the JMS perspective.

Update: As IBM's removed the page, here's a saved snapshot from archive.org Implementing vendor-independent JMS solutions

3
votes

Yes it is possible. What you want to do is make sure you do not refer to any ActiveMQ object in your code.

Then you need to create factory methods to acquire the ActiveMQ/RabbitMQ methods.

something like

public ConnectionFactory getConnectionFactory() throws IllegalAccessException, InstantiationException {
    Class<?> klass = Class.forName(System.getProperty("JMSConnectionFactory");
    return (ConnectionFactory)klass.newInstance();
}

Sadly The difficulty you will have is that each provider has slightly different initialisation code. So you need to support each one.

You can do it all by reflection but helper classes may be better.