🔄Update: 构建生产者和消费者骨架
This commit is contained in:
+2
-1
@@ -1 +1,2 @@
|
|||||||
/.idea
|
/.idea
|
||||||
|
target
|
||||||
@@ -29,6 +29,17 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package cn.hezhaohui.rabbitmq;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息实体类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Message {
|
||||||
|
private String content;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package cn.hezhaohui.rabbitmq;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Message;
|
||||||
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息消费者
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class MessageConsumer {
|
||||||
|
|
||||||
|
// TODO: 实现消息监听方法
|
||||||
|
// TODO: 定义队列名称常量
|
||||||
|
// TODO: 可能需要添加日志记录
|
||||||
|
|
||||||
|
@RabbitListener(queues = "test.queue")
|
||||||
|
public void receiveMessage(Message message) {
|
||||||
|
// TODO: 实现消息处理逻辑
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package cn.hezhaohui.rabbitmq;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.amqp.core.AmqpTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息生产者
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Data
|
||||||
|
public class MessageProducer {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AmqpTemplate amqpTemplate;
|
||||||
|
|
||||||
|
// TODO: 实现发送消息的方法
|
||||||
|
// TODO: 定义队列名称常量
|
||||||
|
// TODO: 可能需要添加日志记录
|
||||||
|
|
||||||
|
public void sendMessage(Message message) {
|
||||||
|
// TODO: 实现发送逻辑
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package cn.hezhaohui.rabbitmq;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class RabbitMQTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MessageProducer messageProducer;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendMessage() {
|
||||||
|
// TODO: 实现生产者发送消息的测试
|
||||||
|
// 1. 创建测试消息对象
|
||||||
|
// 2. 调用messageProducer.sendMessage()方法
|
||||||
|
// 3. 验证消息是否成功发送
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReceiveMessage() {
|
||||||
|
// TODO: 实现消费者接收消息的测试
|
||||||
|
// 1. 发送测试消息到队列
|
||||||
|
// 2. 等待消息被消费
|
||||||
|
// 3. 验证消息内容是否正确
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user