动态更改(使用 JMX)正在监视的用于新文件的文件夹。

huangapple go评论68阅读模式
英文:

Change dynamically (using JMX) the folder being watched for new files

问题

我正在使用SpringBoot来监视一个文件夹以获取新文件
配置如下

@Configuration
@ManagedResource
public class FileWatcherConfig {
    Logger log = LoggerFactory.getLogger(FileWatcherConfig.class);

    @Value("${filewatcher.path:C:\\test}")
    String pathname;

    @Bean
    public FileSystemWatcher fileSystemWatcher() {
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(true, Duration.ofMillis(5000L), Duration.ofMillis(3000L));
        fileSystemWatcher.addSourceDirectory(new File(pathname));
        fileSystemWatcher.addListener(new MyFileChangeListener());
        fileSystemWatcher.start();
        log.info("started fileSystemWatcher");
        return fileSystemWatcher;
    }

    @PreDestroy
    public void onDestroy() {
        fileSystemWatcher().stop();
    }

    @ManagedOperation
    public void setPathname(String pathname) {
        this.pathname = pathname;
    }

    @ManagedAttribute
    public String getPathname(){
        return pathname;
    }
}
尽管我能够使用jconsole在运行时更改pathname的值但应用程序仍然监视最初的文件夹
英文:

I'm using SpringBoot to watch a folder for new files.
The configuration looks like this:

@Configuration
@ManagedResource
public class FileWatcherConfig {
    Logger log = LoggerFactory.getLogger(FileWatcherConfig.class);

    @Value("${filewatcher.path:C:\\test}")
    String pathname;

    @Bean
    public FileSystemWatcher fileSystemWatcher() {
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(true, Duration.ofMillis(5000L), Duration.ofMillis(3000L));
        fileSystemWatcher.addSourceDirectory(new File(pathname));
        fileSystemWatcher.addListener(new MyFileChangeListener());
        fileSystemWatcher.start();
        log.info("started fileSystemWatcher");
        return fileSystemWatcher;
    }

    @PreDestroy
    public void onDestroy() {
        fileSystemWatcher().stop();
    }

    @ManagedOperation
    public void setPathname(String pathname) {
        this.pathname = pathname;
    }

    @ManagedAttribute
    public String getPathname(){
        return pathname;
    }
}

Although I'm able to change the pathname value in runtime using jconsole the application still watches the initial folder.

答案1

得分: 1

当您通过JMX更改路径时,所有正在进行的操作只是将FileWatcherConfig bean中的pathname字段赋予一个新值。

在路径更改后,您需要编写额外的代码,以更新FileSystemWatcher bean的新路径,这可能包括停止它,移除当前监听器,然后更新源目录。

英文:

When you change the path via JMX, all that's doing is assigning a new value to the pathname field in the FileWatcherConfig bean.

You'll need to write additional code to update the FileSystemWatcher bean with the new path after it changes -- including potentially stopping it, removing the current listener, and then updating the source directory.

huangapple
  • 本文由 发表于 2020年5月30日 20:43:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定