1.在pom中添加springboot对amqp的支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.在application.properties中添加RabbitMQ的简单配置信息
spring.rabbitmq.host=127.0.0.1
#5672是发送消息端口,15672是管理端的端口
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3.配置Queue(消息队列)
@Configuration
public class QueueConfig {
@Bean(name = "topic_queue1")
public Queue topic_queue1() {
return new Queue("topic_queue1");
}
@Bean(name = "topic_queue2")
public Queue topic_queue2() {
return new Queue("topic_queue2");
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("topic_exchange");
}
@Bean
Binding bindingExchangeTopicQueue1(@Qualifier("topic_queue1") Queue topic_queue1, TopicExchange exchange) {
return BindingBuilder.bind(topic_queue1).to(exchange).with("topic.message");
}
@Bean
Binding bindingExchangeTopicQueue2(@Qualifier("topic_queue2") Queue topic_queue2, TopicExchange exchange) {
// *表示一个词,#表示零个或多个词
return BindingBuilder.bind(topic_queue2).to(exchange).with("topic.#");
}
}
4.编写消息生产者
@Component
public class Sender_Topic {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* RabbitMQ将会根据第二个参数去寻找有没有匹配此规则的队列,如果有,则把消息给它,如果有不止一个,则把消息分发给匹配的队列(每个队列都有消息!)
*
* @param exchangeName 交换机名称
* @param routingKey 发送的routingKey
* @param message 内容
*/
public void send(String exchangeName,String routingKey,Message message) {
rabbitTemplate.convertAndSend(exchangeName,routingKey,message);
}
}
5.编写消息消费者
@Component
public class Receive_Topic {
//监听器监听指定的Queue
@RabbitListener(queues="topic_queue1")
public void process1(Message message) throws UnsupportedEncodingException {
MessageProperties messageProperties = message.getMessageProperties();
String contentType = messageProperties.getContentType();
System.out.println("Receive-topic_queue1:"+new String(message.getBody(), contentType));
}
//监听器监听指定的Queue
@RabbitListener(queues="topic_queue2")
public void process2(Message message) throws UnsupportedEncodingException {
MessageProperties messageProperties = message.getMessageProperties();
String contentType = messageProperties.getContentType();
System.out.println("Receive-topic_queue2:"+new String(message.getBody(), contentType));
}
}
6.编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRabbitMQ_Topic {
@Autowired
private Sender_Topic sender_Topic;
@Test
public void testRabbit_Topic() {
/**
* 声明消息 (消息体, 消息属性)
*/
MessageProperties messageProperties = new MessageProperties();
//设置消息是否持久化。Persistent表示持久化,Non-persistent表示不持久化
messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
messageProperties.setContentType("UTF-8");
Message message = new Message("hello,rabbit_topic!".getBytes(), messageProperties);
/**
* 参数2Routing key设置为topic.message,匹配了一个队列,消息将会被发放到这一个队列中
*/
sender_Topic.send("topic_exchange","topic.message",message);
/**
* 参数2Routing key设置为topic.messages,匹配了两个队列,消息将会被发放到这两个队列中
*/
sender_Topic.send("topic_exchange","topic.messages",message);
}
}
接下来就可以测试啦,首先启动接收端的应用,紧接着运行发送端的单元测试,接收端应用打印出来接收到的消息,测试即成功!