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

im.shs.tick.sequence.builder.DbSeqBuilder Maven / Gradle / Ivy

package im.shs.tick.sequence.builder;

import im.shs.tick.sequence.range.BizName;
import im.shs.tick.sequence.range.impl.db.DbSeqRangeMgr;
import im.shs.tick.sequence.seq.Sequence;
import im.shs.tick.sequence.seq.format.GenerateNoFormat;
import im.shs.tick.sequence.seq.impl.DefaultRangeSequence;

import javax.sql.DataSource;

/**
 * 基于DB取步长,序列号生成器构建者
 */
public class DbSeqBuilder implements SeqBuilder {

    /**
     * 数据库数据源[必选]
     */
    private DataSource dataSource;

    /**
     * 业务名称[必选]
     */
    private BizName bizName;
    /**
     * 存放序列号步长的表[可选:默认:sequence]
     */
    private String tableName = "sequence";
    /**
     * 并发是数据使用了乐观策略,这个是失败重试的次数[可选:默认:100]
     */
    private int retryTimes = 100;
    /**
     * 获取range步长[可选:默认:1000]
     */
    private int step = 1000;

    /**
     * 序列号分配起始值[可选:默认:0]
     */
    private long stepStart = 0;

    /**
     * 格式
     */
    private GenerateNoFormat format = GenerateNoFormat.DATE;

    /**
     * 前缀
     */
    private String bizPrefix = "";

    /**
     * 是否添加环境参数
     */
    private boolean addEnv;

    /**
     * 添加随机数位数
     */
    private int addRandom;

    public static DbSeqBuilder create() {
        DbSeqBuilder builder = new DbSeqBuilder();
        return builder;
    }

    @Override
    public Sequence build() {
        //利用DB获取区间管理器
        DbSeqRangeMgr dbSeqRangeMgr = new DbSeqRangeMgr();
        dbSeqRangeMgr.setDataSource(this.dataSource);
        dbSeqRangeMgr.setTableName(this.tableName);
        dbSeqRangeMgr.setRetryTimes(this.retryTimes);
        dbSeqRangeMgr.setStep(this.step);
        dbSeqRangeMgr.setStepStart(stepStart);
        dbSeqRangeMgr.init();
        //构建序列号生成器
        DefaultRangeSequence sequence = new DefaultRangeSequence();
        sequence.setName(this.bizName);
        sequence.setFormat(this.format);
        sequence.setBizPrefix(this.bizPrefix);
        sequence.setAddEnv(this.addEnv);
        sequence.setAddRandom(this.addRandom);
        sequence.setSeqRangeMgr(dbSeqRangeMgr);
        return sequence;
    }

    public DbSeqBuilder dataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    public DbSeqBuilder tableName(String tableName) {
        this.tableName = tableName;
        return this;
    }

    public DbSeqBuilder retryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
        return this;
    }

    public DbSeqBuilder step(int step) {
        this.step = step;
        return this;
    }

    public DbSeqBuilder bizName(BizName bizName) {
        this.bizName = bizName;
        return this;
    }

    public DbSeqBuilder stepStart(long stepStart) {
        this.stepStart = stepStart;
        return this;
    }

    public DbSeqBuilder format(GenerateNoFormat format) {
        this.format = format;
        return this;
    }

    public DbSeqBuilder bizPrefix(String bizPrefix) {
        this.bizPrefix = bizPrefix;
        return this;
    }

    public DbSeqBuilder addEnv(boolean addEnv) {
        this.addEnv = addEnv;
        return this;
    }

    public DbSeqBuilder addRandom(int addRandom) {
        this.addRandom = addRandom;
        return this;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy