本篇文章主要讲如何用java代码创建队列和交换机,或者说是声明
我们都是在消费者这边进行声明
所以我们开始写代码
我们先创建这么个文件
package com.itheima.consumer.config; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 功能: * 作者:LiCooer * 日期:2024/4/9 14:52 */ @Configuration public class FanoutConfiguration { @Bean public FanoutExchange fanoutExchange() { // ExchangeBuilder.fanoutExchange("").build(); //这里也可以这么写 return new FanoutExchange("hmall.fanout2"); } @Bean public Queue fanoutQueue3() { // QueueBuilder.durable("").build(); //这里的durable是持久的意思 return new Queue("fanout.queue3"); } @Bean public Binding fanoutBinding3(Queue fanoutQueue3 , FanoutExchange fanoutExchange) { return BindingBuilder.bind(fanoutQueue3).to(fanoutExchange); } @Bean public Queue fanoutQueue4() { // QueueBuilder.durable("").build(); //这里的durable是持久的意思 return new Queue("fanout.queue4"); } @Bean public Binding fanoutBinding4() { return BindingBuilder.bind(fanoutQueue4()).to(fanoutExchange()); } }
只要我们一运行我们就可以看到
但这个只是fanout,如果我们是direct呢
所以我们在同目录下面创建文件
package com.itheima.consumer.config; import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 功能: * 作者:LiCooer * 日期:2024/4/9 14:52 */ @Configuration public class DirectConfiguration { @Bean public DirectExchange directExchange() { // ExchangeBuilder.fanoutExchange("").build(); //这里也可以这么写 return new DirectExchange("hmall.direct1"); } @Bean public Queue directQueue3() { // QueueBuilder.durable("").build(); //这里的durable是持久的意思 return new Queue("direct.queue3"); } @Bean public Binding directQueue3BindingRed(Queue directQueue3 , DirectExchange directExchange) { return BindingBuilder.bind(directQueue3).to(directExchange).with("red"); } @Bean public Binding directQueue3BindingBlue(Queue directQueue3 , DirectExchange directExchange) { return BindingBuilder.bind(directQueue3).to(directExchange).with("blue"); } @Bean public Queue directQueue4() { // QueueBuilder.durable("").build(); //这里的durable是持久的意思 return new Queue("direct.queue4"); } @Bean public Binding directQueue4BindingRed(Queue directQueue4 , DirectExchange directExchange) { return BindingBuilder.bind(directQueue4).to(directExchange).with("red"); } @Bean public Binding directQueue4BindingBrown(Queue directQueue4 , DirectExchange directExchange) { return BindingBuilder.bind(directQueue4).to(directExchange).with("brown"); } }
交换机还有队列就不多赘述了
很显然,很麻烦,所以祝贺各位变成白学公主
标签:return,第八篇,菜菜,durable,RabbitMQ,Queue,Bean,directExchange,public From: https://www.cnblogs.com/codeforceshobby/p/18124053