如何使用Spring Boot通过扇出交换在RabbitMQ上发布消息

我有下面的代码使用来将消息发布到RabbitMQ队列中fanout

exchange。该exchange是越来越创建,但该消息无法被看到RabbitMQ的队列。我也没有看到任何错误。

@SpringBoot" title="SpringBoot">SpringBootApplication

public class BasicApplication {

public static final String QUEUE_NAME_1 = "helloworld.fanout.q1";

public static final String QUEUE_NAME_2 = "helloworld.fanout.q2";

public static final String EXCHANGE_NAME = "helloworld.fanout.x";

//here the message ==> xchange ==> queue1, queue2

@Bean

public List<Declarable> fanoutBindings() {

Queue fanoutQueue1 = new Queue(QUEUE_NAME_1, false);

Queue fanoutQueue2 = new Queue(QUEUE_NAME_2, false);

FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE_NAME);

return Arrays.asList(

fanoutQueue1,

fanoutQueue2,

fanoutExchange,

bind(fanoutQueue1).to(fanoutExchange),

BindingBuilder.bind(fanoutQueue2).to(fanoutExchange));

}

public static void main(String[] args) {

SpringApplication.run(BasicApplication.class, args).close();

}

}

@Component

public class Producer implements CommandLineRunner {

@Autowired

private RabbitTemplate rabbitTemplate;

@Override

public void run(String... args) throws Exception {

this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "Hello World !");

}

}

回答:

您使用了错误的convertAndSend方法;该方法的第一个参数是routingKey

使用this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "", "Hello World !");

以上是 如何使用Spring Boot通过扇出交换在RabbitMQ上发布消息 的全部内容, 来源链接: utcz.com/qa/406328.html

回到顶部