动态集合上的Flux

时间:2018-01-04 16:41:26

标签: java spring project-reactor spring-webflux

好的,我有点疑惑我应该如何在Spring的Webflux / Reactor API中使用Reactor模式。

我们说我已经在我不断添加文件的目录中了。每当出现一个新文件时,我的应用程序都应该在它上面工作。如果队列已满,则应忽略新文件(因此不应忽略FileWatcher),直到队列中有空间为止。

以下只是伪代码,我试图理解一般的想法,而不是具体的实现细节。

班级目录观察员

管理计划任务,如果出现新文件,则每2秒检查一次。如果是,我们尝试将它们添加到队列中。

@PostConstruct
public void initialize() {

    this.flux = Flux.generate(consumer -> {
        consumer.next(this.deque.pop()); 
    });

    this.flux.log();
}

@Scheduled(fixedRate = 2000)
public void checkDirectory() {
   // list files, for each add to deque (if not present)
}

public Flux<Path> getObserver() {
    return this.flux; 
}

类FileProcessor

逐个消耗队列中的项目。

@PostConstruct
private void subscribe() {
    this.observeDirectorytask.getObserver().subscribe(path ->  {
        log.info("Processing '{}'", path.getFileName());

        // process file, delete it once done
    }); 
}

这种方法有意义吗?如果是,那么每当新项目被添加到队列时,我需要做什么才能触发我的订阅(现在这只在启动时执行一次)。

更新

这是我的工作实施:

public class DirectoryObserverTask {

@Autowired
private Path observedDirectory;

private Consumer<Path> newFilesConsumer;
private Consumer<Throwable> errorsConsumer;

private Flux<Path> flux; 


@PostConstruct
public void init() {
    this.observedDirectory = Paths.get(importDirectoryProperty);
}

public void subscribe(Consumer<Path> consumer) {
    if(this.flux == null) {
        this.flux = Flux.push(sink -> {
            this.onError(err -> sink.error(err));
            this.onNewFile(file -> sink.next(file));
        }); 
        this.flux = this.flux.onBackpressureBuffer(10,  BufferOverflowStrategy.DROP_LATEST); 
    }

    this.flux.subscribe(consumer); 

}


@Scheduled(fixedRate = 2000)
public void checkDirectoryContent() throws IOException {    
    Files.newDirectoryStream(this.observedDirectory).forEach(path -> {
        this.newFilesConsumer.accept(path);
    });
}

public void onNewFile(Consumer<Path> newFilesConsumer) {
    this.newFilesConsumer = newFilesConsumer;
}

public void onError(Consumer<Throwable> errorsConsumer) {
    this.errorsConsumer = errorsConsumer;
}

}

和消费者

@Autowired
private DirectoryObserverTask observeDirectorytask;

@PostConstruct
private void init() {
    observeDirectorytask.subscribe(path -> {
        this.processPath(path);
    });
}

public void processPath(Path t) {
    Mono.justOrEmpty(t)
        .subscribe(path -> {
            // handle the file 
            path.toFile().delete();
        });
}

1 个答案:

答案 0 :(得分:3)

您不需要使用队列,此行为已经内置。

我会做这样的事情:

  1. 使用文件观察程序检测更改。

  2. 将更改推送到Flux<File>

  3. 根据要求,限制排队事件的数量(使用背压):

    filesFlux.onBackPressureBuffer(10, BufferOverflowStrategy.DROP_LATEST)

  4. 照常订阅。

  5. 对背压的不良解释是:“当我们无法足够快地处理元素时该怎么做”。

    在这种情况下,我们将元素缓冲到10,然后在缓冲区已满时删除最新元素。

    更新:有很多方法可以创建Flux。在这种特殊情况下,我会看一下createpush方法(参见documentation)。

    示例:假设您有一个FileWatchService,您可以在其中注册检测到新文件的回调以及发生错误时的回调。你可以这样做:

    FileWatchService watcher = ...
    
    Flux<File> fileFlux = Flux.push(sink -> {
        watcher.onError(err -> sink.error(err));
        watcher.onNewFile(file -> sink.next(file));
    });
    
    fileFlux
        .onBackPressureBuffer(10, BufferOverflowStrategy.DROP_LATEST)
        .subscribe(...)