美文网首页
common模块阅读7:ConfigManager

common模块阅读7:ConfigManager

作者: 赤子心_d709 | 来源:发表于2017-09-11 14:10 被阅读79次

说明

该类是配置管理器的父类

提供配置文件,加载配置,持久化配置
提供编码,解码方法

继承关系如下


子类都属于broker模块和store模块

方法

配置相关

configFilePath:用于返回配置文件路径
load:用于加载配置文件
loadBak:用于在配置文件加载失败时,加载${configFile}.bak文件
persist:将编码的内容写入配置文件,用于持久化

源码如下

    //配置文件路径
    public abstract String configFilePath();

    //加载配置文件
    public boolean load() {
        String fileName = null;
        try {
            fileName = this.configFilePath();
            String jsonString = MixAll.file2String(fileName);

            if (null == jsonString || jsonString.length() == 0) {
                return this.loadBak();
            } else {
                this.decode(jsonString);
                log.info("load {} OK", fileName);
                return true;
            }
        } catch (Exception e) {
            log.error("load [{}] failed, and try to load backup file", fileName, e);
            return this.loadBak();
        }
    }

    //加载${configFile}.bak文件
    private boolean loadBak() {
        String fileName = null;
        try {
            fileName = this.configFilePath();
            String jsonString = MixAll.file2String(fileName + ".bak");
            if (jsonString != null && jsonString.length() > 0) {
                this.decode(jsonString);
                log.info("load [{}] OK", fileName);
                return true;
            }
        } catch (Exception e) {
            log.error("load [{}] Failed", fileName, e);
            return false;
        }

        return true;
    }

    //将当前编码的内容,持久化到configFile文件
    public synchronized void persist() {
        String jsonString = this.encode(true);
        if (jsonString != null) {
            String fileName = this.configFilePath();
            try {
                MixAll.string2File(jsonString, fileName);//写入到文件
            } catch (IOException e) {
                log.error("persist file [{}] exception", fileName, e);
            }
        }
    }

编码解码相关

    //编码
    public abstract String encode();

    //编码
    public abstract String encode(final boolean prettyFormat);

    //根据jsonString完成解码
    public abstract void decode(final String jsonString);

思考

decode返回是void型,而不是Object

具体的编码,解码工作由实现类自己完成
比如TopicConfigManager#decode解码 是

1.根据json串得到TopicConfigSerializeWrapper对象
2.如果wrapper非空,则获取topicConfigTable以及dataVersion,来更新自己的属性

相关文章

网友评论

      本文标题:common模块阅读7:ConfigManager

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