Search in sources :

Example 6 with ConfigFileChangeEvent

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

the class ConfigFileTest method testDeleteContent.

@Test
public void testDeleteContent() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    Assert.assertEquals(content, configFile.getContent());
    Assert.assertTrue(configFile.hasContent());
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue() == null && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.DELETED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), null);
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 7 with ConfigFileChangeEvent

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

the class ConfigFileTest method testModifiedContent.

@Test
public void testModifiedContent() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    Assert.assertEquals(content, configFile.getContent());
    Assert.assertTrue(configFile.hasContent());
    String newContent = "hello2";
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue().equals(newContent) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.MODIFIED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), "hello2");
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 8 with ConfigFileChangeEvent

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

the class ConfigFileTest method testContentNotChanged.

@Test
public void testContentNotChanged() throws InterruptedException {
    String content = "hello";
    when(configFileRepo.getContent()).thenReturn(content);
    DefaultConfigFile configFile = new DefaultConfigFile(ConfigFileTestUtils.testNamespace, ConfigFileTestUtils.testGroup, ConfigFileTestUtils.testFileName, configFileRepo, configFileConfig);
    AtomicBoolean check = new AtomicBoolean(false);
    configFile.addChangeListener(new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent event) {
            check.set(event.getNewValue().equals(content) && event.getOldValue().equals(content) && event.getChangeType() == ChangeType.NOT_CHANGED);
        }
    });
    configFile.onChange(ConfigFileTestUtils.assembleDefaultConfigFileMeta(), content);
    TimeUnit.MILLISECONDS.sleep(100);
    Assert.assertTrue(check.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigFileChangeEvent(com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent) ConfigFileChangeListener(com.tencent.polaris.configuration.api.core.ConfigFileChangeListener) DefaultConfigFile(com.tencent.polaris.configuration.client.internal.DefaultConfigFile) Test(org.junit.Test)

Example 9 with ConfigFileChangeEvent

use of com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent 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

ConfigFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigFileChangeEvent)9 ConfigFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigFileChangeListener)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Test (org.junit.Test)7 DefaultConfigFile (com.tencent.polaris.configuration.client.internal.DefaultConfigFile)6 TypeToken (com.google.gson.reflect.TypeToken)1 ChangeType (com.tencent.polaris.configuration.api.core.ChangeType)1 ConfigFile (com.tencent.polaris.configuration.api.core.ConfigFile)1 ConfigFileService (com.tencent.polaris.configuration.api.core.ConfigFileService)1 ConfigKVFileChangeEvent (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent)1 ConfigKVFileChangeListener (com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener)1 ConfigPropertyChangeInfo (com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo)1 ConfigPropertiesFile (com.tencent.polaris.configuration.client.internal.ConfigPropertiesFile)1 Properties (java.util.Properties)1