Java Code Examples for org.apache.rocketmq.common.message.MessageExt#getReconsumeTimes()

The following examples show how to use org.apache.rocketmq.common.message.MessageExt#getReconsumeTimes() . 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: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 2
Source File: ConsumeMessageOrderlyService.java    From rocketmq-4.3.0 with Apache License 2.0 6 votes vote down vote up
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 3
Source File: WalletPaymentMsgListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 钱包扣款关键逻辑
 * @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 4
Source File: ChargeOrderTranListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 根据订单号进行回查
 * @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 5
Source File: ConsumeMessageOrderlyService.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 检查消息重新消费的次数
 * @param msgs msgs
 * @return 返回是否要挂起
 */
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 6
Source File: ConsumeMessageOrderlyService.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 7
Source File: MQConsumeMsgListenerProcessor.java    From learning-code with Apache License 2.0 6 votes vote down vote up
/**
 * 默认msgs里只有一条消息,可以通过设置consumeMessageBatchMaxSize参数来批量接收消息<br/>
 * 不要抛异常,如果没有return CONSUME_SUCCESS ,consumer会重新消费该消息,直到return CONSUME_SUCCESS
 */
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
    if (CollectionUtils.isEmpty(msgs)) {
        logger.info("接受到的消息为空,不处理,直接返回成功");
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
    MessageExt messageExt = msgs.get(0);
    logger.info("接受到的消息为:" + messageExt.toString());
    if (messageExt.getTopic().equals("你的Topic")) {
        if (messageExt.getTags().equals("你的Tag")) {
            //TODO 判断该消息是否重复消费(RocketMQ不保证消息不重复,如果你的业务需要保证严格的不重复消息,需要你自己在业务端去重)
            //TODO 获取该消息重试次数
            int reconsume = messageExt.getReconsumeTimes();
            if (reconsume == 3) {//消息已经重试了3次,如果不需要再次消费,则返回成功
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
            //TODO 处理对应的业务逻辑
        }
    }
    // 如果没有return success ,consumer会重新消费该消息,直到return success
    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
 
Example 8
Source File: ConsumeMessageOrderlyService.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 9
Source File: ConsumeMessageOrderlyService.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
private boolean checkReconsumeTimes(List<MessageExt> msgs) {
    boolean suspend = false;
    if (msgs != null && !msgs.isEmpty()) {
        for (MessageExt msg : msgs) {
            if (msg.getReconsumeTimes() >= getMaxReconsumeTimes()) {
                MessageAccessor.setReconsumeTime(msg, String.valueOf(msg.getReconsumeTimes()));
                if (!sendMessageBack(msg)) {
                    suspend = true;
                    msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
                }
            } else {
                suspend = true;
                msg.setReconsumeTimes(msg.getReconsumeTimes() + 1);
            }
        }
    }
    return suspend;
}
 
Example 10
Source File: NotifySendListenerImpl.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: RocketMQListenerBindingContainer.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
/**
 * Convert rocketmq {@link MessageExt} to Spring {@link Message}.
 * @param messageExt the rocketmq message
 * @return the converted Spring {@link Message}
 */
@SuppressWarnings("unchecked")
private Message convertToSpringMessage(MessageExt messageExt) {

	// add reconsume-times header to messageExt
	int reconsumeTimes = messageExt.getReconsumeTimes();
	messageExt.putUserProperty(ROCKETMQ_RECONSUME_TIMES,
			String.valueOf(reconsumeTimes));
	Message message = RocketMQUtil.convertToSpringMessage(messageExt);
	return MessageBuilder.fromMessage(message)
			.copyHeaders(headerMapper.toHeaders(messageExt.getProperties())).build();
}
 
Example 12
Source File: SecKillChargeOrderListenerImpl.java    From seckill-rocketmq with Apache License 2.0 4 votes vote down vote up
/**
 * 秒杀核心消费逻辑
 * @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;
}