All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.ons.api.exactlyonce.aop.model.MQTxContext Maven / Gradle / Ivy

There is a newer version: 1.9.3.Final
Show newest version
package com.aliyun.openservices.ons.api.exactlyonce.aop.model;

import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageExt;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.common.message.MessageQueue;

import com.aliyun.openservices.ons.api.exactlyonce.datasource.DataSourceConfig;

/**
 * @author gongshi
 */
public class MQTxContext {
    private String messageId;
    private String topicName;
    private String consumerGroup;
    private boolean inTxEnv = false;
    private boolean inConsume = false;
    private boolean autoCommit = false;
    private int timeoutMs;
    private int retryTimes;
    private long startTimestamp;
    private long processTimestamp;
    private long persistenceTimestamp;
    private long aftePersistenceTimestamp;
    private long finishTimestamp;
    private MessageExt messageExt;
    private MessageQueue messageQueue;
    private Long offset;
    private TxStage stage;
    private TxStatus status;
    private DataSourceConfig dataSourceConfig;
    private boolean dup = false;

    public MQTxContext() {
        this.startTimestamp = System.currentTimeMillis();
    }

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public String getConsumerGroup() {
        return consumerGroup;
    }

    public void setConsumerGroup(String consumerGroup) {
        this.consumerGroup = consumerGroup;
    }

    public boolean isInTxEnv() {
        return inTxEnv;
    }

    public void setInTxEnv(boolean inTxEnv) {
        this.inTxEnv = inTxEnv;
    }

    public boolean isInConsume() {
        return inConsume;
    }

    public boolean isAutoCommit() {
        return autoCommit;
    }

    public void setAutoCommit(boolean autoCommit) {
        this.autoCommit = autoCommit;
    }

    public void setInConsume(boolean inConsume) {
        this.inConsume = inConsume;
    }

    public int getTimeoutMs() {
        return timeoutMs;
    }

    public void setTimeoutMs(int timeoutMs) {
        this.timeoutMs = timeoutMs;
    }

    public int getRetryTimes() {
        return retryTimes;
    }

    public void setRetryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
    }

    public long getStartTimestamp() {
        return startTimestamp;
    }

    public void setStartTimestamp(long startTimestamp) {
        this.startTimestamp = startTimestamp;
    }

    public long getProcessTimestamp() {
        return processTimestamp;
    }

    public void setProcessTimestamp(long processTimestamp) {
        this.processTimestamp = processTimestamp;
    }

    public long getPersistenceTimestamp() {
        return persistenceTimestamp;
    }

    public void setPersistenceTimestamp(long persistenceTimestamp) {
        this.persistenceTimestamp = persistenceTimestamp;
    }

    public long getAftePersistenceTimestamp() {
        return aftePersistenceTimestamp;
    }

    public void setAftePersistenceTimestamp(long aftePersistenceTimestamp) {
        this.aftePersistenceTimestamp = aftePersistenceTimestamp;
    }

    public long getFinishTimestamp() {
        return finishTimestamp;
    }

    public void setFinishTimestamp(long finishTimestamp) {
        this.finishTimestamp = finishTimestamp;
    }

    public MessageExt getMessageExt() {
        return messageExt;
    }

    public void setMessageExt(MessageExt messageExt) {
        this.messageExt = messageExt;
    }

    public MessageQueue getMessageQueue() {
        return messageQueue;
    }

    public void setMessageQueue(MessageQueue messageQueue) {
        this.messageQueue = messageQueue;
    }

    public Long getOffset() {
        return offset;
    }

    public void setOffset(Long offset) {
        this.offset = offset;
    }


    public TxStage getStage() {
        return stage;
    }

    public void setStage(TxStage stage) {
        this.stage = stage;
    }

    public TxStatus getStatus() {
        return status;
    }

    public void setStatus(TxStatus status) {
        this.status = status;
    }

    public DataSourceConfig getDataSourceConfig() {
        return dataSourceConfig;
    }

    public void setDataSourceConfig(DataSourceConfig dataSourceConfig) {
        this.dataSourceConfig = dataSourceConfig;
    }

    public boolean isDup() {
        return dup;
    }

    public void setDup(boolean dup) {
        this.dup = dup;
    }

    public static MQTxContext parseParam(MessageExt msgExt, String topicName, String consumerGroup) {
        MQTxContext context = new MQTxContext();
        if (msgExt == null) {
            return context;
        }
        context.setMessageId(msgExt.getMsgId());
        context.setConsumerGroup(consumerGroup);
        context.setTopicName(topicName);
        context.setOffset(msgExt.getQueueOffset());
        return context;
    }

    @Override
    public String toString() {
        return "MQTxContext{" +
                "messageId='" + messageId + '\'' +
                ", topicName='" + topicName + '\'' +
                ", consumerGroup='" + consumerGroup + '\'' +
                ", inTxEnv=" + inTxEnv +
                ", inConsume=" + inConsume +
                ", autoCommit=" + autoCommit +
                ", timeoutMs=" + timeoutMs +
                ", retryTimes=" + retryTimes +
                ", startTimestamp=" + startTimestamp +
                ", messageExt=" + messageExt +
                ", messageQueue=" + messageQueue +
                ", offset=" + offset +
                ", stage=" + stage +
                ", status=" + status +
                '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof MQTxContext)) {
            return false;
        }
        MQTxContext txContext = (MQTxContext) obj;
        boolean equal = false;
        equal &= txContext.isInTxEnv() == this.inTxEnv;
        if (!equal) {
            return false;
        }
        equal &= (txContext.getMessageId() == null && this.messageId == null) ||
                (txContext.getMessageId() != null && this.messageId != null && this.messageId.equals(txContext.getMessageId()));
        if (!equal) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = messageId != null ? messageId.hashCode() : 0;
        result = 31 * result + (topicName != null ? topicName.hashCode() : 0);
        result = 31 * result + (consumerGroup != null ? consumerGroup.hashCode() : 0);
        result = 31 * result + (inTxEnv ? 1 : 0);
        result = 31 * result + (inConsume ? 1 : 0);
        result = 31 * result + (autoCommit ? 1 : 0);
        result = 31 * result + timeoutMs;
        result = 31 * result + retryTimes;
        result = 31 * result + (int)(startTimestamp ^ (startTimestamp >>> 32));
        result = 31 * result + (int)(processTimestamp ^ (processTimestamp >>> 32));
        result = 31 * result + (int)(persistenceTimestamp ^ (persistenceTimestamp >>> 32));
        result = 31 * result + (int)(aftePersistenceTimestamp ^ (aftePersistenceTimestamp >>> 32));
        result = 31 * result + (int)(finishTimestamp ^ (finishTimestamp >>> 32));
        result = 31 * result + (messageExt != null ? messageExt.hashCode() : 0);
        result = 31 * result + (messageQueue != null ? messageQueue.hashCode() : 0);
        result = 31 * result + (offset != null ? offset.hashCode() : 0);
        result = 31 * result + (stage != null ? stage.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        result = 31 * result + (dataSourceConfig != null ? dataSourceConfig.hashCode() : 0);
        return result;
    }

    /**
     * stage: consume message -> process message -> flush result to DB
     */
    public enum TxStage {
        /**
         * before local transaction
         */
        BEFORE_TX,

        /**
         * received message and before processed result flush to db
         */
        BEFORE_DB_COMMIT,

        /**
         * processed result committed to db
         */
        COMMIT,

        /**
         * consume message rollbacked
         */
        ROLLBACK,

        /**
         * local transaction finished
         */
        AFTER_TX
    }

    public enum TxStatus {
        /**
         * user process message fail
         */
        USER_PROCESS_FAIL,

        /**
         * user processed result flush to db fail
         */
        USER_DATA_FLUSH_DB_FAIL,

        /**
         * transaction record conflicted
         */
        TXRECORD_CONFLICT,

        /**
         * consume success
         */
        PROCESS_OK
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy