服务器使用json格式配置文件

来源:互联网 发布:淘宝有情侣网店吗 编辑:程序博客网 时间:2024/05/22 18:02

1. 编辑POM.XML:

    <build>        <plugins>            <plugin>                <groupId>org.jsonschema2pojo</groupId>                <artifactId>jsonschema2pojo-maven-plugin</artifactId>                <version>0.4.0</version>                <configuration>                    <sourceDirectory>src/main/resources/schema/settings.json</sourceDirectory>                    <targetPackage>com.test.maverproject.generated.json</targetPackage>                    <sourceType>json</sourceType>                    <usePrimitives>true</usePrimitives>                    <outputDirectory>src/main/java</outputDirectory>                    <useJodaDates>true</useJodaDates>                </configuration>                <executions>                    <execution>                        <goals>                            <goal>generate</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>            </build>

2.编辑src/main/resources/schema/settings.json,这里只需要编辑json的格式,具体的带数据的json从数据库中取得。

{"config":{ "a":5, "b":true, "c":"STRING", "d":[{"e":1}]}}

3.清理并构建项目,此时在com.test.maverproject.generated.json包下会有3个类Settings,Config,D。每一个JsonObject都会对应生成一个类


4.从服务器读配置json,没有必要每次都从数据库取,做一个缓存

接口

import com.test.mavenproject.generated.json.Settings;import org.jvnet.hk2.annotations.Contract;@Contractpublic interface SettingsProvider {    //服务器需要的完整的配置    Settings getSettings(int clientVersionCode);}


实现

import com.fasterxml.jackson.databind.ObjectMapper;import java.util.concurrent.ExecutionException;import java.util.concurrent.TimeUnit;import javax.inject.Inject;import jersey.repackaged.com.google.common.cache.CacheBuilder;import jersey.repackaged.com.google.common.cache.CacheLoader;import jersey.repackaged.com.google.common.cache.LoadingCache;import org.jvnet.hk2.annotations.Service;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@Servicepublic class CachedDbSettingsProvider implements SettingsProvider {    private final static Logger LOG = LoggerFactory.getLogger(CachedDbSettingsProvider.class);    private final LoadingCache<Integer, Settings> settingsCache;    private final Settings dummySettings;    @Inject    public CachedDbSettingsProvider(            final ObjectMapper objectMapper    ) {        dummySettings = new Settings();        //如果需要区别不同版本        dummySettings.setAdditionalProperty("version", 0);        this.settingsCache = CacheBuilder.newBuilder()                .expireAfterWrite(10, TimeUnit.MINUTES)                .build(new CacheLoader<Integer, Settings>() {                    @Override                    public Settings load(Integer key) throws Exception {                        LOG.debug("Repopulating cache for version {}", key);                        //TODO.从服务器取jsonString                        //如果需要区别不同版本可以用key作为区别去取不同的jsonString                        if (取不到)                            return dummySettings;                        Settings ret = objectMapper.readValue(jsonString, Settings.class);                        ret.setAdditionalProperty("version", 版本号(有需求的话));                        return ret;                    }                });    }    @Override    public Settings getSettings(int version) {        try {            Settings settings = settingsCache.get(version);            if ((int)settings.getAdditionalProperties().get("version") == 0) {                LOG.debug("Failed to get settings for version {}, return default.", version);            }            return settings;        } catch (ExecutionException e) {            LOG.error("Failed to read Settings!", e);        }        return null;    }}



原创粉丝点击