我正在编写一个Spring-Boot应用程序来监视目录并处理正在添加到它的文件.我在配置类中使用WatchService注册目录:
@Configuration
public class WatchServiceConfig {
private static final Logger logger = LogManager.getLogger(WatchServiceConfig.class);
@Value("${dirPath}")
private String dirPath;
@Bean
public WatchService register() {
WatchService watchService = null;
try {
watchService = FileSystems.getDefault().newWatchService();
Paths.get(dirPath).register(watchService, ENTRY_CREATE);
logger.info("Started watching \"{}\" directory ", dlsDirPath);
} catch (IOException e) {
logger.error("Failed to create WatchService for directory \"" + dirPath + "\"", e);
}
return watchService;
}
}
Run Code Online (Sandbox Code Playgroud)
如果注册目录失败,我想优雅地中止Spring Boot启动.有人知道我怎么做吗?