Search in sources :

Example 1 with SimpleWatcher

use of cn.hutool.core.io.watch.SimpleWatcher in project hutool by looly.

the class BasicSetting method autoLoad.

/**
 * 在配置文件变更时自动加载
 *
 * @param autoReload 是否自动加载
 */
public void autoLoad(boolean autoReload) {
    if (autoReload) {
        if (null != this.watchMonitor) {
            this.watchMonitor.close();
        }
        try {
            watchMonitor = WatchMonitor.create(this.settingUrl, StandardWatchEventKinds.ENTRY_MODIFY);
            watchMonitor.setWatcher(new SimpleWatcher() {

                @Override
                public void onModify(WatchEvent<?> event, Path currentPath) {
                    load();
                }
            }).start();
        } catch (Exception e) {
            throw new SettingRuntimeException(e, "Setting auto load not support url: [{}]", this.settingUrl);
        }
        StaticLog.debug("Auto load for [{}] listenning...", this.settingUrl);
    } else {
        IoUtil.close(this.watchMonitor);
        this.watchMonitor = null;
    }
}
Also used : Path(java.nio.file.Path) WatchEvent(java.nio.file.WatchEvent) SimpleWatcher(cn.hutool.core.io.watch.SimpleWatcher) SettingRuntimeException(cn.hutool.setting.SettingRuntimeException) SettingRuntimeException(cn.hutool.setting.SettingRuntimeException)

Example 2 with SimpleWatcher

use of cn.hutool.core.io.watch.SimpleWatcher in project hutool by looly.

the class WatchMonitorTest method main.

public static void main(String[] args) {
    Watcher watcher = new SimpleWatcher() {

        @Override
        public void onCreate(WatchEvent<?> event, Path currentPath) {
            Object obj = event.context();
            Console.log("创建:{}-> {}", currentPath, obj);
        }

        @Override
        public void onModify(WatchEvent<?> event, Path currentPath) {
            Object obj = event.context();
            Console.log("修改:{}-> {}", currentPath, obj);
        }

        @Override
        public void onDelete(WatchEvent<?> event, Path currentPath) {
            Object obj = event.context();
            Console.log("删除:{}-> {}", currentPath, obj);
        }

        @Override
        public void onOverflow(WatchEvent<?> event, Path currentPath) {
            Object obj = event.context();
            Console.log("Overflow:{}-> {}", currentPath, obj);
        }
    };
    WatchMonitor monitor = WatchMonitor.createAll("d:/test/aaa.txt", new DelayWatcher(watcher, 500));
    monitor.setMaxDepth(0);
    monitor.start();
}
Also used : Path(java.nio.file.Path) WatchMonitor(cn.hutool.core.io.watch.WatchMonitor) SimpleWatcher(cn.hutool.core.io.watch.SimpleWatcher) Watcher(cn.hutool.core.io.watch.Watcher) DelayWatcher(cn.hutool.core.io.watch.watchers.DelayWatcher) WatchEvent(java.nio.file.WatchEvent) SimpleWatcher(cn.hutool.core.io.watch.SimpleWatcher) DelayWatcher(cn.hutool.core.io.watch.watchers.DelayWatcher)

Example 3 with SimpleWatcher

use of cn.hutool.core.io.watch.SimpleWatcher in project hutool by looly.

the class Props method autoLoad.

/**
 * 在配置文件变更时自动加载
 *
 * @param autoReload 是否自动加载
 */
public void autoLoad(boolean autoReload) {
    if (autoReload) {
        if (null != this.watchMonitor) {
            this.watchMonitor.close();
            try {
                watchMonitor = WatchMonitor.create(Paths.get(this.propertiesFileUrl.toURI()));
                watchMonitor.setWatcher(new SimpleWatcher() {

                    @Override
                    public void onModify(WatchEvent<?> event, Path currentPath) {
                        load();
                    }
                }).start();
            } catch (Exception e) {
                throw new SettingRuntimeException(e, "Setting auto load not support url: [{}]", this.propertiesFileUrl);
            }
        }
    } else {
        IoUtil.close(this.watchMonitor);
        this.watchMonitor = null;
    }
}
Also used : Path(java.nio.file.Path) WatchEvent(java.nio.file.WatchEvent) SimpleWatcher(cn.hutool.core.io.watch.SimpleWatcher) IORuntimeException(cn.hutool.core.io.IORuntimeException) IOException(java.io.IOException) SettingRuntimeException(cn.hutool.setting.SettingRuntimeException) SettingRuntimeException(cn.hutool.setting.SettingRuntimeException)

Aggregations

SimpleWatcher (cn.hutool.core.io.watch.SimpleWatcher)3 Path (java.nio.file.Path)3 WatchEvent (java.nio.file.WatchEvent)3 SettingRuntimeException (cn.hutool.setting.SettingRuntimeException)2 IORuntimeException (cn.hutool.core.io.IORuntimeException)1 WatchMonitor (cn.hutool.core.io.watch.WatchMonitor)1 Watcher (cn.hutool.core.io.watch.Watcher)1 DelayWatcher (cn.hutool.core.io.watch.watchers.DelayWatcher)1 IOException (java.io.IOException)1