Java Code Examples for org.apache.rocketmq.common.message.MessageExt#getMsgId()
The following examples show how to use
org.apache.rocketmq.common.message.MessageExt#getMsgId() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalTranListenerImpl.java From order-charge-notify with Apache License 2.0 | 6 votes |
/** * 本地事务回查: 回查依据是否存在扣款流水 * @param msg * @return */ @Override public LocalTransactionState checkLocalTransaction(MessageExt msg) { // 本地事务回查,查询扣款流水 String message = new String(msg.getBody()); String msgId = msg.getMsgId(); LOGGER.info("[扣款本地事务回查]-接收到消息,msgId={},message={}", msgId, message); WalletPaymentProtocol payProtocol = new WalletPaymentProtocol(); payProtocol.decode(message); // 获取订单号 String orderId = payProtocol.getOrderId(); ChargeRecordEntity chargeRecordEntity = walletService.queryChargeRecordByOrderId(orderId); if (chargeRecordEntity == null) { LOGGER.info("[扣款本地事务回查]-本地不存在扣款流水,消息回滚,msgId={}", msgId); return LocalTransactionState.ROLLBACK_MESSAGE; } LOGGER.info("[扣款本地事务回查]-本地存在扣款流水,消息提交,msgId={}", msgId); return LocalTransactionState.COMMIT_MESSAGE; }
Example 2
Source File: WalletPaymentMsgListenerImpl.java From order-charge-notify with Apache License 2.0 | 6 votes |
/** * 钱包扣款关键逻辑 * @param msgs * @param context * @return */ @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { try { // 默认msgs只有一条消息 for (MessageExt msg : msgs) { // 消费次数 int reconsumeTimes = msg.getReconsumeTimes(); String msgId = msg.getMsgId(); LOGGER.info("===============msgId={},消费次数={}===============", msgId, reconsumeTimes); return walletCharge(msg, msgId); } return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } catch (Exception e) { LOGGER.error("钱包扣款消费异常,e={}", e); return ConsumeConcurrentlyStatus.RECONSUME_LATER; } }
Example 3
Source File: ChargeOrderTranListenerImpl.java From order-charge-notify with Apache License 2.0 | 6 votes |
/** * 根据订单号进行回查 * @param msg * @return */ @Override public LocalTransactionState checkLocalTransaction(MessageExt msg) { String message = new String(msg.getBody()); String msgId = msg.getMsgId(); int reconsumeTimes = msg.getReconsumeTimes(); LOGGER.info("订单入库本地事务回查--接收到消息, msgId={},message={},reconsumeTimes={}", msgId, message, reconsumeTimes); // 消息解码 WalletPaymentProtocol walletPaymentProtocol = new WalletPaymentProtocol(); walletPaymentProtocol.decode(message); String orderId = walletPaymentProtocol.getOrderId(); // 订单查询 OrderInfoDO orderInfoDO = new OrderInfoDO().setOrderId(orderId); OrderInfoDobj orderInfoDobj = orderChargeService.queryOrderInfo(orderInfoDO); if (orderInfoDobj == null) { LOGGER.info("订单入库本地事务回查--本地不存在订单,[消息回滚],orderId={},msgId={}", orderId, msgId); return LocalTransactionState.ROLLBACK_MESSAGE; } LOGGER.info("订单入库本地事务回查--本地存在订单信息,orderInfoDobj={},msgId={},[消息提交]", orderInfoDobj, msgId); return LocalTransactionState.COMMIT_MESSAGE; }
Example 4
Source File: NotifySendListenerImpl.java From order-charge-notify with Apache License 2.0 | 5 votes |
@Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { try { for (MessageExt msg : msgs) { // 消息解码 String message = new String(msg.getBody()); // 消费次数 int reconsumeTimes = msg.getReconsumeTimes(); String msgId = msg.getMsgId(); String logSuffix = ",msgId=" + msgId + ",reconsumeTimes=" + reconsumeTimes; LOGGER.info("[通知发送消息消费者]-OrderNotifySendProducer-接收到消息,message={},{}", message, logSuffix); // 请求组装 OrderResultNofityProtocol protocol = new OrderResultNofityProtocol(); protocol.decode(message); // 参数加签,获取用户privatekey String privateKey = protocol.getPrivateKey(); String notifyUrl = protocol.getMerchantNotifyUrl(); String purseId = protocol.getPurseId(); ChargeNotifyRequest chargeNotifyRequest = new ChargeNotifyRequest(); chargeNotifyRequest.setChannel_orderid(protocol.getChannelOrderId()) .setFinish_time(DateUtil.formatDate(new Date(System.currentTimeMillis()))) .setOrder_status(NotifyConstant.NOTIFY_SUCCESS) .setPlat_orderid(protocol.getOrderId()) .setSign(chargeNotifyRequest.sign(privateKey)); LOGGER.info("[通知发送消息消费者]-OrderNotifySendProducer-订单结果通知入参:{},{}", chargeNotifyRequest.toString(), logSuffix); // 通知发送 return sendNotifyByPost(reconsumeTimes, logSuffix, protocol, notifyUrl, purseId, chargeNotifyRequest); } } catch (Exception e) { LOGGER.error("[通知发送消息消费者]消费异常,e={}", LogExceptionWapper.getStackTrace(e)); } return ConsumeConcurrentlyStatus.RECONSUME_LATER; }
Example 5
Source File: SecKillChargeOrderListenerImpl.java From seckill-rocketmq with Apache License 2.0 | 4 votes |
/** * 秒杀核心消费逻辑 * @param msgs * @param context * @return */ @Override public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) { try { for (MessageExt msg : msgs) { // 消息解码 String message = new String(msg.getBody()); int reconsumeTimes = msg.getReconsumeTimes(); String msgId = msg.getMsgId(); String logSuffix = ",msgId=" + msgId + ",reconsumeTimes=" + reconsumeTimes; LOGGER.info("[秒杀订单消费者]-SecKillChargeOrderConsumer-接收到消息,message={},{}", message, logSuffix); // 反序列化协议实体 ChargeOrderMsgProtocol chargeOrderMsgProtocol = new ChargeOrderMsgProtocol(); chargeOrderMsgProtocol.decode(message); LOGGER.info("[秒杀订单消费者]-SecKillChargeOrderConsumer-反序列化为秒杀入库订单实体chargeOrderMsgProtocol={},{}", chargeOrderMsgProtocol.toString(), logSuffix); // 消费幂等:查询orderId对应订单是否已存在 String orderId = chargeOrderMsgProtocol.getOrderId(); OrderInfoDobj orderInfoDobj = secKillOrderService.queryOrderInfoById(orderId); if (orderInfoDobj != null) { LOGGER.info("[秒杀订单消费者]-SecKillChargeOrderConsumer-当前订单已入库,不需要重复消费!,orderId={},{}", orderId, logSuffix); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } // 业务幂等:同一个prodId+同一个userPhoneNo只有一个秒杀订单 OrderInfoDO orderInfoDO = new OrderInfoDO(); orderInfoDO.setProdId(chargeOrderMsgProtocol.getProdId()) .setUserPhoneNo(chargeOrderMsgProtocol.getUserPhoneNo()); Result result = secKillOrderService.queryOrder(orderInfoDO); if (result != null && result.getCode().equals(CodeMsg.SUCCESS.getCode())) { LOGGER.info("[秒杀订单消费者]-SecKillChargeOrderConsumer-当前用户={},秒杀的产品={}订单已存在,不得重复秒杀,orderId={}", orderInfoDO.getUserPhoneNo(), orderInfoDO.getProdId(), orderId); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } // 秒杀订单入库 OrderInfoDO orderInfoDODB = new OrderInfoDO(); BeanUtils.copyProperties(chargeOrderMsgProtocol, orderInfoDODB); // 库存校验 String prodId = chargeOrderMsgProtocol.getProdId(); SecKillProductDobj productDobj = secKillProductService.querySecKillProductByProdId(prodId); // 取库存校验 int currentProdStock = productDobj.getProdStock(); if (currentProdStock <= 0) { LOGGER.info("[decreaseProdStock]当前商品已售罄,消息消费成功!prodId={},currStock={}", prodId, currentProdStock); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } // 正式下单 if (secKillOrderService.chargeSecKillOrder(orderInfoDODB)) { LOGGER.info("[秒杀订单消费者]-SecKillChargeOrderConsumer-秒杀订单入库成功,消息消费成功!,入库实体orderInfoDO={},{}", orderInfoDO.toString(), logSuffix); // 模拟订单处理,直接修改订单状态为处理中 secKillOrderService.updateOrderStatusDealing(orderInfoDODB); return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; } return ConsumeConcurrentlyStatus.RECONSUME_LATER; } } catch (Exception e) { LOGGER.info("[秒杀订单消费者]消费异常,e={}", LogExceptionWapper.getStackTrace(e)); } return ConsumeConcurrentlyStatus.RECONSUME_LATER; }
Example 6
Source File: QueryMsgByKeySubCommand.java From DDMQ with Apache License 2.0 | 4 votes |
public static void printMsg(final MessageExt msg) throws IOException { if (msg == null) { System.out.printf("%nMessage not found!"); return; } String bodyTmpFilePath = createBodyFile(msg); String msgId = msg.getMsgId(); if (msg instanceof MessageClientExt) { msgId = ((MessageClientExt) msg).getOffsetMsgId(); } System.out.printf("%-20s %s%n", "OffsetID:", msgId ); System.out.printf("%-20s %s%n", "Topic:", msg.getTopic() ); System.out.printf("%-20s %s%n", "Tags:", "[" + msg.getTags() + "]" ); System.out.printf("%-20s %s%n", "Keys:", "[" + msg.getKeys() + "]" ); System.out.printf("%-20s %d%n", "Queue ID:", msg.getQueueId() ); System.out.printf("%-20s %d%n", "Queue Offset:", msg.getQueueOffset() ); System.out.printf("%-20s %d%n", "CommitLog Offset:", msg.getCommitLogOffset() ); System.out.printf("%-20s %d%n", "Reconsume Times:", msg.getReconsumeTimes() ); System.out.printf("%-20s %s%n", "Born Timestamp:", UtilAll.timeMillisToHumanString2(msg.getBornTimestamp()) ); System.out.printf("%-20s %s%n", "Store Timestamp:", UtilAll.timeMillisToHumanString2(msg.getStoreTimestamp()) ); System.out.printf("%-20s %s%n", "Born Host:", RemotingHelper.parseSocketAddressAddr(msg.getBornHost()) ); System.out.printf("%-20s %s%n", "Store Host:", RemotingHelper.parseSocketAddressAddr(msg.getStoreHost()) ); System.out.printf("%-20s %d%n", "System Flag:", msg.getSysFlag() ); System.out.printf("%-20s %s%n", "Properties:", msg.getProperties() != null ? msg.getProperties().toString() : "" ); System.out.printf("%-20s %s%n", "Message Body Path:", bodyTmpFilePath ); }
Example 7
Source File: QueryMsgByKeySubCommand.java From DDMQ with Apache License 2.0 | 4 votes |
public static void printMsg(final MessageExt msg) throws IOException { if (msg == null) { System.out.printf("%nMessage not found!"); return; } String bodyTmpFilePath = createBodyFile(msg); String msgId = msg.getMsgId(); if (msg instanceof MessageClientExt) { msgId = ((MessageClientExt) msg).getOffsetMsgId(); } System.out.printf("%-20s %s%n", "OffsetID:", msgId ); System.out.printf("%-20s %s%n", "Topic:", msg.getTopic() ); System.out.printf("%-20s %s%n", "Tags:", "[" + msg.getTags() + "]" ); System.out.printf("%-20s %s%n", "Keys:", "[" + msg.getKeys() + "]" ); System.out.printf("%-20s %d%n", "Queue ID:", msg.getQueueId() ); System.out.printf("%-20s %d%n", "Queue Offset:", msg.getQueueOffset() ); System.out.printf("%-20s %d%n", "CommitLog Offset:", msg.getCommitLogOffset() ); System.out.printf("%-20s %d%n", "Reconsume Times:", msg.getReconsumeTimes() ); System.out.printf("%-20s %s%n", "Born Timestamp:", UtilAll.timeMillisToHumanString2(msg.getBornTimestamp()) ); System.out.printf("%-20s %s%n", "Store Timestamp:", UtilAll.timeMillisToHumanString2(msg.getStoreTimestamp()) ); System.out.printf("%-20s %s%n", "Born Host:", RemotingHelper.parseSocketAddressAddr(msg.getBornHost()) ); System.out.printf("%-20s %s%n", "Store Host:", RemotingHelper.parseSocketAddressAddr(msg.getStoreHost()) ); System.out.printf("%-20s %d%n", "System Flag:", msg.getSysFlag() ); System.out.printf("%-20s %s%n", "Properties:", msg.getProperties() != null ? msg.getProperties().toString() : "" ); System.out.printf("%-20s %s%n", "Message Body Path:", bodyTmpFilePath ); }
Example 8
Source File: QueryMsgByIdSubCommand.java From rocketmq with Apache License 2.0 | 4 votes |
public static void printMsg(final DefaultMQAdminExt admin, final MessageExt msg) throws IOException { if (msg == null) { System.out.printf("%nMessage not found!"); return; } String bodyTmpFilePath = createBodyFile(msg); String msgId = msg.getMsgId(); if (msg instanceof MessageClientExt) { msgId = ((MessageClientExt) msg).getOffsetMsgId(); } System.out.printf("%-20s %s%n", "OffsetID:", msgId ); System.out.printf("%-20s %s%n", "Topic:", msg.getTopic() ); System.out.printf("%-20s %s%n", "Tags:", "[" + msg.getTags() + "]" ); System.out.printf("%-20s %s%n", "Keys:", "[" + msg.getKeys() + "]" ); System.out.printf("%-20s %d%n", "Queue ID:", msg.getQueueId() ); System.out.printf("%-20s %d%n", "Queue Offset:", msg.getQueueOffset() ); System.out.printf("%-20s %d%n", "CommitLog Offset:", msg.getCommitLogOffset() ); System.out.printf("%-20s %d%n", "Reconsume Times:", msg.getReconsumeTimes() ); System.out.printf("%-20s %s%n", "Born Timestamp:", UtilAll.timeMillisToHumanString2(msg.getBornTimestamp()) ); System.out.printf("%-20s %s%n", "Store Timestamp:", UtilAll.timeMillisToHumanString2(msg.getStoreTimestamp()) ); System.out.printf("%-20s %s%n", "Born Host:", RemotingHelper.parseSocketAddressAddr(msg.getBornHost()) ); System.out.printf("%-20s %s%n", "Store Host:", RemotingHelper.parseSocketAddressAddr(msg.getStoreHost()) ); System.out.printf("%-20s %d%n", "System Flag:", msg.getSysFlag() ); System.out.printf("%-20s %s%n", "Properties:", msg.getProperties() != null ? msg.getProperties().toString() : "" ); System.out.printf("%-20s %s%n", "Message Body Path:", bodyTmpFilePath ); try { List<MessageTrack> mtdList = admin.messageTrackDetail(msg); if (mtdList.isEmpty()) { System.out.printf("%n%nWARN: No Consumer"); } else { System.out.printf("%n%n"); for (MessageTrack mt : mtdList) { System.out.printf("%s", mt); } } } catch (Exception e) { e.printStackTrace(); } }