【Vesta發(fā)號(hào)器源碼】AbstractIdServiceImpl

Vesta發(fā)號(hào)器源碼解析——AbstractIdServiceImpl

這個(gè)類是Id生成的實(shí)現(xiàn)類的抽象類,主要的入口就在AbstractIdServiceImpl的系列類里面

字段

    //日志記錄
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    protected long machineId = -1;
    protected long genMethod = 0;
    protected long version = 0;
    
    //Id類型
    protected IdType idType;
    //Id元數(shù)據(jù)
    protected IdMeta idMeta;
    //Id轉(zhuǎn)換器
    protected IdConverter idConverter;
    //MachineId存儲(chǔ)
    protected MachineIdProvider machineIdProvider;
    //timer字段
    protected Timer timer;

構(gòu)造方法

    //默認(rèn)構(gòu)造方法
   public AbstractIdServiceImpl() {
        idType = IdType.SECONDS;
    }
    //按照給定的名稱初始化
    public AbstractIdServiceImpl(String type) {
        idType = IdType.parse(type);
    }
    //按照給定的類型值初始化
    public AbstractIdServiceImpl(long type) {
        idType = IdType.parse(type);
    }
    //按照給定的枚舉類初始化
    public AbstractIdServiceImpl(IdType type) {
        idType = type;
    }

初始化方法

    public void init() {
        //如果id元數(shù)據(jù)沒有初始化,利用工廠初始化id元數(shù)據(jù)
        if (this.idMeta == null) {
            setIdMeta(IdMetaFactory.getIdMeta(idType));
        }
        //Id轉(zhuǎn)換器如果沒有初始化按照默認(rèn)的初始化
        if (this.idConverter == null) {
            setIdConverter(new IdConverterImpl());
        }
        //timer如果沒有初始化,按默認(rèn)的實(shí)例化
        if (this.timer == null) {
            setTimer(new SimpleTimer());
        }
        //初始化,元數(shù)據(jù)和類型
        this.timer.init(idMeta, idType);
        //初始化machineID
        this.machineId = machineIdProvider.getMachineId();
        //校驗(yàn)machineid
        validateMachineId(this.machineId);
    }

校驗(yàn)MachineId

方法主要用于校驗(yàn)MachineId

    public void validateMachineId(long machineId){
        //MachineId小于0,說明沒有配置,停止啟動(dòng)拋出異常記錄日志
        if (machineId < 0) {
            log.error("The machine ID is not configured properly (" + machineId + " < 0) so that Vesta Service refuses to start.");

            throw new IllegalStateException(
                    "The machine ID is not configured properly (" + machineId + " < 0) so that Vesta Service refuses to start.");
        //machineId超過了最大的MachineId的標(biāo)示范圍,停止啟動(dòng),拋出異常
        } else if (machineId >= (1 << this.idMeta.getMachineBits())) {
            log.error("The machine ID is not configured properly ("
                    + machineId + " >= " + (1 << this.idMeta.getMachineBits()) + ") so that Vesta Service refuses to start.");

            throw new IllegalStateException("The machine ID is not configured properly ("
                    + machineId + " >= " + (1 << this.idMeta.getMachineBits()) + ") so that Vesta Service refuses to start.");

        }
    }

核心方法 生成id

    public long genId() {
        Id id = new Id();
        //對(duì)ID進(jìn)行基本信息配置
        id.setMachine(machineId);
        id.setGenMethod(genMethod);
        id.setType(idType.value());
        id.setVersion(version);
        //生成變化字段,一個(gè)是時(shí)間一個(gè)是序列
        populateId(id);
        //將Id對(duì)象轉(zhuǎn)換成long型的
        long ret = idConverter.convert(id, this.idMeta);

        // 記錄日志
        if (log.isTraceEnabled())
            log.trace(String.format("Id: %s => %d", id, ret));

        return ret;
    }

其他方法

具體作用詳見各方法注釋

    //Id主要變化生成方法,實(shí)現(xiàn)類中實(shí)現(xiàn)
    protected abstract void populateId(Id id);
    //轉(zhuǎn)換時(shí)間,將long時(shí)間轉(zhuǎn)換為date
    public Date transTime(final long time) {
        return timer.transTime(time);
    }

    //解析Id,本質(zhì)上是調(diào)用轉(zhuǎn)換方法
    //將long轉(zhuǎn)換為Id對(duì)象
    public Id expId(long id) {
        return idConverter.convert(id, this.idMeta);
    }
    
    //構(gòu)建一個(gè)指定的ID,主要參數(shù)是時(shí)間和序列號(hào)
    public long makeId(long time, long seq) {
        return makeId(time, seq, machineId);
    }

    //構(gòu)建一個(gè)指定的ID,主要參數(shù)是時(shí)間和序列號(hào)以及machineid
    public long makeId(long time, long seq, long machine) {
        return makeId(genMethod, time, seq, machine);
    }
    
    //構(gòu)建一個(gè)指定的ID,主要參數(shù)是生成方法、時(shí)間和序列號(hào)以及machineid
    public long makeId(long genMethod, long time, long seq, long machine) {
        return makeId(idType.value(), genMethod, time, seq, machine);
    }
    
    //構(gòu)建一個(gè)指定的ID,主要參數(shù)是類型、生成方法、時(shí)間和序列號(hào)以及machineid
    public long makeId(long type, long genMethod, long time,
                       long seq, long machine) {
        return makeId(version, type, genMethod, time, seq, machine);
    }

    //構(gòu)建一個(gè)指定的ID,主要參數(shù)是版本、類型、生成方法、時(shí)間和序列號(hào)以及machineid
    public long makeId(long version, long type, long genMethod,
                       long time, long seq, long machine) {
        Id id = new Id(machine, seq, time, genMethod, type, version);
        return idConverter.convert(id, this.idMeta);
    }
    
    //下面是相關(guān)字段的get和set方法
    public void setMachineId(long machineId) {
        this.machineId = machineId;
    }

    public void setGenMethod(long genMethod) {
        this.genMethod = genMethod;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public void setIdConverter(IdConverter idConverter) {
        this.idConverter = idConverter;
    }

    public void setIdMeta(IdMeta idMeta) {
        this.idMeta = idMeta;
    }

    public void setMachineIdProvider(MachineIdProvider machineIdProvider) {
        this.machineIdProvider = machineIdProvider;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、編程規(guī)約 (一)命名規(guī)約 【強(qiáng)制】 代碼中的命名均不能以下劃線或美元符號(hào)開始,也不能以下劃線或美元符號(hào)結(jié)束。反...
    喝咖啡的螞蟻閱讀 1,615評(píng)論 0 2
  • 阿里巴巴 JAVA 開發(fā)手冊 1 / 32 Java 開發(fā)手冊 版本號(hào) 制定團(tuán)隊(duì) 更新日期 備 注 1.0.0 阿...
    糖寶_閱讀 7,894評(píng)論 0 5
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,699評(píng)論 1 32
  • 感恩鐘聲響起,讓我在睡夢中醒來,開始一天的修行。為了晚上能睡個(gè)好覺,我不設(shè)定鬧鐘,我的生物鐘發(fā)揮作用,這樣的正念讓...
    讀書不負(fù)我魏霞閱讀 279評(píng)論 0 4
  • 有人說愛情如裸陳于空氣中的香水,隨時(shí)間的推移,味道逐漸寡淡,直至消失;有人說愛情如陳釀,隨歲月的增長,味道越來越醇...
    大_可閱讀 649評(píng)論 0 6

友情鏈接更多精彩內(nèi)容