Skip to content

Commit 007c569

Browse files
committed
feat: example 添加测试 mq 发送 text json 类型消息的接口
1 parent 5d3a472 commit 007c569

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.github.cadecode.uniboot.example.svc.bean.data;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* 测试消息 DO
10+
*
11+
* @author Cade Li
12+
* @since 2023/8/20
13+
*/
14+
@Data
15+
@AllArgsConstructor
16+
@NoArgsConstructor
17+
@Builder
18+
public class ExampleMsgDo {
19+
20+
private String testKey;
21+
22+
private String testVal;
23+
}

example/example_svc/src/main/java/com/github/cadecode/uniboot/example/svc/consumer/RabbitExampleConsumer.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.cadecode.uniboot.example.svc.consumer;
22

33
import cn.hutool.core.util.ObjectUtil;
4+
import com.github.cadecode.uniboot.example.svc.bean.data.ExampleMsgDo;
45
import com.rabbitmq.client.Channel;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.amqp.core.Message;
@@ -19,18 +20,36 @@
1920
@Component
2021
public class RabbitExampleConsumer {
2122

22-
@RabbitListener(queues = "demo-queue-0", id = "demo-queue-0", ackMode = "AUTO")
23-
public void demoQueue0(String body, Message message, Channel channel) throws IOException {
23+
@RabbitListener(queues = "example-delay-queue-0", id = "example-delay-queue-0", ackMode = "AUTO")
24+
public void exampleDelayQueue0(String body, Message message, Channel channel) throws IOException {
25+
log.info("Received msg:{}", body);
26+
}
27+
28+
/**
29+
* 测试 SpEL 获取 queue name
30+
*/
31+
@RabbitListener(queues = "#{@'example-delay-queue-1'.name}", id = "example-delay-queue-1")
32+
public void exampleDelayQueue1(String body, Message message, Channel channel) throws IOException {
33+
log.info("Received msg:{}", body);
34+
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
35+
}
36+
37+
38+
@RabbitListener(queues = "example-biz-queue-0", id = "example-biz-queue-0", ackMode = "AUTO")
39+
public void exampleBizQueue0(String body, Message message, Channel channel) throws IOException {
2440
log.info("Received msg:{}", body);
2541
// 自动模式下,测试重试机制
2642
if (ObjectUtil.equal(body, "ERROR")) {
2743
throw new RuntimeException("ERROR");
2844
}
2945
}
3046

31-
@RabbitListener(queues = "#{@'demo-queue-1'.name}", id = "demo-queue-1")
32-
public void demoQueue1(String body, Message message, Channel channel) throws IOException {
33-
log.info("Received msg:{}", body);
34-
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
47+
/**
48+
* 测试用对象获取消息
49+
*/
50+
@RabbitListener(queues = "example-biz-queue-1", id = "example-biz-queue-1", ackMode = "AUTO")
51+
public void exampleBizQueue1(ExampleMsgDo msgDo, Message message, Channel channel) throws IOException {
52+
log.info("Received msg:{}", msgDo);
3553
}
54+
3655
}

example/example_svc/src/main/java/com/github/cadecode/uniboot/example/svc/controller/MqExampleController.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.github.cadecode.uniboot.example.svc.controller;
22

3+
import com.github.cadecode.uniboot.common.plugin.mq.util.RabbitUtil;
4+
import com.github.cadecode.uniboot.example.svc.bean.data.ExampleMsgDo;
35
import com.github.cadecode.uniboot.framework.base.annotation.ApiFormat;
46
import io.swagger.annotations.Api;
57
import io.swagger.annotations.ApiOperation;
68
import lombok.RequiredArgsConstructor;
79
import lombok.extern.slf4j.Slf4j;
8-
import org.springframework.amqp.rabbit.core.RabbitTemplate;
910
import org.springframework.web.bind.annotation.GetMapping;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112
import org.springframework.web.bind.annotation.RequestParam;
@@ -25,16 +26,25 @@
2526
@RequestMapping("demo/mq")
2627
public class MqExampleController {
2728

28-
private final RabbitTemplate rabbitTemplate;
29-
3029
@ApiOperation("发送 delay 消息")
3130
@GetMapping("send_delay")
32-
public boolean sendDelay(@RequestParam String exc, @RequestParam String routingKey,
33-
@RequestParam String msg, @RequestParam Integer ms) {
34-
rabbitTemplate.convertAndSend(exc, routingKey, msg, message -> {
35-
message.getMessageProperties().setDelay(ms);
36-
return message;
37-
});
31+
public boolean sendDelay(@RequestParam String exchange, @RequestParam String routingKey, @RequestParam Integer ms) {
32+
RabbitUtil.sendDelay(exchange, routingKey, "Test delay msg", ms);
33+
return true;
34+
}
35+
36+
@ApiOperation("发送字符串消息")
37+
@GetMapping("send_str")
38+
public boolean sendStr(@RequestParam String exchange, @RequestParam String routingKey) {
39+
RabbitUtil.send(exchange, routingKey, "Test str msg");
40+
return true;
41+
}
42+
43+
@ApiOperation("发送对象消息")
44+
@GetMapping("send_obj")
45+
public boolean sendObj(@RequestParam String exchange, @RequestParam String routingKey) {
46+
ExampleMsgDo msgDo = new ExampleMsgDo("Test key", "Test name");
47+
RabbitUtil.send(exchange, routingKey, msgDo);
3848
return true;
3949
}
4050
}

0 commit comments

Comments
 (0)