| | |
| | | package com.smartor.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; |
| | | |
| | | @Configuration |
| | | public class RabbitMqConfig { |
| | | // public static final String EXCHANGE_NAME = "phone_topic_exchange2"; |
| | | // public static final String QUEUE_NAME = "phone_queue"; |
| | | //package com.smartor.config; |
| | | // |
| | | // /** |
| | | // * topic交换机,并持久化 |
| | | // */ |
| | | // @Bean(EXCHANGE_NAME) |
| | | // public Exchange phoneExchange() { |
| | | // Map<String, Object> arguments = new HashMap<>(); |
| | | // //指定通信方式为topic |
| | | // arguments.put("x-delayed-type", "topic"); |
| | | // //使用CustomExchange类创建,类型要指定为“x-delayed-message”类型 |
| | | // Exchange exchange = new CustomExchange(EXCHANGE_NAME, "x-delayed-message", true, false, arguments); |
| | | // return exchange; |
| | | //import org.springframework.amqp.core.*; |
| | | //import org.springframework.beans.factory.annotation.Qualifier; |
| | | //import org.springframework.context.annotation.Bean; |
| | | //import org.springframework.context.annotation.Configuration; |
| | | // |
| | | //// |
| | | //// return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build(); |
| | | // } |
| | | //import java.util.HashMap; |
| | | //import java.util.Map; |
| | | // |
| | | ////@Configuration |
| | | //public class RabbitMqConfig { |
| | | // |
| | | ////定义延迟队列 |
| | | // public static final String phone_queue = "phone_queue"; |
| | | // //定义延迟交换机 |
| | | // public static final String phone_exchange = "phone_exchange"; |
| | | // //定义路由键 |
| | | // public static final String routing_key = "phone.123"; |
| | | // |
| | | // |
| | | // /** |
| | | // * 队列 |
| | | // */ |
| | | // @Bean(QUEUE_NAME) |
| | | // public Queue phoneQueue() { |
| | | // return QueueBuilder.durable(QUEUE_NAME).build(); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 交换机与队列的绑定 |
| | | // * 定义延迟队列 |
| | | // * |
| | | // * @return |
| | | // */ |
| | | // @Bean |
| | | // public Binding bindQueueExchange(@Qualifier(QUEUE_NAME) Queue phoneQueue, @Qualifier(EXCHANGE_NAME) Exchange phoneExchange) { |
| | | // return BindingBuilder.bind(phoneQueue).to(phoneExchange).with(" phone.#").noargs(); |
| | | // public Queue delayQueue() { |
| | | // return new Queue(phone_queue, true); |
| | | // } |
| | | //定义延迟队列 |
| | | public static final String phone_queue = "phone_queue"; |
| | | //定义延迟交换机 |
| | | public static final String phone_exchange = "phone_exchange"; |
| | | //定义路由键 |
| | | public static final String routing_key = "phone.123"; |
| | | |
| | | |
| | | /** |
| | | * 定义延迟队列 |
| | | * |
| | | * @return |
| | | */ |
| | | @Bean |
| | | public Queue delayQueue() { |
| | | return new Queue(phone_queue, true); |
| | | } |
| | | |
| | | /** |
| | | * 延时队列交换机 |
| | | * 交换机类型:CustomExchange |
| | | * |
| | | * @return |
| | | */ |
| | | @Bean |
| | | public CustomExchange delayExchange() { |
| | | Map<String, Object> args = new HashMap<>(); |
| | | args.put("x-delayed-type", "topic"); |
| | | return new CustomExchange(phone_exchange, "x-delayed-message", true, false, args); |
| | | } |
| | | |
| | | /** |
| | | * 为延迟队列绑定交换机 |
| | | * |
| | | * @param queue |
| | | * @param exchange |
| | | * @return |
| | | */ |
| | | @Bean |
| | | public Binding delayBinding(Queue queue, CustomExchange exchange) { |
| | | return BindingBuilder.bind(queue).to(exchange).with(routing_key).noargs(); |
| | | } |
| | | |
| | | } |
| | | // |
| | | // /** |
| | | // * 延时队列交换机 |
| | | // * 交换机类型:CustomExchange |
| | | // * |
| | | // * @return |
| | | // */ |
| | | // @Bean |
| | | // public CustomExchange delayExchange() { |
| | | // Map<String, Object> args = new HashMap<>(); |
| | | // args.put("x-delayed-type", "topic"); |
| | | // return new CustomExchange(phone_exchange, "x-delayed-message", true, false, args); |
| | | // } |
| | | // |
| | | // /** |
| | | // * 为延迟队列绑定交换机 |
| | | // * |
| | | // * @param queue |
| | | // * @param exchange |
| | | // * @return |
| | | // */ |
| | | // @Bean |
| | | // public Binding delayBinding(Queue queue, CustomExchange exchange) { |
| | | // return BindingBuilder.bind(queue).to(exchange).with(routing_key).noargs(); |
| | | // } |
| | | // |
| | | //} |