【发布时间】: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