Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态更改(使用JMX)正在监视的文件夹以查找新文件_Java_Spring Boot_Jmx - Fatal编程技术网

Java 动态更改(使用JMX)正在监视的文件夹以查找新文件

Java 动态更改(使用JMX)正在监视的文件夹以查找新文件,java,spring-boot,jmx,Java,Spring Boot,Jmx,我正在使用SpringBoot查看文件夹中的新文件。 配置如下所示: @Configuration @ManagedResource public class FileWatcherConfig { Logger log = LoggerFactory.getLogger(FileWatcherConfig.class); @Value("${filewatcher.path:C:\\test}") String pathname; @Bean pub

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

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

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