阿里云rocketmq用法
package com.sunxing.service.merchants.config;
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.sunxing.service.merchants.listener.RocketConsumerListener;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Properties;
/**
* rocketMQ消费者配置
* @author vincen
*/
@Configuration
@Getter
@Slf4j
public class ConsumerConfig {
@Value("${workorder.rocketmq.name-server}")
private String nameServer;
@Value("${workorder.rocketmq.consumer.access-key}")
private String accessKey;
@Value("${workorder.rocketmq.consumer.secret-key}")
private String secretKey;
@Value("${workorder.rocketmq.consumer.topic}")
private String topic;
@Value("${workorder.rocketmq.consumer.tag}")
private String tag;
@Value("${workorder.rocketmq.consumer.group-id}")
private String groupId;
@Value("${workorder.rocketmq.consumer.timeout-mills}")
private String timeoutMills;
/**
*
* @return
*/
@Bean
public RocketConsumerListener rocketConsumerListener() {
return new RocketConsumerListener();
}
@PostConstruct
public void init(){
Properties props = new Properties();
props.put(PropertyKeyConst.NAMESRV_ADDR, nameServer);
props.put(PropertyKeyConst.GROUP_ID, groupId);
props.put(PropertyKeyConst.AccessKey, accessKey);
props.put(PropertyKeyConst.SecretKey, secretKey);
props.put(PropertyKeyConst.MaxReconsumeTimes, 3);
props.put(PropertyKeyConst.SendMsgTimeoutMillis, timeoutMills);
Consumer consumer = ONSFactory.createConsumer(props);
consumer.subscribe(topic, tag, rocketConsumerListener());
consumer.start();
log.info("--- RocketMq Consumer Runner Started OK.");
}
}
package com.sunxing.service.merchants.listener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.sunxing.service.merchants.constant.SceneType;
import com.sunxing.service.merchants.service.MerchantsPlatMerchantService;
import lombok.extern.slf4j.Slf4j;
import net.sunxing.workorder.data.dto.response.mq.MsgDto;
import net.sunxing.workorder.data.dto.response.mq.NotifyMsgRepsDto;
import net.sunxing.workorder.data.dto.response.operator.OperatorDto;
import net.sunxing.workorder.data.dto.response.task.TaskDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import static com.sunxing.service.merchants.constant.CommonConstants.OPEN_WORK_ORDER;
/**
* rocketMq消费监听器
*
* @author vincen
*/
@Slf4j
public class RocketConsumerListener implements MessageListener {
/**
*
*/
private static final Logger logger = LoggerFactory.getLogger(RocketConsumerListener.class);
/**
* 消费消息
* @param message
* @param consumeContext
* @return
*/
@Override
public Action consume(Message message, ConsumeContext consumeContext) {
try {
messagePrinter(message);
//MsgDto<NotifyMsgRepsDto> result = JsonUtils.json2Object(new String(message.getBody()), MsgDto.class);
MsgDto<NotifyMsgRepsDto> result = JSON.parseObject(new String(message.getBody()),
new TypeReference<MsgDto<NotifyMsgRepsDto>>(){});
if (result!=null){
}else
{
}
} catch (Exception e) {
// if (message.getReconsumeTimes() >= 3) {
// return Action.CommitMessage;
// }
return Action.ReconsumeLater;
}
return Action.CommitMessage;
}
/**
* 打印消息
*
* @param message
*/
private void messagePrinter(Message message) {
String msg = String.format("messageID = %s, value = %s", message.getMsgID(), new String(message.getBody()));
log.info("招商rocketMq消费监听器收到topic:Tag为[ {}:{} ]的rocketMQ消息: {}", message.getTopic(), message.getTag(), msg);
}
/**
* 当消费遇到异常时,打印error消息
*
* @param message
* @param e
* @return
*/
private String errorMessagePrinter(Message message, Exception e) {
String ERROR_MSG_TEMPLATE = "rocketMQ[ topic=%s, tag=%s, msgId=%s, message=%s ]消费遇到异常:%s";
return String.format(ERROR_MSG_TEMPLATE, message.getTopic(), message.getTag(), message.getMsgID(),
new String(message.getBody()), e.getMessage());
}
}
阿里云可以和本地进行联调 message_id
//MsgDto<NotifyMsgRepsDto> result = JsonUtils.json2Object(new String(message.getBody()), MsgDto.class);
MsgDto<NotifyMsgRepsDto> result = JSON.parseObject(new String(message.getBody()),
new TypeReference<MsgDto<NotifyMsgRepsDto>>(){});
小蚊子大人
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~