【问题标题】:Java WatchService unexpectedly stops workingJava WatchService 意外停止工作
【发布时间】:2017-05-05 08:42:01
【问题描述】:

我有一个递归监视服务,用于在应用程序运行时监视目录。由于未知原因,watchservice 似乎在大约一天后停止工作。那时我可以将一个新文件添加到受监视的目录中,并且不会收到任何日志语句,并且不会通知我的观察者。

我认为 Spring 可能正在销毁 bean,所以我在类的 @pre-destroy 部分添加了一条日志语句,但是在 watchservice 停止工作后该日志语句没有显示,所以似乎 bean 仍然存在,只是没有按预期运行。类如下

import com.sun.nio.file.SensitivityWatchEventModifier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;

@Service
public class DirectoryMonitor {

private static final Logger logger = LoggerFactory.getLogger(DirectoryMonitor.class);
private WatchService watcher;
private ExecutorService executor;
private List<DirectoryMonitorObserver> observerList = new ArrayList<>();
private final Map<WatchKey, Path> keys = new HashMap<>();

public void addObserver(DirectoryMonitorObserver observer){
    observerList.add(observer);
}

private void notifyObservers(){
    observerList.forEach(DirectoryMonitorObserver::directoryModified);
}

@PostConstruct
public void init() throws IOException {
    watcher = FileSystems.getDefault().newWatchService();
    executor = Executors.newSingleThreadExecutor();
}

@PreDestroy
public void cleanup() {
    try {
        logger.info("Stopping directory monitor");
        watcher.close();
    } catch (IOException e) {
        logger.error("Error closing watcher service", e);
    }
    executor.shutdown();
}

@SuppressWarnings("unchecked")
public void startRecursiveWatcher(String pathToMonitor) {
    logger.info("Starting Recursive Watcher");

    Consumer<Path> register = p -> {
        if (!p.toFile().exists() || !p.toFile().isDirectory())
            throw new RuntimeException("folder " + p + " does not exist or is not a directory");

        try {
            Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    logger.info("registering " + dir + " in watcher service");
                    WatchKey watchKey = dir.register(watcher, new WatchEvent.Kind[]{ENTRY_CREATE, ENTRY_DELETE}, SensitivityWatchEventModifier.HIGH);
                    keys.put(watchKey, dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            throw new RuntimeException("Error registering path " + p);
        }
    };

    register.accept(Paths.get(pathToMonitor));

    executor.submit(() -> {
        while (true) {
            final WatchKey key;
            try {
                key = watcher.take();
            } catch (InterruptedException ex) {
                logger.error(ex.toString());
                continue;
            }

            final Path dir = keys.get(key);

            key.pollEvents().stream()
                    .map(e -> ((WatchEvent<Path>) e).context())
                    .forEach(p -> {
                        final Path absPath = dir.resolve(p);
                        if (absPath.toFile().isDirectory()) {
                            register.accept(absPath);
                        } else {
                            final File f = absPath.toFile();
                            logger.info("Detected new file " + f.getAbsolutePath());
                        }
                    });

            notifyObservers();
            key.reset();
        }
    });
}

}

这是我创建监视器 bean 的地方..

@Component
public class MovieInfoFacade {
    @Value("${media.path}")
    private String mediaPath;
    private MovieInfoControl movieInfoControl;
    private DirectoryMonitor directoryMonitor;
    private FileListProvider fileListProvider;

@Autowired
public MovieInfoFacade(MovieInfoControl movieInfoControl, DirectoryMonitor directoryMonitor, FileListProvider fileListProvider){
    this.movieInfoControl = movieInfoControl;
    this.directoryMonitor = directoryMonitor;
    this.fileListProvider = fileListProvider;
}

@PostConstruct
public void startDirectoryMonitor(){
    if(!mediaPath.equalsIgnoreCase("none")) {
        directoryMonitor.addObserver(fileListProvider);
        directoryMonitor.startRecursiveWatcher(mediaPath);
    }
}

public int loadMovieListLength(String directoryPath){
    return fileListProvider.listFiles(directoryPath).length;
}

public List<MovieInfo> loadMovieList(MovieSearchCriteria searchCriteria) {
    List<File> files = Arrays.asList(fileListProvider.listFiles(searchCriteria.getPath()));

    return files.parallelStream()
            .sorted()
            .skip(searchCriteria.getPage() * searchCriteria.getItemsPerPage())
            .limit(searchCriteria.getItemsPerPage())
            .map(file -> movieInfoControl.loadMovieInfoFromCache(file.getAbsolutePath()))
            .collect(Collectors.toList());
}

public MovieInfo loadSingleMovie(String filePath) {
    return movieInfoControl.loadMovieInfoFromCache(filePath);
}

}

【问题讨论】:

  • 到底发生了什么?你怎么知道它停止了?您在操作系统的事件句柄中看到任何事件吗?
  • 我在问题中添加了更多细节。基本上我没有得到日志输出,我的观察者也没有像往常一样收到通知。
  • 你怎么知道程序实际上停止了呢? :-)

标签: java spring java-8 watchservice


【解决方案1】:

看来错误出在我的异常处理中。删除 throw 语句(并用日志替换它们)后,我没有任何问题。

【讨论】:

    猜你喜欢
    • 2014-06-09
    • 1970-01-01
    • 2017-06-29
    • 2021-12-07
    • 1970-01-01
    • 2018-12-05
    • 2012-10-18
    • 2020-12-14
    • 2017-07-28
    相关资源
    最近更新 更多