Java Code Examples for org.apache.rocketmq.client.producer.LocalTransactionState#UNKNOW

The following examples show how to use org.apache.rocketmq.client.producer.LocalTransactionState#UNKNOW . 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: TransactionListenerImpl.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * 可以根据由MQ回传的key去数据库查询,这条数据到底是成功了还是失败了。
 *
 * @param messageExt
 * @return
 */
@Override
public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
    Integer status = localTrans.get(messageExt.getTransactionId());
    if (null != status) {
        switch (status) {
            case 0:
                return LocalTransactionState.UNKNOW;
            case 1:
                return LocalTransactionState.COMMIT_MESSAGE;
            default:
                return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    }
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 2
Source File: TransactionalMsgIT.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
static Pair<Boolean, LocalTransactionState> getTransactionHandle(int msgIndex) {
    switch (msgIndex % 5) {
        case 0:
            //commit immediately
            return new Pair<>(true, LocalTransactionState.COMMIT_MESSAGE);
        case 1:
            //rollback immediately
            return new Pair<>(true, LocalTransactionState.ROLLBACK_MESSAGE);
        case 2:
            //commit in check
            return new Pair<>(false, LocalTransactionState.COMMIT_MESSAGE);
        case 3:
            //rollback in check
            return new Pair<>(false, LocalTransactionState.ROLLBACK_MESSAGE);
        case 4:
        default:
            return new Pair<>(false, LocalTransactionState.UNKNOW);

    }
}
 
Example 3
Source File: TransactionListenerImpl.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    Integer status = localTrans.get(msg.getTransactionId());
    if (null != status) {
        switch (status) {
            case 0:
                return LocalTransactionState.UNKNOW;
            case 1:
                return LocalTransactionState.COMMIT_MESSAGE;
            case 2:
                return LocalTransactionState.ROLLBACK_MESSAGE;
            default:
                return LocalTransactionState.COMMIT_MESSAGE;
        }
    }
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 4
Source File: ChargeOrderTranListenerImpl.java    From order-charge-notify with Apache License 2.0 6 votes vote down vote up
/**
 * 执行本地订单入库操作
 * @param msg
 * @param arg
 * @return
 */
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    // 消息解码
    String message = new String(msg.getBody());
    WalletPaymentProtocol walletPaymentProtocol = new WalletPaymentProtocol();
    walletPaymentProtocol.decode(message);
    LOGGER.info("订单入库实体WalletPaymentProtocol={}", walletPaymentProtocol.toString());
    // 组装下单实体
    OrderInfoDO orderInfoDO = new OrderInfoDO();
    BeanUtils.copyProperties(walletPaymentProtocol, orderInfoDO);
    String orderId = orderInfoDO.getOrderId();
    // 执行下单操作
    try {
        if (!orderChargeService.insertOrder(orderInfoDO)) {
            LOGGER.error("订单入库失败,事务消息回滚,LocalTransactionState={},orderId={}", LocalTransactionState.ROLLBACK_MESSAGE, orderId);
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    } catch (Exception e) {
        LOGGER.error("订单入库异常,等待回查发起,orderId={},e={}", orderId, LogExceptionWapper.getStackTrace(e));
        return LocalTransactionState.UNKNOW;
    }
    LOGGER.info("订单入库成功,orderId={}", orderId);
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 5
Source File: TransactionListenerImpl.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    Integer status = localTrans.get(msg.getTransactionId());
    if (null != status) {
        switch (status) {
            case 0:
                return LocalTransactionState.UNKNOW;
            case 1:
                return LocalTransactionState.COMMIT_MESSAGE;
            case 2:
                return LocalTransactionState.ROLLBACK_MESSAGE;
            default:
                return LocalTransactionState.COMMIT_MESSAGE;
        }
    }
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 6
Source File: TransactionListenerImpl.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
	Integer status = localTrans.get(msg.getTransactionId());
	System.out.println("checkLocalTransaction:status = " + status);
	if (null != status) {
		switch (status) {
		case 0:
			return LocalTransactionState.UNKNOW;
		case 1:
			return LocalTransactionState.COMMIT_MESSAGE;
		case 2:
			return LocalTransactionState.ROLLBACK_MESSAGE;
		default:
			return LocalTransactionState.COMMIT_MESSAGE;
		}
	}
	return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 7
Source File: TransactionProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransactionBranch(final Message msg, final Object arg) {
    if (ischeck) {
        return LocalTransactionState.UNKNOW;
    }
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 8
Source File: TransactionExecuterImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransactionBranch(final Message msg, final Object arg) {
    int value = transactionIndex.getAndIncrement();

    if (value == 0) {
        throw new RuntimeException("Could not find db");
    } else if ((value % 5) == 0) {
        return LocalTransactionState.ROLLBACK_MESSAGE;
    } else if ((value % 4) == 0) {
        return LocalTransactionState.COMMIT_MESSAGE;
    }

    return LocalTransactionState.UNKNOW;
}
 
Example 9
Source File: TransactionListenerImpl.java    From java-tutorial with MIT License 5 votes vote down vote up
/**
 * 执行本地事务
 *
 * @param message
 * @param o
 * @return
 */
@Override
public LocalTransactionState executeLocalTransaction(Message message, Object o) {
    int value = transactionIndex.getAndIncrement();
    int status = value % 3;
    localTrans.put(message.getTransactionId(), status);

    return LocalTransactionState.UNKNOW;
}
 
Example 10
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransactionBranch(final Message msg, final Object arg) {
    if (ischeck) {
        return LocalTransactionState.UNKNOW;
    }
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 11
Source File: TransactionCheckListenerImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    System.out.printf("server checking TrMsg " + msg.toString() + "%n");

    int value = transactionIndex.getAndIncrement();
    if ((value % 6) == 0) {
        throw new RuntimeException("Could not find db");
    } else if ((value % 5) == 0) {
        return LocalTransactionState.ROLLBACK_MESSAGE;
    } else if ((value % 4) == 0) {
        return LocalTransactionState.COMMIT_MESSAGE;
    }

    return LocalTransactionState.UNKNOW;
}
 
Example 12
Source File: TransactionExecuterImpl.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransactionBranch(final Message msg, final Object arg) {
    int value = transactionIndex.getAndIncrement();

    if (value == 0) {
        throw new RuntimeException("Could not find db");
    } else if ((value % 5) == 0) {
        return LocalTransactionState.ROLLBACK_MESSAGE;
    } else if ((value % 4) == 0) {
        return LocalTransactionState.COMMIT_MESSAGE;
    }

    return LocalTransactionState.UNKNOW;
}
 
Example 13
Source File: RocketMQUtil.java    From rocketmq-spring with Apache License 2.0 5 votes vote down vote up
private static LocalTransactionState convertLocalTransactionState(RocketMQLocalTransactionState state) {
    switch (state) {
        case UNKNOWN:
            return LocalTransactionState.UNKNOW;
        case COMMIT:
            return LocalTransactionState.COMMIT_MESSAGE;
        case ROLLBACK:
            return LocalTransactionState.ROLLBACK_MESSAGE;
    }

    // Never happen
    log.warn("Failed to covert enum type RocketMQLocalTransactionState {}.", state);
    return LocalTransactionState.UNKNOW;
}
 
Example 14
Source File: TransactionListenerImpl.java    From gpmall with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
	int value = transactionIndex.getAndIncrement();
	int status = value % 3;
	localTrans.put(msg.getTransactionId(), status);
	return LocalTransactionState.UNKNOW;
}
 
Example 15
Source File: LocalTranListenerImpl.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
/**
 * 扣款本地事务
 * @param msg
 * @param arg
 * @return
 */
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    String message = new String(msg.getBody());
    LOGGER.info("[扣款本地事务监听回调]执行逻辑--接收到消息, message={}", message);
    OrderStatusUpdateProtocol protocol = new OrderStatusUpdateProtocol();
    protocol.decode(message);
    // 扣款
    String purseId = protocol.getPurseId();
    String merchantName = protocol.getMerchantName();
    LOGGER.info("[扣款本地事务监听回调]反序列化扣款消息成功,开始进行扣款操作,purseId={}", purseId);
    WalletEntity walletEntity = new WalletEntity();
    walletEntity.setChargeMoney(
            new BigDecimal(protocol.getChargeMoney()))
            .setPurseId(purseId)
            .setMerchantName(merchantName);
    // 查询当前账户信息获取版本号,基于乐观锁更新
    WalletEntity realWalletInfo = walletService.queryWalletInfoByPurseId(protocol.getPurseId());
    // 判断是否足够扣减
    BigDecimal currBalanceAccount = realWalletInfo.getBalanceAccount();
    if (currBalanceAccount.subtract(new BigDecimal(protocol.getChargeMoney())).longValue() < 0) {
        LOGGER.error("执行钱包扣款,账户不足扣减,消息回滚.purseId={}", purseId);
        return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    // 足够扣减,进行账户扣减
    int version = realWalletInfo.getVersion();
    walletEntity.setVersion(version);
    try {
        if (!walletService.updateWallet(walletEntity, protocol.getOrderId())) {
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
    } catch (Exception e) {
        LOGGER.error("执行钱包扣款本地扣款异常,purseId={},e={}", purseId, LogExceptionWapper.getStackTrace(e));
        return LocalTransactionState.UNKNOW;
    }
    LOGGER.info("[扣款本地事务监听回调]扣款本地操作成功,本地事务提交,purseId={}", purseId);
    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 16
Source File: TransactionCheckListenerImpl.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    System.out.printf("server checking TrMsg %s%n", msg);

    int value = transactionIndex.getAndIncrement();
    if ((value % 6) == 0) {
        throw new RuntimeException("Could not find db");
    } else if ((value % 5) == 0) {
        return LocalTransactionState.ROLLBACK_MESSAGE;
    } else if ((value % 4) == 0) {
        return LocalTransactionState.COMMIT_MESSAGE;
    }

    return LocalTransactionState.UNKNOW;
}
 
Example 17
Source File: TransactionalMsgIT.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    LocalTransactionState state = checkStatus.get(msg.getTransactionId());
    if (state == null) {
        return LocalTransactionState.UNKNOW;
    } else {
        return state;
    }
}
 
Example 18
Source File: TransactionalMsgIT.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
    Pair<Boolean, LocalTransactionState> transactionHandle = (Pair<Boolean,LocalTransactionState>) arg;
    if (transactionHandle.getObject1()) {
        return transactionHandle.getObject2();
    } else {
        checkStatus.put(msg.getTransactionId(), transactionHandle.getObject2());
        return LocalTransactionState.UNKNOW;
    }
}
 
Example 19
Source File: TransactionListenerImpl.java    From gpmall with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
	Integer status = localTrans.get(msg.getTransactionId());
	if (null != status) {
		switch (status) {
			case 0:
				return LocalTransactionState.UNKNOW;
			case 1:
				return LocalTransactionState.COMMIT_MESSAGE;
			case 2:
				return LocalTransactionState.ROLLBACK_MESSAGE;
		}
	}
	return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 20
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    MsgMeta msgMeta = parseFromMsg(msg);
    if (msgMeta.batchId != sendConfig.batchId) {
        // message not generated in this test
        return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    statBenchmark.getCheckCount().incrementAndGet();

    int times = 0;
    try {
        String checkTimes = msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES);
        times = Integer.parseInt(checkTimes);
    } catch (Exception e) {
        return LocalTransactionState.ROLLBACK_MESSAGE;
    }
    times = times <= 0 ? 1 : times;

    boolean dup;
    synchronized (cache) {
        Integer oldCheckLog = cache.get(msgMeta.msgId);
        Integer newCheckLog;
        if (oldCheckLog == null) {
            newCheckLog = 1 << (times - 1);
        } else {
            newCheckLog = oldCheckLog | (1 << (times - 1));
        }
        dup = newCheckLog.equals(oldCheckLog);
    }
    if (dup) {
        statBenchmark.getDuplicatedCheckCount().incrementAndGet();
    }
    if (msgMeta.sendResult != LocalTransactionState.UNKNOW) {
        System.out.printf("%s unexpected check: msgId=%s,txId=%s,checkTimes=%s,sendResult=%s\n",
                new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()),
                msg.getMsgId(), msg.getTransactionId(),
                msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES),
                msgMeta.sendResult.toString());
        statBenchmark.getUnexpectedCheckCount().incrementAndGet();
        return msgMeta.sendResult;
    }

    for (int i = 0; i < times - 1; i++) {
        LocalTransactionState s = msgMeta.checkResult.get(i);
        if (s != LocalTransactionState.UNKNOW) {
            System.out.printf("%s unexpected check: msgId=%s,txId=%s,checkTimes=%s,sendResult,lastCheckResult=%s\n",
                    new SimpleDateFormat("HH:mm:ss,SSS").format(new Date()),
                    msg.getMsgId(), msg.getTransactionId(),
                    msg.getUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES), s);
            statBenchmark.getUnexpectedCheckCount().incrementAndGet();
            return s;
        }
    }
    return msgMeta.checkResult.get(times - 1);
}