目录
RabbitMq实操Topic方式
项目准备和配置相关见系列二
一、RabbitMq的config代码实现:
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class RabbitConfigTopic {
public final static String topic_message_A = "topic.topic_message_A";
public final static String topic_message_AMany ="topic.topic_message_A.many";
public final static String topic_exchange="topic_exchange";
@Bean
public Queue queueMessageTopicA(){
return new Queue(RabbitConfigTopic.topic_message_A, true);
}
/*@Bean
public Queue queueMessageAMany(){
return new Queue(RabbitConfigTopic.topic_message_AMany, true);
}*/
@Bean
TopicExchange exchange(){
return new TopicExchange(topic_exchange);
}
@Bean
Binding bindingExchangeMessageA(Queue queueMessageTopicA, TopicExchange exchange){
return BindingBuilder.bind(queueMessageTopicA).to(exchange).with("topic.topic_message_A");
}
/**
* 只要是topic开头的message都会绑到AMany的queue中(会收到更多的message)
* @param queueMessageTopicAMany
* @param exchange
* @return
*/
@Bean
Binding bindingExchangeMessageAMany(Queue queueMessageTopicAMany, TopicExchange exchange){
return BindingBuilder.bind(queueMessageTopicAMany).to(exchange).with("topic.#");
}
}
二、生产者代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
@Slf4j
public class MsgProducerTopic {
@Autowired
public RabbitTemplate rabbitTemplate;
public void send1(String msg){
//String context = "这是从topic_message_A发出来的:"+ new Date();
log.info(msg);
rabbitTemplate.convertAndSend(RabbitConfigTopic.topic_exchange, RabbitConfigTopic.topic_message_A, msg);
}
public void send2(String msg){
//String context = "这是从topic_message_AMany发出来的:"+ new Date();
log.info(msg);
rabbitTemplate.convertAndSend(RabbitConfigTopic.topic_exchange, RabbitConfigTopic.topic_message_AMany, msg);
}
}
三、消费者代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = RabbitConfigTopic.topic_message_A)
@Slf4j
public class MsgReceiverTopicQueueA {
@RabbitHandler
public void process(String message) {
log.info("接收队列A的信息:" + message);
}
}
四、测试代码实现
package org.maggiezhang.springbootdemo.common.mq;
import lombok.extern.slf4j.Slf4j;
import org.maggiezhang.springbootdemo.CaseBase;
import org.maggiezhang.springbootdemo.common.mq.MsgProducerTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
import java.util.Date;
@Slf4j
public class TestMsgProducerTopic extends CaseBase{
@Autowired
MsgProducerTopic msgProducerTopic;
@Test
public void testSendMsgTopicA(){
msgProducerTopic.send1("这是从topic_message_A发出来的:"+ new Date());
log.info("test: send message A successfully");
}
@Test
public void testSendMsgTopicAMany(){
msgProducerTopic.send2("这是从topic_message_AMany发出来的:"+ new Date());
log.info("test: send message Many successfully");
}
}