import de.juplo.kafka.chat.backend.api.MessageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
-import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.reactive.function.client.WebClient;
-import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
-import reactor.util.retry.Retry;
-import java.nio.charset.Charset;
-import java.time.Duration;
-import java.util.concurrent.ThreadLocalRandom;
+import java.util.*;
@Slf4j
{
Flux
.fromArray(chatRooms)
- .flatMap(chatRoom -> receiveMessages(chatRoom)
- .flatMap(sse ->
- {
- try
+ .flatMap(chatRoom ->
+ {
+ List<MessageTo> list = new LinkedList<>();
+ receivedMessages.put(chatRoom.getId(), list);
+ return receiveMessages(chatRoom)
+ .flatMap(sse ->
{
- return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
- }
- catch (Exception e)
+ try
+ {
+ return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
+ }
+ catch (Exception e)
+ {
+ return Mono.error(e);
+ }
+ })
+ .doOnNext(message ->
{
- return Mono.error(e);
- }
- })
- .doOnNext(message -> log.info(
- "Received a message from chat-room {}: {}",
- chatRoom,
- message))
- .take(30))
+ list.add(message);
+ log.info(
+ "Received a message from chat-room {}: {}",
+ chatRoom,
+ message);
+ })
+ .take(30);
+ })
.takeUntil(message -> !running)
.then()
.block();
private final ChatRoomInfoTo[] chatRooms;
private final ObjectMapper objectMapper;
+ final Map<UUID, List<MessageTo>> receivedMessages = new HashMap<>();
+
volatile boolean running = true;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
.flatMap(message -> sendMessage(chatRoom, message)
.delayElement(Duration.ofMillis(ThreadLocalRandom.current().nextLong(500, 1500)))
.retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1))))
- .doOnNext(message -> log.info(
- "{} sent a message to {}: {}",
- user,
- chatRoom,
- message))
+ .doOnNext(message ->
+ {
+ sentMessages.add(message);
+ log.info(
+ "{} sent a message to {}: {}",
+ user,
+ chatRoom,
+ message);
+ })
.doOnError(throwable ->
{
WebClientResponseException e = (WebClientResponseException)throwable.getCause();
private final ChatRoomInfoTo chatRoom;
private final User user;
+ final List<MessageTo> sentMessages = new LinkedList<>();
+
volatile boolean running = true;