【问题标题】:Change dynamically (using JMX) the folder being watched for new files动态更改(使用 JMX)正在监视新文件的文件夹
【发布时间】:2020-05-30 12:43:35
【问题描述】:

我正在使用 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 更改路径名值,但应用程序仍会监视初始文件夹。

【问题讨论】:

    标签: java spring-boot jmx


    【解决方案1】:

    当您通过 JMX 更改路径时,所做的只是为 FileWatcherConfig bean 中的 pathname 字段分配一个新值。

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

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2012-11-21
      • 1970-01-01
      • 2016-11-23
      相关资源
      最近更新 更多