小编Zhu*_*ehi的帖子

如何中止Spring-Boot启动?

我正在编写一个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启动.有人知道我怎么做吗?

java watchservice spring-boot

7
推荐指数
2
解决办法
5110
查看次数

标签 统计

java ×1

spring-boot ×1

watchservice ×1