Is it ok to use mix of reactive code i.e Mono & Flux Objects which are reactive streams along with Java 8 Streams

Arun Gupta :

I have two objects which contain list in each of them and my whole spring boot project is using project reactor constructs. Should I use java streams inside the flux or mono map operations for traversing & operations on data ?

Flux.fromIterable(page.getPageFieldData()).subscribeOn(Schedulers.elastic()).map(pageField - > {
        clientDataMono.subscribeOn(Schedulers.elastic()).map(clientData - > {
            Flux.fromIterable(clientData.getPageFieldData())
            .filter(clientPageField - > clientPageField.getId() == pageField.getId())
            .subscribeOn(Schedulers.elastic()).map(field - > {
                dataUpdated = true;
                pageField.setData(field.getData());
                return field;
            }).subscribe();
            return clientData;
        }).subscribe();
        if (dataUpdated) {
            pageField.setModifiedOn(Instant.now(Clock.systemUTC()));
        }
        return pageField;
    }).subscribe();

Or I should use java streams inside the map operations of the flux.

Michael Berry :

I sometimes feel like we're seeing a rehash of the same "wanting to use streams for everything" mindset just after Java 8 emerged, but with Reactor / RXJava.

Should I use java streams inside the flux or mono map operations for traversing & operations on data ?

The short answer is that if you can sensbily use Java streams, and Reactor doesn't offer any advantage in the situation, then there's no reason to use Reactor. Reactor / reactive programming is fantastic when the use case calls for it, but bear in mind that it's an external library (not bundled in the core JRE), it's much more complex, and much less universally understood than standard Java streams.

Reactor is of course much, much more powerful. If you need control over how fast values are produced, backpressure, detailed publisher / subscriber behaviour, multicasting, caching, retrying, etc., or you may feasibly in the future, then it'll win hands down every time.

But if you're literally just filtering and mapping an existing Java collection, as you appear to be in that example, where reactor could offer no improvement, I see no good reason to not use standard Java streams.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323648&siteId=1