AMPQ Configuration

I am currently building an application that needs to use queuing. My usual response to queuing is to either roll my own (if the need is very simple) or reach for something like JMS with ActiveMQ. But my boss pointed me towards RabbitMQ and AMPQ. Now, AMPQ is kind of the big brother of the JMS/ActiveMQ family. It can do the same stuff, and a whole lot more I have yet to get into, but it is described as ‘middleware messaging’ rather than queuing, so there’s a clue in there.

Quite soon after I got into it I found it was a bit harder to configure than JMS to which Spring adds a lot of help. As I got a little further it seemed like this was an omission rather than something deliberate. Well, I’m fairly conversant with adding my own annotations to get Spring to do a little more, so I built something.

It is called madura-ampq and you can get it from git. I’ve published it to maven central as well. It’s not a big deal, but it allows you to annotate the receiver methods rather than configure them in a long winded way. For example you would say something like this:

@AMPQReceiver(queueName="transaction-queue")
public void receiveMessage(String message) {
...
}

…instead of this

@Bean
SimpleMessageListenerContainer container(
ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}

@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}

This means that to add a new listener to a queue you only need to annotate the method, you don’t have to open up the configuration file and add that extra stuff. The extra stuff is not very much when there is only one receiver method, but when you have a dozen or so the maintenance gets tedious.

There’s more to this, you can specify what message container etc you want, but I’m keeping it simple here. Check out the readme in the git repository.

1 Comment


  1. Very nice article, exactly what I was looking for.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *