添加RabbitMQ队列的测试

This commit is contained in:
LittleBoy 2022-05-23 17:09:06 +08:00
parent e4481507fa
commit 47f79ff458
6 changed files with 139 additions and 5 deletions

View File

@ -21,10 +21,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-amqp</artifactId>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>

View File

@ -0,0 +1,47 @@
package xyz.longicorn.driver.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
// 要订阅消息队列的主题
public static final String RABBITMQ_DEFAULT_TOPIC = "default_message_topic";
public static final String RABBITMQ_DEFAULT_DIRECT_EXCHANGE = "default_message_exchange";
public static final String RABBITMQ_DEFAULT_DIRECT_ROUTING = "default_message_routing";
// 队列
@Bean
public Queue rabbitmqDemoDirectQueue() {
/**
* 1name: 队列名称
* 2durable: 是否持久化
* 3exclusive: 是否独享排外的如果设置为true定义为排他队列则只有创建者可以使用此队列也就是private私有的
* 4autoDelete: 是否自动删除也就是临时队列当最后一个消费者断开连接后会自动删除
* */
return new Queue(RABBITMQ_DEFAULT_TOPIC, true, false, false);
}
// 交换机
@Bean
public DirectExchange rabbitmqDemoDirectExchange() {
//Direct交换机
return new DirectExchange(RABBITMQ_DEFAULT_DIRECT_EXCHANGE, true, false);
}
// 队列与交换机的绑定
@Bean
public Binding bindDirect() {
//链式写法绑定交换机和队列并设置匹配键
return BindingBuilder
//绑定队列
.bind(rabbitmqDemoDirectQueue())
//到交换机
.to(rabbitmqDemoDirectExchange())
//并设置匹配键
.with(RABBITMQ_DEFAULT_DIRECT_ROUTING);
}
}

View File

@ -0,0 +1,20 @@
package xyz.longicorn.driver.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import xyz.longicorn.driver.config.RabbitConfig;
import java.util.Map;
@Component
@RabbitListener(queues = RabbitConfig.RABBITMQ_DEFAULT_TOPIC) // 设置要监听队列的主题名称
@Slf4j
public class RabbitMessageConsumer {
// 处理消息的方法 参数取决去发送消息的数据类型
@RabbitHandler
public void processMessage(Map<String, Object> map) {
log.info("获取到一条消息:" + map.toString());
}
}

View File

@ -0,0 +1,20 @@
package xyz.longicorn.driver.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.longicorn.driver.service.RabbitMQService;
import javax.annotation.Resource;
@RestController
public class MessageQueueController {
@Resource
private RabbitMQService rabbitMQService;
@RequestMapping("/send-msg")
public String sendMessage(String message) {
rabbitMQService.sendMessage(message);
return "send message success";
}
}

View File

@ -0,0 +1,43 @@
package xyz.longicorn.driver.service;
import cn.hutool.core.lang.UUID;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.longicorn.driver.config.RabbitConfig;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class RabbitMQService {
@Resource
private RabbitTemplate rabbitTemplate;
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public boolean sendMessage(String message) {
try {
String msgId = UUID.fastUUID().toString();
String sendTime = sdf.format(new Date());
Map<String, Object> map = new HashMap<>();
map.put("id", msgId);
map.put("sendTime", sendTime);
map.put("message", message);
// 发送
rabbitTemplate.convertAndSend(
RabbitConfig.RABBITMQ_DEFAULT_DIRECT_EXCHANGE, // 接收消息的交换机
RabbitConfig.RABBITMQ_DEFAULT_DIRECT_ROUTING, // 发送消息的路由
map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -61,7 +61,11 @@ spring:
# redis缓存
redis:
database: 1
rabbitmq:
host: 127.0.0.1
port: 5672
username: client
password: 123123
mvc:
pathmatch:
# 因为Springfox使用的路径匹配是基于AntPathMatcher的而Spring Boot 2.6.X使用的是PathPatternMatcher。