From: Kai Moritz Date: Sun, 16 Apr 2023 09:54:37 +0000 (+0200) Subject: NEU X-Git-Tag: kafkadata~19 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=9aaca321143c4fd08859dc14cdd7e602cc1714f6;p=demos%2Fkafka%2Fchat NEU --- diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomTo.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomTo.java new file mode 100644 index 00000000..9aa60947 --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatRoomTo.java @@ -0,0 +1,33 @@ +package de.juplo.kafka.chat.backend.persistence.kafka; + +import de.juplo.kafka.chat.backend.domain.ChatRoom; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.Clock; +import java.util.UUID; + + +@Data +@NoArgsConstructor +@AllArgsConstructor(staticName = "of") +public class ChatRoomTo +{ + private UUID id; + private String name; + private int shard; + + public ChatRoom toChatRoom( + Clock clock, + KafkaChatRoomService service, + int bufferSize) + { + return new ChatRoom(id, name, shard, clock, service, bufferSize); + } + + public static ChatRoomTo from(ChatRoom chatRoom) + { + return ChatRoomTo.of(chatRoom.getId(), chatRoom.getName(), chatRoom.getShard()); + } +} diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java index 22cd74b7..e23f08db 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java @@ -26,9 +26,12 @@ import java.util.stream.IntStream; @Slf4j public class KafkaChatHomeService implements ChatHomeService, Runnable, ConsumerRebalanceListener { - private final Consumer consumer; - private final Producer producer; - private final String topic; + private final String chatRoomsTopic; + private final Consumer chatRoomsConsumer; + private final Producer chatRoomsProducer; + private final String chatMessagesTopic; + private final Consumer chatMessagesConsumer; + private final Producer chatMessagesProducer; private final ZoneId zoneId; private final int numShards; private final boolean[] isShardOwned; @@ -42,16 +45,22 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer public KafkaChatHomeService( - Consumer consumer, - Producer producer, - String topic, + String chatRoomsTopic, + Consumer chatRoomsConsumer, + Producer chatRoomsProducer, + String chatMessagesTopic, + Consumer chatMessagesConsumer, + Producer chatMessagesProducer, ZoneId zoneId, int numShards) { log.debug("Creating KafkaChatHomeService"); - this.consumer = consumer; - this.producer = producer; - this.topic = topic; + this.chatRoomsTopic = chatRoomsTopic; + this.chatRoomsConsumer = chatRoomsConsumer; + this.chatRoomsProducer = chatRoomsProducer; + this.chatMessagesTopic = chatMessagesTopic; + this.chatMessagesConsumer = chatMessagesConsumer; + this.chatMessagesProducer = chatMessagesProducer; this.zoneId = zoneId; this.numShards = numShards; this.isShardOwned = new boolean[numShards]; @@ -68,7 +77,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer log.info("Newly assigned partitions! Pausing normal operations..."); loadInProgress = true; - consumer.endOffsets(partitions).forEach((topicPartition, currentOffset) -> + chatMessagesConsumer.endOffsets(partitions).forEach((topicPartition, currentOffset) -> { int partition = topicPartition.partition(); isShardOwned[partition] = true; @@ -80,10 +89,10 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer nextOffset[partition], currentOffset); - consumer.seek(topicPartition, nextOffset[partition]); + chatMessagesConsumer.seek(topicPartition, nextOffset[partition]); }); - consumer.resume(partitions); + chatMessagesConsumer.resume(partitions); } @Override @@ -108,7 +117,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer @Override public void run() { - consumer.subscribe(List.of(topic)); + chatMessagesConsumer.subscribe(List.of(chatMessagesTopic)); running = true; @@ -116,7 +125,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer { try { - ConsumerRecords records = consumer.poll(Duration.ofMinutes(5)); + ConsumerRecords records = chatMessagesConsumer.poll(Duration.ofMinutes(5)); log.info("Fetched {} messages", records.count()); if (loadInProgress) @@ -185,10 +194,10 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer void pauseAllOwnedPartions() { - consumer.pause(IntStream + chatMessagesConsumer.pause(IntStream .range(0, numShards) .filter(shard -> isShardOwned[shard]) - .mapToObj(shard -> new TopicPartition(topic, shard)) + .mapToObj(shard -> new TopicPartition(chatMessagesTopic, shard)) .toList()); } @@ -199,7 +208,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer String text) { int shard = this.shardingStrategy.selectShard(chatRoomId); - TopicPartition tp = new TopicPartition(topic, shard); + TopicPartition tp = new TopicPartition(chatMessagesTopic, shard); ZonedDateTime zdt = ZonedDateTime.of(timestamp, zoneId); return Mono.create(sink -> { @@ -211,7 +220,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer chatRoomId.toString(), MessageTo.of(key.getUsername(), key.getMessageId(), text)); - producer.send(record, ((metadata, exception) -> + chatMessagesProducer.send(record, ((metadata, exception) -> { if (metadata != null) { @@ -239,6 +248,8 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer public void putChatRoom(ChatRoom chatRoom) { + + ProducerRecord record = new ProducerRecord<>(chatRoom.getShard(), ); // TODO: Nachricht senden! chatrooms[chatRoom.getShard()].put(chatRoom.getId(), chatRoom); }