1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package club.kittybunny.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import java.util.HashMap; import java.util.Map;
/** * @Author: bunny * @Description: 我是兔子我会喵,我叫喵星兔 */ @Configuration public class RabbitConfig { // 两个交换机 @Bean("topicEx") public TopicExchange getTopicExchange(){
return new TopicExchange("BUNNY_TOPIC_EXCHANGE");//交换机名字 }
@Bean("fanoutEx") public FanoutExchange getFanoutExchange(){ return new FanoutExchange("BUNNY_FANOUT_EXCHANGE"); }
// 两个队列,不同方式定义
//queue: QueueName, //队列名称 //durable: false, //队列是否持久化.false:队列在内存中,服务器挂掉后,队列就没了;true:服务器重启后,队列将会重新生成.注意:只是队列持久化,不代表队列中的消息持久化!!!! //exclusive: false, //队列是否专属,专属的范围针对的是连接,也就是说,一个连接下面的多个信道是可见的.对于其他连接是不可见的.连接断开后,该队列会被删除.注意,不是信道断开,是连接断开.并且,就算设置成了持久化,也会删除. //autoDelete: true, //如果所有消费者都断开连接了,是否自动删除.如果还没有消费者从该队列获取过消息或者监听该队列,那么该队列不会删除.只有在有消费者从该队列获取过消息后,该队列才有可能自动删除(当所有消费者都断开连接,不管消息是否获取完) //arguments: null //队列的配置 //第5个参数: arguments 它的类型是一个键值对集合 @Bean("firstQueue") public Queue getFirstQueue(){ Map<String, Object> args = new HashMap<String, Object>(); args.put("x-message-ttl",6000); Queue queue = new Queue("BUNNY_FIRST_QUEUE", false, false, true, args); return queue; }
@Bean("secondQueue") public Queue getSecondQueue(){ return new Queue("BUNNY_SECOND_QUEUE");//队列名字 }
// 三个绑定 @Bean public Binding bindOne(@Qualifier("firstQueue") Queue queue, @Qualifier("topicEx") TopicExchange exchange){//使用bean的名字 return BindingBuilder.bind(queue).to(exchange).with("#.bunny.#"); } @Bean public Binding bindSecond(@Qualifier("secondQueue") Queue queue, @Qualifier("topicEx") TopicExchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("changsha.#"); } @Bean public Binding bindThird(@Qualifier("secondQueue") Queue queue,@Qualifier("fanoutEx") FanoutExchange exchange){ return BindingBuilder.bind(queue).to(exchange); }
}
|