I'm writing a Spring-Boot application to monitor a directory and process files that are being added to it. I register the directory with WatchService in a configuration class:
@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;
}
}
I would like to abort Spring Boot startup gracefully if registering the directory fails. Does anybody know how I can do this?
System.exit(SIGNUM)
– TillerySystem.stop()
kills the JVM immediately. – VincentyRuntimeException
? – Tilleryregister
, which is a terrible name for aWatchService
bean. I'd strongly suggest renaming the method to something more appropriate for the bean name, e.g.public WatchService watchService() { ... }
. – Superman