1
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
package cn.van.business.mq;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.van.business.util.WxtsUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import org.apache.rocketmq.spring.annotation.ConsumeMode;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
@@ -13,11 +10,6 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.van.business.util.WXUtil.WX_BASE_URL;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @version 1.0
|
||||
@@ -46,35 +38,34 @@ public class MessageConsumerService implements RocketMQListener<JSONObject> {
|
||||
public void onMessage(JSONObject message) {
|
||||
try {
|
||||
logger.info("消费消息:{}", message);
|
||||
//logger.info("[RateLimiter] 开始处理消息,当前时间:{}", System.currentTimeMillis());
|
||||
//rateLimiter.acquire();
|
||||
//logger.info("[RateLimiter] 获得令牌,当前时间:{}", System.currentTimeMillis());
|
||||
|
||||
// 解析消息类型和数据
|
||||
//String type = message.getString("type");
|
||||
JSONObject data = message.getJSONObject("data");
|
||||
//
|
||||
//if (data == null) {
|
||||
// logger.error("消息数据为空:{}", message);
|
||||
// return;
|
||||
//}
|
||||
//
|
||||
String wxid = data.getString("wxid");
|
||||
//if (wxid == null || wxid.isEmpty()) {
|
||||
// logger.error("消息缺少wxid字段:{}", message);
|
||||
// return;
|
||||
//}
|
||||
|
||||
// 根据消息类型调用不同的wxts接口
|
||||
|
||||
// 发送文本消息
|
||||
String content = data.getString("msg");
|
||||
Integer msgType = data.getInteger("msgType");
|
||||
String fromWxid = data.getString("fromWxid");
|
||||
Boolean hiddenTime = data.getBoolean("hiddenTime");
|
||||
|
||||
wxtsUtil.sendWxTextMessage(wxid, content, msgType, fromWxid, hiddenTime);
|
||||
|
||||
|
||||
//logger.debug("构造完成的消息结构:{}", requestBody.toJSONString());
|
||||
|
||||
// 4. 发送请求(保持原有)
|
||||
String responseStr;
|
||||
|
||||
// 修改onMessage方法中的HTTP请求部分
|
||||
responseStr = HttpRequest.post(WX_BASE_URL).header("Content-Type", "application/json; charset=UTF-8") // 明确指定编码
|
||||
.body(message.toJSONString().getBytes(StandardCharsets.UTF_8)) // 显式转为UTF-8字节
|
||||
.execute().charset("UTF-8") // 强制响应体使用UTF-8解码
|
||||
.body();
|
||||
// ... [保持原有响应处理逻辑]
|
||||
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) {
|
||||
logger.error("消息处理失败,原始消息:{}", message, e);
|
||||
wxtsUtil.sendNotify("系统异常:" + e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user