Cyrusmith Cyrusmith - 1 year ago 84
Java Question

Spring and AMQP RabbitMQ topic exchange not working

I'm trying to set up topic exchange on my spring app.
Here's my context configuration:

public class IntegrationConfig {

public final static String queueName = "my-queue";

AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);


Queue queue() {
return new Queue(queueName);

TopicExchange exchange() {
return new TopicExchange("my-exchange", false, true);

Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("ru.interosite.*");

SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
return container;

ImageUploadReceiver receiver() {
return new ImageUploadReceiver();

MessageListenerAdapter listenerAdapter(ImageUploadReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");


This is receiver class:

public class ImageUploadReceiver {
private CountDownLatch latch = new CountDownLatch(1);

public void receiveMessage(String message) {
System.out.println("Received ");

public CountDownLatch getLatch() {
return latch;

This is sender code:

public String sendMessage() {
rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
try {
imageUploadReceiver.getLatch().await(3, TimeUnit.SECONDS);
} catch (InterruptedException e) {
return "Msg received";

So I'm sending message to topic exchange using binding key "ru.interosite.1" to the queue that was bound with pattern "ru.interosite.*". I used these key and pattern when tried sample from and they worked fine.

But inside String AMQP it does not work, i.e. receiver never gets called. It called only if binding key and pattern are completely the same as if I were using DirectExchange.

Am I missing something here?

Answer Source

You don't show the config for the RabbitTemplate, but I guess it is with default options.

To send a message to the my-exchange you must specify it directly:

rabbitTemplate.convertAndSend("my-exchange", "ru.interosite.1", "ttt1233");