我正在寫一個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