一:引入RabbitMQ的相关jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
二:配置application.yml
配置rabbitmq的yml:
server:
port: 44000
spring:
application:
name: test‐rabbitmq‐producer
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtualHost: /
三:创建rabbitMQ的配置类
配置RabbitMQ配置类,主要有交换机、队列、已经两者的绑定
本案例配置Topic交换机
package com.xuecheng.test.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;
/**
* @Auther: 星仔
* @Date: 2018/12/29 10:54
* @Description:
*/
@Configuration
public class RabbitMQConfig {
//声明队列名称
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
//声明交换机的名称
public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";
//声明RoutingKey
private static final String ROUTINGKEY_SMS = "inform.#.sms.#";
private static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
/**
* 交换机配置
* ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
* * @return the exchange
*/
@Bean(EXCHANGE_TOPICS_INFORM)
public Exchange EXCHANGE_TOPICS_INFORM(){
//durable(true)持久化,消息队列重启后交换机仍然存在
return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
}
//声明队列
@Bean(QUEUE_INFORM_EMAIL)
public Queue QUEUE_INFORM_EMAIL(){
return new Queue(QUEUE_INFORM_EMAIL);
}
//声明队列
@Bean(QUEUE_INFORM_SMS)
public Queue QUEUE_INFORM_SMS(){
return new Queue(QUEUE_INFORM_SMS);
}
/** channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
* * 绑定队列到交换机
* @param queue the queue
* @param exchange the exchange
* @return the binding
*/
@Bean
public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
}
@Bean
public Binding BINDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
}
}
四:配置生产端发送消息
package com.xuecheng.rabbitmq.producer;
import com.xuecheng.test.config.RabbitMQConfig;
import com.xuecheng.test.config.RabbitmqApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @Auther: 星仔
* @Date: 2018/12/24 21:31
* @Description:
*/
@SpringBootTest(classes = RabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class ProducerTest05 {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void rabbitMQTest(){
String message = "send message";
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_TOPICS_INFORM,"inform.sms.email",message);
}
}
五:配置消费端接受消息
package com.xuecheng.test.receive;
import com.rabbitmq.client.Channel;
import com.xuecheng.test.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Auther: 星仔
* @Date: 2018/12/29 11:26
* @Description:
*/
@Component
public class ReceiveHandler {
//监听队列
@RabbitListener(queues = {RabbitMQConfig.QUEUE_INFORM_SMS})
public void receive_sms(String msg, Message message, Channel channel){
System.out.println(msg);
System.out.println(message.getBody());
}
//监听队列
@RabbitListener(queues = {RabbitMQConfig.QUEUE_INFORM_EMAIL})
public void receive_email(String msg, Message message, Channel channel){
System.out.println(msg);
System.out.println(message.getBody());
}
}