1 package de.juplo.kafka.chat.backend;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.fasterxml.jackson.databind.SerializationFeature;
5 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
6 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
7 import de.juplo.kafka.chat.backend.api.MessageTo;
8 import lombok.extern.slf4j.Slf4j;
9 import org.springframework.core.ParameterizedTypeReference;
10 import org.springframework.http.HttpStatus;
11 import org.springframework.http.MediaType;
12 import org.springframework.http.codec.ServerSentEvent;
13 import org.springframework.web.reactive.function.client.WebClient;
14 import org.springframework.web.reactive.function.client.WebClientResponseException;
15 import reactor.core.publisher.Flux;
16 import reactor.core.publisher.Mono;
17 import reactor.util.retry.Retry;
19 import java.nio.charset.Charset;
20 import java.time.Duration;
21 import java.util.concurrent.ThreadLocalRandom;
25 public class TestListener implements Runnable
27 static final ParameterizedTypeReference<ServerSentEvent<String>> SSE_TYPE = new ParameterizedTypeReference<>() {};
35 .flatMap(chatRoom -> receiveMessages(chatRoom)
40 return Mono.just(objectMapper.readValue(sse.data(), MessageTo.class));
47 .doOnNext(message -> log.info(
48 "Received a message from chat-room {}: {}",
52 .takeUntil(message -> !running)
57 Flux<ServerSentEvent<String>> receiveMessages(ChatRoomInfoTo chatRoom)
62 "/{chatRoomId}/listen",
64 .accept(MediaType.TEXT_EVENT_STREAM)
66 .bodyToFlux(SSE_TYPE);
70 private final WebClient webClient;
71 private final ChatRoomInfoTo[] chatRooms;
72 private final ObjectMapper objectMapper;
74 volatile boolean running = true;
77 TestListener(Integer port, ChatRoomInfoTo[] chatRooms)
79 webClient = WebClient.create("http://localhost:" + port);
80 this.chatRooms = chatRooms;
81 objectMapper = new ObjectMapper();
82 objectMapper.registerModule(new JavaTimeModule());
83 objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);