美文网首页
【Vesta发号器源码】IdServiceImpl

【Vesta发号器源码】IdServiceImpl

作者: DeanChangDM | 来源:发表于2019-05-14 09:59 被阅读0次

    Vesta发号器源码解析——IdServiceImpl

    这个类是继承了AbstractIdServiceImpl的一个具体的实现

    字段属性及构造方法

        //参数名称,用于在启动的时候传入标示使用synchronized关键字与否
        private static final String SYNC_LOCK_IMPL_KEY = "vesta.sync.lock.impl.key";
        //参数名称,用于在启动的时候传入标示使用Atomic与否
        private static final String ATOMIC_IMPL_KEY = "vesta.atomic.impl.key";
        //存储id生成器
        protected IdPopulator idPopulator;
        //构造方法,无参
        public IdServiceImpl() {
            super();
        }
        //构造方法,传入参数id类型名
        public IdServiceImpl(String type) {
            super(type);
        }
        //构造方法,传入参数id类型值
        public IdServiceImpl(long type) {
            super(type);
        }
        //构造方法,传入参数id类型枚举
        public IdServiceImpl(IdType type) {
            super(type);
        }
    

    初始化方法

        @Override
        public void init() {
            //先初始化父类
            super.init();
            //然后初始化id生成器
            initPopulator();
        }
    

    id生成器初始化

        
        public void initPopulator() {
            //如果不为空说明已经设置过了,跳过
            if (idPopulator != null){
                log.info("The " + idPopulator.getClass().getCanonicalName() + " is used.");
            } else if (CommonUtils.isPropKeyOn(SYNC_LOCK_IMPL_KEY)) {
                log.info("The SyncIdPopulator is used.");
                //启动时参数标识了使用synchronized关键字
                idPopulator = new SyncIdPopulator();
            } else if (CommonUtils.isPropKeyOn(ATOMIC_IMPL_KEY)) {
                //标识了使用atomic
                log.info("The AtomicIdPopulator is used.");
                idPopulator = new AtomicIdPopulator();
            } else {
                //默认采用Lock来实现
                log.info("The default LockIdPopulator is used.");
                idPopulator = new LockIdPopulator();
            }
        }
    

    id生成器调用和id生成器设置

        //调用id生成器构成id
        protected void populateId(Id id) {
            idPopulator.populateId(timer, id, idMeta);
        }
        //设置id生成器
        public void setIdPopulator(IdPopulator idPopulator) {
            this.idPopulator = idPopulator;
        }
    

    相关文章

      网友评论

          本文标题:【Vesta发号器源码】IdServiceImpl

          本文链接:https://www.haomeiwen.com/subject/onblaqtx.html