- 一、依赖
- 二、代码编写
- 1、Hello World!(简单队列)
- 1.1、RabbitMQ配置类代码
- 1.2、生产者代码
- 1.3、消费者代码
- 2、Work queues(工作队列 / 任务队列)
- 2.1、RabbitMQ配置类代码
- 2.2、生产者代码
- 2.3、消费者代码
- 3、Work queues(工作队列 / 任务队列)
- 3.1、RabbitMQ配置类代码
- 3.2、生产者代码
- 3.3、消费者代码
- 4、Publish/Subscribe(发布订阅模式)
- 4.1、RabbitMQ配置类代码
- 4.2、生产者代码
- 4.3、消费者代码
- 5、Routing(路由模式)
- 5.1、RabbitMQ配置类代码
- 5.2、生产者代码
- 5.3、消费者代码
说明:
以下Demo案例对应RabbitMQ学习文档(入门篇(Demo使用Spring编写))中的Demo代码
一、依赖<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二、代码编写
1、Hello World!(简单队列)
1.1、RabbitMQ配置类代码
@Configuration
public class RabbitMQConfig1 {
// 队列名称
public static final String COMMON_QUEUE_NAME = "common_queue1";
@Bean
public Queue commonQueue1() {
return QueueBuilder.durable(COMMON_QUEUE_NAME).build();
}
}
1.2、生产者代码
@RestController
public class Provider1 {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg1")
public String sendMsg() {
rabbitTemplate.convertAndSend("", RabbitMQConfig1.COMMON_QUEUE_NAME, "测试消息");
return "发送成功";
}
}
1.3、消费者代码
@Component
public class Consumer1 {
@RabbitListener(queues = {RabbitMQConfig1.COMMON_QUEUE_NAME})
public void receiveMsg(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消息内容:" + msg);
}
}
2、Work queues(工作队列 / 任务队列)
2.1、RabbitMQ配置类代码
@Configuration
public class RabbitMQConfig2 {
// 队列名称
public static final String COMMON_QUEUE_NAME = "common_queue2";
@Bean
public Queue commonQueue2() {
return QueueBuilder.durable(COMMON_QUEUE_NAME).build();
}
}
2.2、生产者代码
@RestController
public class Provider2 {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg2")
public String sendMsg() {
for (int i = 1; i <= 10; i++) {
rabbitTemplate.convertAndSend("", RabbitMQConfig2.COMMON_QUEUE_NAME, "测试消息" + i);
}
return "发送成功";
}
}
2.3、消费者代码
@Component
public class Consumer2 {
@RabbitListener(queues = {RabbitMQConfig2.COMMON_QUEUE_NAME})
public void receiveMsg1(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者1,接收到的消息内容:" + msg);
}
@RabbitListener(queues = {RabbitMQConfig2.COMMON_QUEUE_NAME})
public void receiveMsg2(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者2,接收到的消息内容:" + msg);
}
}
3、Work queues(工作队列 / 任务队列)
3.1、RabbitMQ配置类代码
@Configuration
public class RabbitMQConfig3 {
// 普通交换机
public static final String COMMON_EXCHANGE_NAME = "common_exchange3";
// 普通队列
public static final String COMMON_QUEUE_NAME = "common_queue3";
@Bean
public Exchange commonExchange3() {
return ExchangeBuilder.fanoutExchange(COMMON_EXCHANGE_NAME).build();
}
@Bean
public Queue commonQueue3() {
return QueueBuilder.durable(COMMON_QUEUE_NAME).build();
}
@Bean
public Binding commonBinding3(@Qualifier("commonQueue3") Queue commonQueue, @Qualifier("commonExchange3") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with("").noargs();
}
}
3.2、生产者代码
@RestController
public class Provider3 {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg3")
public String sendMsg() {
rabbitTemplate.convertAndSend(RabbitMQConfig3.COMMON_EXCHANGE_NAME, "", "测试消息");
return "发送成功";
}
}
3.3、消费者代码
@Component
public class Consumer3 {
@RabbitListener(queues = {RabbitMQConfig3.COMMON_QUEUE_NAME})
public void receiveMsg(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消息内容:" + msg);
}
}
4、Publish/Subscribe(发布订阅模式)
4.1、RabbitMQ配置类代码
@Configuration
public class RabbitMQConfig4 {
// 普通交换机
public static final String COMMON_EXCHANGE_NAME = "common_exchange4";
// 普通队列1
public static final String COMMON_QUEUE_NAME41 = "common_queue41";
// 普通队列2
public static final String COMMON_QUEUE_NAME42 = "common_queue42";
// 普通队列路由1
public static final String COMMON_ROUTING_NAME41 = "COMMON_routing41";
// 普通队列路由2
public static final String COMMON_ROUTING_NAME42 = "COMMON_routing42";
// 普通队列路由3
public static final String COMMON_ROUTING_NAME43 = "common_queue43";
@Bean
public Exchange commonExchange4() {
return ExchangeBuilder.directExchange(COMMON_EXCHANGE_NAME).build();
}
@Bean
public Queue commonQueue41() {
return QueueBuilder.durable(COMMON_QUEUE_NAME41).build();
}
@Bean
public Queue commonQueue42() {
return QueueBuilder.durable(COMMON_QUEUE_NAME42).build();
}
@Bean
public Binding commonBinding41(@Qualifier("commonQueue41") Queue commonQueue, @Qualifier("commonExchange4") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_ROUTING_NAME41).noargs();
}
@Bean
public Binding commonBinding42(@Qualifier("commonQueue41") Queue commonQueue, @Qualifier("commonExchange4") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_ROUTING_NAME42).noargs();
}
@Bean
public Binding commonBinding43(@Qualifier("commonQueue42") Queue commonQueue, @Qualifier("commonExchange4") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_ROUTING_NAME43).noargs();
}
}
4.2、生产者代码
@RestController
public class Provider4 {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg4")
public String sendMsg() {
rabbitTemplate.convertAndSend(RabbitMQConfig4.COMMON_EXCHANGE_NAME, RabbitMQConfig4.COMMON_ROUTING_NAME41, "测试消息,使用路由1");
rabbitTemplate.convertAndSend(RabbitMQConfig4.COMMON_EXCHANGE_NAME, RabbitMQConfig4.COMMON_ROUTING_NAME42, "测试消息,使用路由2");
rabbitTemplate.convertAndSend(RabbitMQConfig4.COMMON_EXCHANGE_NAME, RabbitMQConfig4.COMMON_ROUTING_NAME43, "测试消息,使用路由3");
return "发送成功";
}
}
4.3、消费者代码
@Component
public class Consumer4 {
@RabbitListener(queues = {RabbitMQConfig4.COMMON_QUEUE_NAME41})
public void receiveMsg1(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者1接收到的消息内容:" + msg);
}
@RabbitListener(queues = {RabbitMQConfig4.COMMON_QUEUE_NAME42})
public void receiveMsg2(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者2接收到的消息内容:" + msg);
}
}
5、Routing(路由模式)
5.1、RabbitMQ配置类代码
@Configuration
public class RabbitMQConfig5 {
// 普通交换机
public static final String COMMON_EXCHANGE_NAME = "common_exchange5";
// 普通队列1
public static final String COMMON_QUEUE_NAME51 = "common_queue51";
// 普通队列2
public static final String COMMON_QUEUE_NAME52 = "common_queue52";
// 普通队列路由1
public static final String COMMON_ROUTING_NAME51 = "common.routing.51";
// 普通队列路由2
public static final String COMMON_ROUTING_NAME52 = "common.routing.52";
// 普通队列路由3
public static final String COMMON_ROUTING_NAME53 = "common.routing.53";
// 话题1
public static final String COMMON_TOPIC1 = "common.*.*";
// 话题2
public static final String COMMON_TOPIC2 = "*.*.51";
@Bean
public Exchange commonExchange5() {
return ExchangeBuilder.topicExchange(COMMON_EXCHANGE_NAME).build();
}
@Bean
public Queue commonQueue51() {
return QueueBuilder.durable(COMMON_QUEUE_NAME51).build();
}
@Bean
public Queue commonQueue52() {
return QueueBuilder.durable(COMMON_QUEUE_NAME52).build();
}
@Bean
public Binding commonBinding51(@Qualifier("commonQueue51") Queue commonQueue, @Qualifier("commonExchange5") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_TOPIC1).noargs();
}
@Bean
public Binding commonBinding52(@Qualifier("commonQueue51") Queue commonQueue, @Qualifier("commonExchange5") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_TOPIC2).noargs();
}
@Bean
public Binding commonBinding53(@Qualifier("commonQueue52") Queue commonQueue, @Qualifier("commonExchange5") Exchange commonExchange) {
return BindingBuilder.bind(commonQueue).to(commonExchange).with(COMMON_TOPIC2).noargs();
}
}
5.2、生产者代码
@RestController
public class Provider5 {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/sendMsg5")
public String sendMsg() {
rabbitTemplate.convertAndSend(RabbitMQConfig5.COMMON_EXCHANGE_NAME, RabbitMQConfig5.COMMON_ROUTING_NAME51, "测试消息,使用路由1");
rabbitTemplate.convertAndSend(RabbitMQConfig5.COMMON_EXCHANGE_NAME, RabbitMQConfig5.COMMON_ROUTING_NAME52, "测试消息,使用路由2");
rabbitTemplate.convertAndSend(RabbitMQConfig5.COMMON_EXCHANGE_NAME, RabbitMQConfig5.COMMON_ROUTING_NAME53, "测试消息,使用路由3");
return "发送成功";
}
}
5.3、消费者代码
@Component
public class Consumer5 {
@RabbitListener(queues = {RabbitMQConfig5.COMMON_QUEUE_NAME51})
public void receiveMsg1(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者1接收到的消息内容:" + msg);
}
@RabbitListener(queues = {RabbitMQConfig5.COMMON_QUEUE_NAME52})
public void receiveMsg2(Message message, Channel channel) {
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
System.out.println("消费者2接收到的消息内容:" + msg);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)