Spring AMQP RabbitMQ 代码中的发送方是否不提供交换类型? [英] Is exchange type not available at sender side in Spring AMQP RabbitMQ codes?

查看:37
本文介绍了Spring AMQP RabbitMQ 代码中的发送方是否不提供交换类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RabbitMQ 中有一个基本问题.在 Spring AMQP 的情况下,在使用注解的 XML 配置和 Java 配置中,所使用的交换类型仅在侦听器端知道.我的意思是,只有消费者配置代码包含正在使用的交换类型(主题/直接/扇出).在发送方,我们将 -

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??

在python的情况下,我们在发送方有,

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

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

所以在这里,发送方知道交换的类型.但是在 Spring 的情况下,交换名称是唯一可用的吗??

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

推荐答案

通常,发送者不需要知道交换的类型,尽管发送到扇出意味着路由键被忽略,因此可以是任何值.

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");
} 

如果有 @RabbitAdmin bean,将根据 bean 类型自动声明交换.Spring Boot 自动配置管理员;如果您不使用引导,则需要声明自己的.

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.

这篇关于Spring AMQP RabbitMQ 代码中的发送方是否不提供交换类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆