From: Kai Moritz Date: Tue, 20 Feb 2024 07:50:56 +0000 (+0100) Subject: refactor: One stream -> using `flatMap` instead of an inner `subscribe` X-Git-Tag: rebase--2024-02-20--15-07~10 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=3e8f8bfc16a93711b709e4e638a11045db7ac960;p=demos%2Fkafka%2Fchat refactor: One stream -> using `flatMap` instead of an inner `subscribe` --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java index 99fe54d0..487a37fe 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/StorageStrategy.java @@ -6,6 +6,7 @@ import de.juplo.kafka.chat.backend.domain.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.UUID; @@ -19,15 +20,15 @@ public interface StorageStrategy return writeChatRoomInfo( chatHomeService .getChatRoomInfo() - .doOnNext(chatRoomInfo -> writeChatRoomData( + .flatMap(chatRoomInfo -> writeChatRoomData( chatRoomInfo.getId(), chatHomeService .getChatRoomData(chatRoomInfo.getId()) .flatMapMany(chatRoomData -> chatRoomData.getMessages()) ) - .doOnComplete(() -> log.info("Stored {}", chatRoomInfo)) - .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable)) - .subscribe()) + .then(Mono.just(chatRoomInfo)) + .doOnSuccess(emittedChatRoomInfo -> log.info("Stored {}", chatRoomInfo)) + .doOnError(throwable -> log.error("Could not store {}: {}", chatRoomInfo, throwable))) ) .doOnComplete(() -> log.info("Stored {}", chatHomeService)) .doOnError(throwable -> log.error("Could not store {}: {}", chatHomeService, throwable));