Z
Z
zencd2016-05-31 13:56:28
Java
zencd, 2016-05-31 13:56:28

Is it possible to create a RabbitMQ consumer without an exchange definition?

There is code that creates a consumer of messages from the rmq queue at runtime (taken from the `sping-rabbitmq` example).
Question: is it possible to get rid of the exchange definition? And if so, how? Exchanges are needed only to deliver the message to the queue, and consumers seem to have nothing to do with it ...

CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
cf.setUsername("tst");
cf.setPassword("tst");

RabbitAdmin admin = new RabbitAdmin(cf);
Queue queue = new Queue(queueName);
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange("myExchange");
admin.declareExchange(exchange);
admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(routingKey));

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
Object listener = new Object() {
    public void handleMessage(byte[] message) { ... }
};
MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
container.setMessageListener(adapter);
container.setQueueNames(queueName);
container.start();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Владимир, 2016-05-31
@rostel

воркеров сажаю в router и не парюсь

Сергей, 2016-06-07
@yarkin

На Java не пишу, но: а Вы не пробовали убрать те три строчки, где используется exchange? Это приводит к ошибке?
Проблема будет в том, что очередь и обменник нужно будет связать (создать биндинг), это можно сделать при создании очереди или при создании обменника, ну или в какой-то третий момент времени. Если этого не сделать, то сообщения, публикуемые в обменник не попадут в очередь.
Но, если требуется просто очередь, без какой-то реальной потребности в обменнике, то при публикации можно использовать обменник по умолчанию (пустая строка) и имя очереди в качестве ключа маршрутизации и RabbitMQ закинет сообщение в эту очередь.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question