Is exchange type not available at sender side in Spring AMQP RabbitMQ codes?

一笑奈何 提交于 2019-12-11 16:17:44

问题


I have a basic question in RabbitMQ. In case of Spring AMQP, in both XML configuration and Java configuration using annotations, the type of exchange being used is known only at the listener side. I mean to at, only the consumer configuration code contains the type of exchange being used (topic/direct/fanout). At the sender side we will have -

amqpTemplate.convertAndSend("exchange_name", "routing_key",sampleMessage);

So is it like the sender code is not bothered or aware of the type(topic/direct/fanout) of exchange to which it is sending to??

In case of python, we have at the sender side,

 channel.exchange_declare(exchange='logs',
                     exchange_type='fanout')

So here, the type of exchange is known at the sender. But in case of Spring, is exchange name is all that is available??


回答1:


In general, the sender doesn't need to know the type of exchange, although sending to a fanout means the routing key is ignored so can be any value.

You can declare the exchange as follows:

@Bean
public FanoutExchange exchange() {
    return new FanoutExchange("logs");
} 

If there is a @RabbitAdmin bean, the exchange will automatically be declared, based on the bean type. Spring Boot auto-configures an admin; if you are not using boot you need to declare your own.



来源:https://stackoverflow.com/questions/49302660/is-exchange-type-not-available-at-sender-side-in-spring-amqp-rabbitmq-codes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!