Is exchange type not available at sender side in S

2019-08-16 06:59发布

问题:

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.