Search in sources :

Example 6 with ConfigFile

use of com.tencent.polaris.configuration.api.core.ConfigFile in project polaris-java by polarismesh.

the class ConfigFileManagerTest method testGetConfigFile.

@Test
public void testGetConfigFile() {
    ConfigFileMetadata configFileMetadata = ConfigFileTestUtils.assembleDefaultConfigFileMeta();
    ConfigFile mockedConfigFile = mock(ConfigFile.class);
    when(configFileFactoryManager.getFactory(any())).thenReturn(configFileFactory);
    when(configFileFactory.createConfigFile(configFileMetadata)).thenReturn(mockedConfigFile);
    // 第一次获取
    ConfigFile configFile = defaultConfigFileManager.getConfigFile(configFileMetadata);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigFile(configFileMetadata);
    Assert.assertEquals(mockedConfigFile, configFile);
    // 第二次获取,经过缓存
    ConfigFile configFile2 = defaultConfigFileManager.getConfigFile(configFileMetadata);
    verify(configFileFactoryManager).getFactory(configFileMetadata);
    verify(configFileFactory).createConfigFile(configFileMetadata);
    Assert.assertEquals(mockedConfigFile, configFile2);
}
Also used : ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) ConfigFileMetadata(com.tencent.polaris.configuration.api.core.ConfigFileMetadata) Test(org.junit.Test)

Example 7 with ConfigFile

use of com.tencent.polaris.configuration.api.core.ConfigFile in project polaris-java by polarismesh.

the class ConfigFileExample method main.

public static void main(String[] args) throws Exception {
    String namespace = "dev";
    String fileGroup = "myGroup";
    String fileName = "application.properties";
    // 创建配置中心服务类,一般情况下只需要单例对象
    ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService();
    // 获取配置文件
    ConfigFile configFile = configFileService.getConfigFile(namespace, fileGroup, fileName);
    // 打印配置文件内容
    Utils.print(configFile.getContent());
    // 添加变更监听器
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            System.out.printf("Received config file change event. old value = %s, new value = %s, change type = %s%n", event.getOldValue(), event.getNewValue(), event.getChangeType());
            // 获取配置文件最新内容
            Utils.print(configFile.getContent());
        }
    });
    // 更多 API 用法
    // User user = configFile.asJson(User.class, null);  自动反序列化配置文件成 JSON 对象
    // List<User> users = configFile.asJson(new TypeToken<List<User>>() {}.getType(), null)
    System.in.read();
}
Also used : ConfigFile(com.tencent.polaris.configuration.api.core.ConfigFile) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) ConfigFileService(com.tencent.polaris.configuration.api.core.ConfigFileService)

Aggregations

ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)7 Test (org.junit.Test)5 DefaultConfigFile (com.tencent.polaris.configuration.client.internal.DefaultConfigFile)3 TypeToken (com.google.gson.reflect.TypeToken)1 ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)1 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)1 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)1 ConfigFileService (com.tencent.polaris.configuration.api.core.ConfigFileService)1 ConfigFileFactory (com.tencent.polaris.configuration.client.factory.ConfigFileFactory)1