use of com.alibaba.nacos.sys.file.FileWatcher in project nacos by alibaba.
the class NacosAutoRefreshPropertySourceLoader method load.
@Override
public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
holder = resource;
Map<String, ?> tmp = loadProperties(resource);
properties.putAll(tmp);
try {
WatchFileCenter.registerWatcher(EnvUtil.getConfPath(), new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
try {
Map<String, ?> tmp1 = loadProperties(holder);
properties.putAll(tmp1);
} catch (IOException ignore) {
}
}
@Override
public boolean interest(String context) {
return StringUtils.contains(context, "application.properties");
}
});
} catch (NacosException ignore) {
}
if (properties.isEmpty()) {
return Collections.emptyList();
}
return Collections.singletonList(new OriginTrackedMapPropertySource("nacos_application_conf", properties));
}
use of com.alibaba.nacos.sys.file.FileWatcher in project nacos by alibaba.
the class WatchFileCenter_ITCase method func.
private void func(final String fileName, final File file, final Consumer<String> consumer) throws Exception {
CountDownLatch latch = new CountDownLatch(100);
DiskUtils.touch(file);
WatchFileCenter.registerWatcher(path, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
final File file = Paths.get(path, fileName).toFile();
final String content = DiskUtils.readFile(file);
consumer.accept(content);
}
@Override
public boolean interest(String context) {
return StringUtils.contains(context, fileName);
}
});
final AtomicInteger id = new AtomicInteger(0);
final AtomicReference<String> finalContent = new AtomicReference<>(null);
for (int i = 0; i < 100; i++) {
executor.execute(() -> {
final String j = fileName + "_" + id.incrementAndGet();
try {
final File file1 = Paths.get(path, fileName).toFile();
synchronized (monitor) {
finalContent.set(j);
DiskUtils.writeFile(file1, j.getBytes(StandardCharsets.UTF_8), false);
}
} finally {
latch.countDown();
}
});
}
}
use of com.alibaba.nacos.sys.file.FileWatcher in project nacos by alibaba.
the class WatchFileCenter_ITCase method test_modify_file_much.
@Test
public void test_modify_file_much() throws Exception {
final String fileName = "modify_file_much";
final File file = Paths.get(path, fileName).toFile();
CountDownLatch latch = new CountDownLatch(3);
AtomicInteger count = new AtomicInteger(0);
WatchFileCenter.registerWatcher(path, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
try {
System.out.println(event);
System.out.println(DiskUtils.readFile(file));
count.incrementAndGet();
} finally {
latch.countDown();
}
}
@Override
public boolean interest(String context) {
return StringUtils.contains(context, fileName);
}
});
for (int i = 0; i < 3; i++) {
DiskUtils.writeFile(file, ByteUtils.toBytes(("test_modify_file_" + i)), false);
ThreadUtils.sleep(10_000L);
}
latch.await(10_000L, TimeUnit.MILLISECONDS);
Assert.assertEquals(3, count.get());
}
use of com.alibaba.nacos.sys.file.FileWatcher in project nacos by alibaba.
the class ConnectionManager method registerFileWatch.
private void registerFileWatch() {
try {
String tpsPath = Paths.get(EnvUtil.getNacosHome(), "data", "loader").toString();
WatchFileCenter.registerWatcher(tpsPath, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
try {
String fileName = event.getContext().toString();
if (RULE_FILE_NAME.equals(fileName)) {
loadRuleFromLocal();
}
} catch (Throwable throwable) {
Loggers.REMOTE.warn("Fail to load rule from local", throwable);
}
}
@Override
public boolean interest(String context) {
return RULE_FILE_NAME.equals(context);
}
});
} catch (NacosException e) {
Loggers.REMOTE.warn("Register connection rule fail ", e);
}
}
use of com.alibaba.nacos.sys.file.FileWatcher in project nacos by alibaba.
the class TpsMonitorManager method registerFileWatch.
private void registerFileWatch() {
try {
String tpsPath = Paths.get(EnvUtil.getNacosHome(), "data" + File.separator + "tps" + File.separator).toString();
checkBaseDir();
WatchFileCenter.registerWatcher(tpsPath, new FileWatcher() {
@Override
public void onChange(FileChangeEvent event) {
String fileName = event.getContext().toString();
try {
if (points.get(fileName) != null) {
loadRuleFromLocal(points.get(fileName));
}
} catch (Throwable throwable) {
Loggers.TPS_CONTROL.warn("Fail to load rule from local,pointName={},error={}", fileName, throwable);
}
}
@Override
public boolean interest(String context) {
for (String pointName : points.keySet()) {
if (context.equals(pointName)) {
return true;
}
}
return false;
}
});
} catch (NacosException e) {
Loggers.TPS_CONTROL.warn("Register fire watch fail.", e);
}
}
Aggregations