当前位置: 首页 > news >正文

Rocket MQ消费消息的两种写法

Rocket MQ消费消息代码示例:

第一种继承:AbstractMessageListener


import cn.com.fsg.common.util.monitor.TraceUtils;
import cn.com.fsg.ihro.pay.constant.PayGlobalConstants;
import cn.com.fsg.ihro.pay.dto.PayApplRespDTO;
import cn.com.fsg.ihro.pay.manager.consumer.message.PayApplSingleMessage;
import cn.com.fsg.ihro.pay.support.util.StringSubUtils;
import cn.com.fsg.plugins.rocketmq.core.listener.AbstractMessageListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.Map;/*** 单笔付款结果 MQ消费** @author zkg* @since 2024-09-06 16:11:19*/
@Slf4j
@Component
@RocketMQMessageListener(topic = PayGlobalConstants.PAY_APPL_SINGLE_TOPIC, consumerGroup = PayGlobalConstants.PAY_APPL_SINGLE_CONSUMER_GROUP, selectorExpression = PayGlobalConstants.PAY_APPL_SINGLE_TAG)
public class PayApplSingleConsumer extends AbstractMessageListener<PayApplSingleMessage> {@Resourceprivate Map<String, PayApplSingleApi> serviceMap;@Overrideprotected void handleMessage(PayApplSingleMessage message) {TraceUtils.setDefaultTraceId();try {log.info("PayApplSingleConsumer====单笔付款结果====message: {}", message);PayApplRespDTO data = message.getData();// 截取编码codeString code = StringSubUtils.substring(data.getPayReqBizBatId());log.info("PayApplSingleConsumer====单笔付款结果====payReqBizBatId: {},code: {}",data.getPayReqBizBatId(), code);// 调用serviceserviceMap.get(code).handleMessage(data);log.info("PayApplSingleConsumer====单笔付款结果====end");} catch (Exception e) {e.printStackTrace();log.error("PayApplSingleConsumer====单笔付款结果====异常: {}", e.getMessage());}}}

第二种:实现RocketMQListener

/*** 付款结果消费客户端*/
@Slf4j
@RocketMQMessageListener(topic = FIN_CDPS_BATCH_PAYMENT_STATUS_TOPIC, consumerGroup = FIN_CDPS_BATCH_PAYMENT_STATUS_GROUP, selectorExpression = FIN_CDPS_BATCH_PAYMENT_STATUS_TAG)
@Component
@Profile({"local", "uat", "prod"})
public class PaymentStatusConsumer implements RocketMQListener<String> {@Resourceprivate Map<String, PaymentStatusConsumerApi> consumerMap;@PostConstructpublic void init() {consumerMap.forEach((k, v) -> {log.info("消费组件名称: {}, 组件实现类:{}", k, v.getClass());});}@Overridepublic void onMessage(String message) {TraceUtils.setDefaultTraceId();try {PaymentStatusMessage paymentStatusMessage = JsonUtils.parseObject(message, PaymentStatusMessage.class);String busNo = paymentStatusMessage.getData().getPayReqBizBatId();String[] numbers = busNo.split(SPLITS[0]);log.info("付款结果消费返回业务单号:{}", busNo);Assert.isTrue(ArrayUtil.isNotEmpty(numbers));consumerMap.get(numbers[1]).doExecute(paymentStatusMessage);} catch (Exception e) {log.error("付款结果消息消费异常: ", e);}}
}

StringSubUtils

public class StringSubUtils {/*** 截取编码code** @param str 字符串* @return 结果*/public static String substring(String str) {// 获取第一个"-"的位置并加1跳过"-"本身int startIndex = str.indexOf("-") + 1;// 从startIndex位置向后查找第二个"-"的位置int endIndex = str.indexOf("-", startIndex);// 使用substring方法截取内容return str.substring(startIndex, endIndex);}public static void main(String[] args) {String str = "text-要截取的内容-endtext-111-111";String result = substring(str);System.out.println(result);}}

http://www.mrgr.cn/news/46145.html

相关文章:

  • 【JDK17 | 7】Java 17 深入剖析:基础概述与新特性实战
  • 这个 JavaScript API 比你想象中更强大!
  • 国产长芯微LPA1311高性能隔离放大器P2P替代AMC1311替代ACPL-C87A-000E
  • 74.【C语言】文件操作(1)
  • Unity3D Compute Shader同步详解
  • 一文彻底搞懂大模型 - Hugging Face Transformers
  • Fetch获取内容乱码问题
  • 中医文献学:概念、历史、现状与展望
  • 力扣10.9
  • 2024年新能源汽车Q3季报:传统车企车机表现逆袭,问界M7暴露最大短板
  • sql-lab打不开网页了(没解决)
  • linux安装Go ImageMagick插件
  • 基于Kafka2.1解读Producer原理
  • 【Pydantic】如何在 Pydantic 中实现严格模式校验
  • Gpt4.0最新保姆级教程开通升级
  • 滚雪球学Redis[2.0讲]:Redis的数据类型与基本操作
  • L1和L2正则化的区别
  • 校园二手交易平台的小程序+ssm论文源码调试讲解
  • 并发编程-任务创建、执行-Demo
  • 项目管理之精准管理规划:引领团队迈向成功之路