Search in sources :

Example 1 with FileWatcher

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));
}
Also used : OriginTrackedMapPropertySource(org.springframework.boot.env.OriginTrackedMapPropertySource) FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) IOException(java.io.IOException) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 2 with FileWatcher

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();
            }
        });
    }
}
Also used : FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) File(java.io.File)

Example 3 with FileWatcher

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());
}
Also used : FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) File(java.io.File) Test(org.junit.Test)

Example 4 with FileWatcher

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);
    }
}
Also used : FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 5 with FileWatcher

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);
    }
}
Also used : FileWatcher(com.alibaba.nacos.sys.file.FileWatcher) FileChangeEvent(com.alibaba.nacos.sys.file.FileChangeEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

FileChangeEvent (com.alibaba.nacos.sys.file.FileChangeEvent)5 FileWatcher (com.alibaba.nacos.sys.file.FileWatcher)5 NacosException (com.alibaba.nacos.api.exception.NacosException)3 File (java.io.File)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 IOException (java.io.IOException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 OriginTrackedMapPropertySource (org.springframework.boot.env.OriginTrackedMapPropertySource)1