简体   繁体   English

来自无限Java流的磁通端点

[英]Flux endpoint from infinite java stream

I have an issue while processing a flux that is built from a Stream.generate construct. 处理从Stream.generate构造构建的流量时遇到问题。

The Java stream is fetching some data from a remote source, hence I implemented a custom supplier that has the data fetching logic embedded, and then used it to populate the Stream. Java流正在从远程源中获取一些数据,因此我实现了一个自定义的供应商,该供应商嵌入了数据获取逻辑,然后使用它来填充Stream。

Stream.generate(new SearchSupplier(...))

My idea is to detect an empty list and use the Java9 feature of takeWhile -> 我的想法是检测一个空列表并使用的Java9功能takeWhile - >

Stream.generate(new SearchSupplier(this, queryBody))
            .takeWhile(either -> either.isRight() && either.get().nonEmpty())

(using Vavr's Either construct) (使用Vavr的Either构造)

The repositoroy layer flux will then do: 然后,存储层通量将执行以下操作:

return Flux.fromStream (
            this.searchStream(...) //this is where the stream gets generated
        )
        .map(Either::get)
        .flatMap(Flux::fromIterable);

The "service" layer is composed of some transformation steps on the flux, but the method signature is something like Flux<JsonObject> search(...) . “服务”层由通量上的一些转换步骤组成,但方法签名类似于Flux<JsonObject> search(...)

Finally, the controller layer has a GetMapping: 最后,控制器层具有GetMapping:

@GetMapping(produces = "application/stream+json")
public Flux search(...) {
    return searchService.search(...) //this is the Flux<JsonObject> parth
         .subscriberContext(...) //stuff I need available during processing
         .doOnComplete(() -> log.debug("DONE")); 
}

My problem is that the Flux seems to never terminate. 我的问题是助焊剂似乎永远不会终止。 Doing a call from Postman for example just shot the 'Loading...' part in the response section. 例如,从Postman拨打电话只是在响应部分中拍摄了“正在加载...”部分。 When I terminate the process from my IDE the results are then flushed to postman and I see what I'm expecting. 当我从IDE中终止该过程时,结果将刷新到邮递员,我看到了期望的结果。 Also the doOnComplete lambda never gets called 而且doOnComplete lambda永远不会被调用

What I noticed is that if I change the source of a Flux: 我注意到的是,如果我改变助焊剂的来源:

Flux.fromArray(...) //harcoded array of lists of jsons

the doOnComplete lambda is called and also the http connection closes, and results are displayed in postman. doOnComplete lambda被调用,并且http连接也关闭,结果显示在邮递员中。

Any idea of what might be the issue? 知道可能是什么问题吗?

Thanks. 谢谢。

You could create the Flux directly using code that looks like this. 您可以使用如下代码直接创建Flux。 Note that I'm adding some assumed methods which you would need to implement based on your how your SearchSupplier works: 请注意,我要添加一些假定的方法,这些方法需要根据您的SearchSupplier的工作方式来实现:

Flux<SearchResultType> flux = Flux.generate(
            () -> new SearchSupplier(this, queryBody),
            (supplier, sink) -> {
                SearchResultType current = supplier.next();
                if (isNotLast(current)) {
                    sink.next(current);
                } else {
                    sink.complete();
                }
                return supplier;
            },
            supplier -> anyCleanupOperations(supplier)
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM