我正在写一个Spring-Boot应用程序来监视一个目录并处理正在添加的文件。我注册WatchService的目录中配置类:如何中止Spring-Boot启动?
@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;
}
}
我想优雅地放弃春节引导启动,如果注册目录失败。有人知道我能做到吗?
您是否试过'System.exit(SIGNUM)' – Turtle
我宁愿优雅地去做。 System.stop()立即杀死** JVM **。 –
然后'RuntimeException'? – Turtle