RabbitMQ四种模式
1、配置类
@Configuration public class MQConfig { public static final String QUEUE = "queue"; public static final String TOPIC_QUEUE1 = "topic.queue1"; public static final String TOPIC_QUEUE2 = "topic.queue2"; public static final String HEADER_QUEUE = "header.queue"; public static final String TOPIC_EXCHANGE = "topicExchage"; public static final String FANOUT_EXCHANGE = "fanoutxchage"; public static final String HEADERS_EXCHANGE = "headersExchage"; /** * 1.Direct模式 交换机Exchange,生产者要指定一个queue * */ @Bean public Queue queue() { return new Queue(QUEUE, true); } /** * 2.Topic模式 交换机Exchange,生产者指定一个TopicExchange,只要与TopicExchange绑定并routingKey能匹配得上queue都会收到消息 * */ @Bean public Queue topicQueue1() { return new Queue(TOPIC_QUEUE1, true); } @Bean public Queue topicQueue2() { return new Queue(TOPIC_QUEUE2, true); } @Bean public TopicExchange topicExchage(){ return new TopicExchange(TOPIC_EXCHANGE); } @Bean public Binding topicBinding1() { return BindingBuilder.bind(topicQueue1()).to(topicExchage()).with("topic.key1"); } @Bean public Binding topicBinding2() { return BindingBuilder.bind(topicQueue2()).to(topicExchage()).with("topic.#");//通配符:*表示一个单词,#表示0个或多个单词 } /** * 3.Fanout模式(广播者模式) 交换机Exchange,生产者指定一个FanoutExchange,只要与FanoutExchange绑定的queue都能收到消息 * */ @Bean public FanoutExchange fanoutExchage(){ return new FanoutExchange(FANOUT_EXCHANGE); } @Bean public Binding FanoutBinding1() { return BindingBuilder.bind(topicQueue1()).to(fanoutExchage()); } @Bean public Binding FanoutBinding2() { return BindingBuilder.bind(topicQueue2()).to(fanoutExchage()); } /** * 4.Header模式 交换机Exchange,生产者指定一个HeadersExchange,并指定一个queue,但是当头部信息匹配时才能发送信息 * */ @Bean public HeadersExchange headersExchage(){ return new HeadersExchange(HEADERS_EXCHANGE); } @Bean public Queue headerQueue1() { return new Queue(HEADER_QUEUE, true); } @Bean public Binding headerBinding() { Map<String, Object> map = new HashMap<String, Object>(); map.put("header1", "value1"); map.put("header2", "value2"); return BindingBuilder.bind(headerQueue1()).to(headersExchage()).whereAll(map).match(); } }
2、生产者
@Service public class MQSender { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private AmqpTemplate amqpTemplate; public void send(String message){ logger.info("send message:{}",message); amqpTemplate.convertAndSend(MQConfig.QUEUE,message); } public void sendTopic(String message){ logger.info("send topic message:{}",message); amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key1",message+1); amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE,"topic.key2",message+2); } public void sendFanout(String message){ logger.info("send fanout message:{}",message); amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"",message);//routingKey为空 } public void sendHeader(String message){ logger.info("send header message:{}",message); // map.put("header1", "value1"); // map.put("header2", "value2"); MessageProperties messageProperties = new MessageProperties(); messageProperties.setHeader("header1","value1"); messageProperties.setHeader("header2","value2"); Message obj = new Message(message.getBytes(),messageProperties); amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,MQConfig.HEADER_QUEUE,obj);//routingKey为空 } }
3、消费者
@Service public class MQReceiver { private Logger logger = LoggerFactory.getLogger(getClass()); @RabbitListener(queues = MQConfig.QUEUE) public void receiver(String message){ logger.info("receiver message:{}",message); } @RabbitListener(queues = MQConfig.TOPIC_QUEUE1) public void receiverTopic1(String message){ logger.info("receiver topic1 message:{}",message); } @RabbitListener(queues = MQConfig.TOPIC_QUEUE2) public void receiverTopic2(String message){ logger.info("receiver topic2 message:{}",message); } @RabbitListener(queues = MQConfig.HEADER_QUEUE) public void receiverHeader(byte[] message){ logger.info("receiver header message:{}",new String(message)); } }
本文来自博客园,作者:咔咔皮卡丘,转载请注明原文链接:https://www.cnblogs.com/anquing/p/17638088.html