英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论