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

The following examples show how to use org.apache.rocketmq.client.producer.LocalTransactionState#ROLLBACK_MESSAGE . 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 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 2
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 3
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 4
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 5
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 6
Source File: TransactionListenerImpl.java    From rocketmq-4.3.0 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 7
Source File: TransactionProducer.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    statsBenchmarkTProducer.getCheckRequestSuccessCount().incrementAndGet();
    if (isCheckFalse) {

        return LocalTransactionState.ROLLBACK_MESSAGE;
    }

    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 8
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 9
Source File: TransactionProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    statsBenchmarkTProducer.getCheckRequestSuccessCount().incrementAndGet();
    if (ischeckffalse) {

        return LocalTransactionState.ROLLBACK_MESSAGE;
    }

    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 10
Source File: TransactionCheckListenerImpl.java    From rocketmq_trans_message with Apache License 2.0 5 votes vote down vote up
@Override
    public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
        try {
            String val = new String(msg.getBody(), "UTF-8");
            System.out.printf("server checking TrMsg " + val + "%n");
            if (Integer.parseInt(val) < 0) {
                return LocalTransactionState.UNKNOW;
            }
            if (statsMap.get(Integer.parseInt(val)) != null && statsMap.get(Integer.parseInt(val))) {
                return LocalTransactionState.COMMIT_MESSAGE;
            } else {
                System.out.printf("server checking TrMsg " + val + ",ROLLBACK");
                return LocalTransactionState.ROLLBACK_MESSAGE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
//        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.COMMIT_MESSAGE;
    }
 
Example 11
Source File: TransactionCheckListenerImpl.java    From rocketmq-all-4.1.0-incubating 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: 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 13
Source File: TransactionProducer.java    From rocketmq-read with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    statsBenchmarkTProducer.getCheckRequestSuccessCount().incrementAndGet();
    if (ischeckffalse) {

        return LocalTransactionState.ROLLBACK_MESSAGE;
    }

    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 14
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 15
Source File: ClearUserTransactionListener.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransaction(MessageExt msg) {
    String message = new String(msg.getBody());
    logger.info("收到检查清除用户本地事务状态消息 | {}", message);
    JSONObject messageJSON = JSON.parseObject(message);
    Long userId = messageJSON.getLong("userId");

    User user = userMapper.getById(userId);
    return user == null ? LocalTransactionState.COMMIT_MESSAGE : LocalTransactionState.ROLLBACK_MESSAGE;
}
 
Example 16
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 17
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 18
Source File: TransactionProducer.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    statsBenchmarkTProducer.getCheckRequestSuccessCount().incrementAndGet();
    if (ischeckffalse) {

        return LocalTransactionState.ROLLBACK_MESSAGE;
    }

    return LocalTransactionState.COMMIT_MESSAGE;
}
 
Example 19
Source File: TransactionProducer.java    From DDMQ with Apache License 2.0 5 votes vote down vote up
@Override
public LocalTransactionState checkLocalTransactionState(MessageExt msg) {
    statsBenchmarkTProducer.getCheckRequestSuccessCount().incrementAndGet();
    if (ischeckffalse) {

        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);
}