Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/cn/van/business/mq/MessageConsumerService.java
This commit is contained in:
@@ -3,17 +3,20 @@ package cn.van.business.mq;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.van.business.util.WxtsUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static cn.hutool.core.thread.ThreadUtil.sleep;
|
||||
import static cn.van.business.util.WXUtil.WX_BASE_URL;
|
||||
|
||||
/**
|
||||
@@ -28,32 +31,41 @@ public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageConsumerService.class);
|
||||
|
||||
private final WxtsUtil wxtsUtil;
|
||||
// create a rate limiter of 1 qps
|
||||
RateLimiter rateLimiter = RateLimiter.create(0.5);
|
||||
|
||||
@Autowired
|
||||
public MessageConsumerService(WxtsUtil wxtsUtil) {
|
||||
this.wxtsUtil = wxtsUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(JSONObject jsonObject) {
|
||||
// 处理消息
|
||||
try {
|
||||
rateLimiter.acquire(); // 请求许可。如果超过速率,则此方法会阻塞
|
||||
//logger.info("消费到消息:{}", jsonObject);
|
||||
String body = jsonObject.getString("body");
|
||||
byte[] decodedBody = Base64.getDecoder().decode(body);
|
||||
String decodedBodyStr = new String(decodedBody, StandardCharsets.UTF_8);
|
||||
JSONObject decodedBodyJson = JSONObject.parseObject(decodedBodyStr);
|
||||
String jsonStr = JSONUtil.toJsonStr(decodedBodyJson);
|
||||
String responseStr = HttpRequest.post(WX_BASE_URL)
|
||||
.body(JSONUtil.toJsonStr(decodedBodyJson))
|
||||
.body(jsonStr)
|
||||
.execute()
|
||||
.body();
|
||||
|
||||
logger.info("消费消息:{}", jsonStr);
|
||||
if (ObjectUtil.isNotEmpty(responseStr)) {
|
||||
JSONObject response = JSONObject.parseObject(responseStr);
|
||||
logger.info("消息成功发送并得到响应:{}", response);
|
||||
if (response.getInteger("code") != 200) {
|
||||
// TODO: 如果需要处理错误,您可以在这里添加逻辑
|
||||
wxtsUtil.sendNotify("消息发送失败: " + responseStr);
|
||||
throw new RuntimeException("消息发送失败: " + responseStr);
|
||||
}
|
||||
//logger.info("消息成功发送并得到响应:{}", response);
|
||||
} else {
|
||||
wxtsUtil.sendNotify("消息发送失败,没有收到响应");
|
||||
throw new RuntimeException("消息发送失败,没有收到响应");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user