com.aliyun.openservices.ons.api.exactlyonce.manager.util.TxContextUtil Maven / Gradle / Ivy
package com.aliyun.openservices.ons.api.exactlyonce.manager.util;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.client.consumer.listener.ConsumeExactlyOnceStatus;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageConst;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageExt;
import com.aliyun.openservices.ons.api.exactlyonce.TxConstant;
import com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxContext;
import com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxContext.TxStage;
import com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxContext.TxStatus;
import com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxRecord;
/**
* @author gongshi
*/
public class TxContextUtil {
public static void updateTxContext(MQTxContext txContext) {
if (txContext == null) {
txContext = buildTxContext();
}
txContext.setInTxEnv(true);
txContext.setAutoCommit(true);
}
public static MQTxContext buildTxContext() {
MQTxContext txContext = new MQTxContext();
txContext.setInTxEnv(true);
txContext.setAutoCommit(true);
return txContext;
}
public static MQTxRecord buildTxRecord(MQTxContext context) {
MQTxRecord record = new MQTxRecord();
record.setTopicName(context.getTopicName());
record.setOffset(context.getOffset());
record.setMessageId(context.getMessageId());
record.setQid(context.getMessageQueue().getQueueId());
record.setBrokerName(context.getMessageQueue().getBrokerName());
record.setConsumerGroup(context.getConsumerGroup());
record.setCreateTime(System.currentTimeMillis());
return record;
}
public static boolean isTxContextCommitted(MQTxContext context) {
if (context == null || context.getStage() == null) {
return false;
}
return TxStage.COMMIT == context.getStage() && TxStatus.PROCESS_OK == context.getStatus();
}
public static boolean isTxContextRollbacked(MQTxContext context) {
if (context == null) {
return false;
}
return TxStage.ROLLBACK == context.getStage() && TxStatus.USER_PROCESS_FAIL == context.getStatus();
}
public static boolean isTxContextFinished(MQTxContext context) {
if (context == null) {
return false;
}
return (TxStage.COMMIT == context.getStage() && TxStatus.PROCESS_OK == context.getStatus()) ||
(TxStage.ROLLBACK == context.getStage() && TxStatus.USER_PROCESS_FAIL == context.getStatus());
}
public static boolean isTxProcessed(MQTxContext context) {
if (context == null) {
return false;
}
return context.getStage() != null && context.getStatus() != null;
}
public static String buildInternalMsgId(MessageExt msg, String consumerGroup) {
String uniqInfo = null;
if (msg.getProperty(MessageConst.PROPERTY_EXTEND_UNIQ_INFO) != null) {
uniqInfo = msg.getProperty(MessageConst.PROPERTY_EXTEND_UNIQ_INFO);
} else if (msg.getMsgId() != null) {
uniqInfo = msg.getMsgId();
}
return uniqInfo + TxConstant.INTERNAL_MSGID_SEPARATOR + consumerGroup;
}
public static ConsumeExactlyOnceStatus getExactlyOnceStatus(MQTxContext context) {
if (!isTxProcessed(context)) {
return ConsumeExactlyOnceStatus.NO_EXACTLYONCE;
}
if (context.isDup()) {
return ConsumeExactlyOnceStatus.EXACTLYONCE_DEDUP;
}
return ConsumeExactlyOnceStatus.EXACTLYONCE_PASS;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy