org.apache.rocketmq.client.producer.TransactionSendResult Java Examples

The following examples show how to use org.apache.rocketmq.client.producer.TransactionSendResult. 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: Producer.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * 事务消息
 */
private void sendTransaction() throws InterruptedException {
    String[] tags = new String[]{"TagA", "TagB", "TagC", "TagD", "TagE"};
    for (int i = 0; i < 10; i++) {
        try {
            Message message = MessageBuilder.withPayload("Hello RocketMQ " + i)
                    .setHeader(RocketMQHeaders.KEYS, "KEY_" + i).build();
            TransactionSendResult sendResult = rocketMQTemplate.sendMessageInTransaction(TX_PGROUP_NAME,
                    springTransTopic + ":" + tags[i % tags.length], message, null);
            System.out.printf("------send transaction message body =%s,sendResult = %s %n",
                    message.getPayload(), sendResult.getSendStatus());
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example #2
Source File: RocketMQTemplate.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
/**
 * Send Spring Message in Transaction
 *
 * @param destination destination formats: `topicName:tags`
 * @param message message {@link org.springframework.messaging.Message}
 * @param arg ext arg
 * @return TransactionSendResult
 * @throws MessagingException
 */
public TransactionSendResult sendMessageInTransaction(final String destination,
    final Message<?> message, final Object arg) throws MessagingException {
    try {
        if (((TransactionMQProducer) producer).getTransactionListener() == null) {
            throw new IllegalStateException("The rocketMQTemplate does not exist TransactionListener");
        }
        org.apache.rocketmq.common.message.Message rocketMsg = this.createRocketMqMessage(destination, message);
        return producer.sendMessageInTransaction(rocketMsg, arg);
    } catch (MQClientException e) {
        throw RocketMQUtil.convert(e);
    }
}
 
Example #3
Source File: WalletPaymentMsgListenerImpl.java    From order-charge-notify with Apache License 2.0 4 votes vote down vote up
/**
 * 钱包扣款,并插入扣款流水
 *
 * @param msg
 */
private ConsumeConcurrentlyStatus walletCharge(MessageExt msg, String msgId) {
    String message = new String(msg.getBody());
    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("[扣款本地事务回查]-本地已经存在orderId=[{}]对应的扣款流水,不需要重复消费,msgId={}", orderId, msgId);
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    try {
        // 组装半消息:扣款成功修改订单状态为成功,消息事件=修改订单支付状态
        OrderStatusUpdateProtocol orderStatusUpdateProtocol = new OrderStatusUpdateProtocol();
        orderStatusUpdateProtocol.setTopicName(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getTopic());
        orderStatusUpdateProtocol.setOrderId(payProtocol.getOrderId())
                .setChargeMoney(payProtocol.getChargeMoney())
                .setPurseId(payProtocol.getPurseId())
                .setMerchantName(payProtocol.getMerchantName())
                .setEventType(UpdateEventTypeConst.EVENT_UPDATE_PAY_STATUS.getEventType());

        Message updateOrderStatusMsg =
                new Message(MessageProtocolConst.ORDER_STATUS_UPDATE_TOPIC.getTopic(),
                        orderStatusUpdateProtocol.encode().getBytes());
        // 半消息发送
        TransactionSendResult transactionSendResult = orderStatusUpdateProducer.getProducer()
                /**
                 * sendMessageInTransaction(final Message msg,
                 * final Object arg) 第二个参数为回调参数,可以为null,
                 * 该参数和LocalTransactionState executeLocalTransaction(Message msg, Object arg)第二个参数为同一个值
                 */
                .sendMessageInTransaction(updateOrderStatusMsg, null);
        if (transactionSendResult == null) {
            // 发送未知重新消费
            LOGGER.info("msgId={},订单状态更新半消息发送状态未知,消费状态[RECONSUME_LATER],等待重新消费,orderId={}", msgId, payProtocol.getOrderId(), message);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
        if (transactionSendResult.getLocalTransactionState().equals(LocalTransactionState.COMMIT_MESSAGE)) {
            LOGGER.info("msgId={},订单状态更新半消息发送成功,消费状态[CONSUME_SUCCESS],orderId={},sendResult={}", msgId, payProtocol.getOrderId(), transactionSendResult);
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
        if (transactionSendResult.getLocalTransactionState().equals(LocalTransactionState.UNKNOW)) {
            LOGGER.warn("msgId={},订单状态更新本地事务执行状态未知,半消息发送未知,消费状态[RECONSUME_LATER],orderId={},sendResult={}", msgId, payProtocol.getOrderId(), transactionSendResult);
            return ConsumeConcurrentlyStatus.RECONSUME_LATER;
        }
    } catch (Exception e) {
        LOGGER.error("msgId={},订单状态更新半消息发送异常,消费状态[RECONSUME_LATER],e={}", msgId, LogExceptionWapper.getStackTrace(e));
    }
    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
}