如何监控spring中的文件夹/目录?

时间:2016-10-13 12:28:29

标签: java spring-boot spring-integration

我不想在春天编写Spring Boot Application,它将监视windows中的目录,当我更改子文件夹或添加新文件夹或删除现有文件夹时,我想获得有关该信息的信息。

我该怎么做? 我读过这个: http://docs.spring.io/spring-integration/reference/html/files.html 以及谷歌中'spring file watcher'下的每个结果, 但我找不到解决方案......

你有这么好的文章或例子吗? 我不喜欢这样:

@SpringBootApplication
@EnableIntegration
public class SpringApp{

    public static void main(String[] args) {
        SpringApplication.run(SpringApp.class, args);
    }

    @Bean
    public WatchService watcherService() {
        ...//define WatchService here
    }
}

此致

7 个答案:

答案 0 :(得分:6)

spring-boot-devtools具有FileSystemWatcher

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

FileWatcherConfig

@Configuration
public class FileWatcherConfig {
    @Bean
    public FileSystemWatcher fileSystemWatcher() {
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(true, Duration.ofMillis(5000L), Duration.ofMillis(3000L));
        fileSystemWatcher.addSourceFolder(new File("/path/to/folder"));
        fileSystemWatcher.addListener(new MyFileChangeListener());
        fileSystemWatcher.start();
        System.out.println("started fileSystemWatcher");
        return fileSystemWatcher;
    }

    @PreDestroy
    public void onDestroy() throws Exception {
        fileSystemWatcher().stop();
    }
}

MyFileChangeListener

@Component
public class MyFileChangeListener implements FileChangeListener {

    @Override
    public void onChange(Set<ChangedFiles> changeSet) {
        for(ChangedFiles cfiles : changeSet) {
            for(ChangedFile cfile: cfiles.getFiles()) {
                if( /* (cfile.getType().equals(Type.MODIFY) 
                     || cfile.getType().equals(Type.ADD)  
                     || cfile.getType().equals(Type.DELETE) ) && */ !isLocked(cfile.getFile().toPath())) {
                    System.out.println("Operation: " + cfile.getType() 
                      + " On file: "+ cfile.getFile().getName() + " is done");
                }
            }
        }
    }
    
    private boolean isLocked(Path path) {
        try (FileChannel ch = FileChannel.open(path, StandardOpenOption.WRITE); FileLock lock = ch.tryLock()) {
            return lock == null;
        } catch (IOException e) {
            return true;
        }
    }

}

答案 1 :(得分:3)

请参阅Spring Integration Samples Repo&#39; basic&#39;下的文件示例。

在应用程序file-split-ftp下有一个更新,更复杂的示例 - 它使用Spring Boot和Java配置Vs.旧样本中使用的xml。

答案 2 :(得分:2)

答案 3 :(得分:2)

在Java 7中有WatchService-这将是最好的解决方案。

Spring配置可能如下所示:

@Slf4j
@Configuration
public class MonitoringConfig {

    @Value("${monitoring-folder}")
    private String folderPath;

    @Bean
    public WatchService watchService() {
        log.debug("MONITORING_FOLDER: {}", folderPath);
        WatchService watchService = null;
        try {
            watchService = FileSystems.getDefault().newWatchService();
            Path path = Paths.get(folderPath);

            if (!Files.isDirectory(path)) {
                throw new RuntimeException("incorrect monitoring folder: " + path);
            }

            path.register(
                    watchService,
                    StandardWatchEventKinds.ENTRY_DELETE,
                    StandardWatchEventKinds.ENTRY_MODIFY,
                    StandardWatchEventKinds.ENTRY_CREATE
            );
        } catch (IOException e) {
            log.error("exception for watch service creation:", e);
        }
        return watchService;
    }
}

还有用于启动监视自身的Bean:

@Slf4j
@Service
@AllArgsConstructor
public class MonitoringServiceImpl {

    private final WatchService watchService;

    @Async
    @PostConstruct
    public void launchMonitoring() {
        log.info("START_MONITORING");
        try {
            WatchKey key;
            while ((key = watchService.take()) != null) {
                for (WatchEvent<?> event : key.pollEvents()) {
                    log.debug("Event kind: {}; File affected: {}", event.kind(), event.context());
                }
                key.reset();
            }
        } catch (InterruptedException e) {
            log.warn("interrupted exception for monitoring service");
        }
    }

    @PreDestroy
    public void stopMonitoring() {
        log.info("STOP_MONITORING");

        if (watchService != null) {
            try {
                watchService.close();
            } catch (IOException e) {
                log.error("exception while closing the monitoring service");
            }
        }
    }
}

此外,您还必须为应用程序类(其配置)设置@EnableAsync

并从application.yml中摘录:

监视文件夹:C:\ Users \ nazar_art

已通过Spring Boot 2.3.1测试。


还使用了异步池配置:

@Slf4j
@EnableAsync
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(AsyncProperties.class)
public class AsyncConfiguration implements AsyncConfigurer {

    private final AsyncProperties properties;

    @Override
    @Bean(name = "taskExecutor")
    public Executor getAsyncExecutor() {
        log.debug("Creating Async Task Executor");
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(properties.getCorePoolSize());
        taskExecutor.setMaxPoolSize(properties.getMaxPoolSize());
        taskExecutor.setQueueCapacity(properties.getQueueCapacity());
        taskExecutor.setThreadNamePrefix(properties.getThreadName());
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Bean
    public TaskScheduler taskScheduler() {
        return new ConcurrentTaskScheduler();
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new CustomAsyncExceptionHandler();
    }
}

自定义异步异常处理程序的位置是:

@Slf4j
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
        log.error("Exception for Async execution: ", throwable);
        log.error("Method name - {}", method.getName());
        for (Object param : objects) {
            log.error("Parameter value - {}", param);
        }
    }
}

答案 4 :(得分:0)

这里没有详细说明可能会帮助你的一些指示。

您可以从SławomirCzaja的答案中获取目录WatchService代码:

  

你可以使用纯java,不需要spring https://docs.oracle.com/javase/tutorial/essential/io/notification.html

并将该代码包装到一个可运行的任务中。此任务可以使用SimpMessagingTemplate通知客户目录更改,如下所述: Websocket STOMP handle send

然后你可以创建一个如下所述的调度程序: Scheduling处理任务的启动和重新启动。

不要忘记在mvc-config中配置调度和websocket支持以及客户端的STOMP支持(进一步阅读:STOMP over Websocket

答案 5 :(得分:0)

以防万一,如果有人在寻找递归子文件夹监视程序,则此链接可能会有所帮助:How to watch a folder and subfolders for changes

答案 6 :(得分:0)

@catch23 我找到了一个解决方法 您可以通过 @Scheduled(fixedDelay = Long.MAX_VALUE) 注释您的任务 你可以在这里查看代码

`

@Scheduled(fixedDelay = Long.MAX_VALUE)
 public void watchTask() {
            {
                this.loadOnStartup();
                try {
                    WatchService watcher = FileSystems.getDefault().newWatchService();
                    Path file = Paths.get(propertyFile);
                    Path dir = Paths.get(file.getParent().toUri());
                    dir.register(watcher, ENTRY_MODIFY);
                    logger.info("Watch Service registered for dir: " + dir.getFileName());
    
                    while (true) {
                        WatchKey key;
                        try {
                            key = watcher.take();
                        } catch (InterruptedException ex) {
                            return;
                        }
    
                        for (WatchEvent<?> event : key.pollEvents()) {
                            WatchEvent.Kind<?> kind = event.kind();
    
                            @SuppressWarnings("unchecked")
                            WatchEvent<Path> ev = (WatchEvent<Path>) event;
                            Path fileName = ev.context();
                            logger.debug(kind.name() + ": " + fileName);
                            if (kind == ENTRY_MODIFY &&
                                    fileName.toString().equals(file.getFileName().toString())) {
                                    //publish event here
                            }
                        }
                        boolean valid = key.reset();
                        if (!valid) {
                            break;
                        }
                    }
                } catch (Exception ex) {
                    logger.error(ex.getMessage(), ex);
                }
            }
    
        }

`