1 package de.juplo.kafka.chat.backend;
3 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
4 import de.juplo.kafka.chat.backend.api.MessageTo;
5 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.http.HttpStatus;
7 import org.springframework.http.MediaType;
8 import org.springframework.web.reactive.function.client.WebClient;
9 import reactor.core.publisher.Flux;
10 import reactor.core.publisher.Mono;
11 import reactor.util.retry.Retry;
13 import java.time.Duration;
17 public class TestClient
25 .map(chatRoom -> sendMessage(chatRoom, "Message #" + i))
26 .flatMap(result -> result
27 .map(MessageTo::toString)
28 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))))
29 .subscribe(result -> log.info("{}", result));
32 private Mono<MessageTo> sendMessage(
33 ChatRoomInfoTo chatRoom,
39 "/{chatRoomId}/{username}/{serial}",
43 .contentType(MediaType.TEXT_PLAIN)
44 .accept(MediaType.APPLICATION_JSON)
46 .exchangeToMono(response ->
48 if (response.statusCode().equals(HttpStatus.OK))
50 return response.bodyToMono(MessageTo.class);
54 return response.createError();
60 private final WebClient webClient;
61 private final ChatRoomInfoTo[] chatRooms;
62 private final User user;
65 TestClient(Integer port, ChatRoomInfoTo[] chatRooms, String username)
67 webClient = WebClient.create("http://localhost:" + port);
68 this.chatRooms = chatRooms;
69 user = new User(username);