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.Mono;
10 import reactor.util.retry.Retry;
12 import java.time.Duration;
13 import java.util.concurrent.ThreadLocalRandom;
17 public class TestClient implements Runnable
22 for (int i = 0; running; i++)
24 String message = "Message #" + i;
25 for (ChatRoomInfoTo chatRoom : chatRooms)
27 sendMessage(chatRoom, message)
28 .retryWhen(Retry.fixedDelay(10, Duration.ofSeconds(1)))
29 .map(MessageTo::toString)
30 .subscribe(result -> log.info(
31 "{} sent message \"{}\" to {}",
38 Thread.sleep(ThreadLocalRandom.current().nextLong(700, 1000));
42 throw new RuntimeException(e);
47 private Mono<MessageTo> sendMessage(
48 ChatRoomInfoTo chatRoom,
54 "/{chatRoomId}/{username}/{serial}",
58 .contentType(MediaType.TEXT_PLAIN)
59 .accept(MediaType.APPLICATION_JSON)
61 .exchangeToMono(response ->
63 if (response.statusCode().equals(HttpStatus.OK))
65 return response.bodyToMono(MessageTo.class);
69 return response.createError();
75 private final WebClient webClient;
76 private final ChatRoomInfoTo[] chatRooms;
77 private final User user;
79 volatile boolean running = true;
82 TestClient(Integer port, ChatRoomInfoTo[] chatRooms, String username)
84 webClient = WebClient.create("http://localhost:" + port);
85 this.chatRooms = chatRooms;
86 user = new User(username);