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());
}
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());
}
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());
}
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();
}
Aggregations