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

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;

@Contract
public 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;

@Service
public 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;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值