X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Fkafka%2FKafkaChatHomeService.java;h=e23f08db4677975bac0f5fcbf286a3278ec5d099;hb=9aaca321143c4fd08859dc14cdd7e602cc1714f6;hp=3ca5b7f7436f33d9476fec5be4f899a4f4244ee9;hpb=ead1db4a7daa0bcf944063f3920b3974c2f62e07;p=demos%2Fkafka%2Fchat 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 3ca5b7f7..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 @@ -20,23 +20,24 @@ import reactor.core.publisher.Mono; import java.time.*; import java.util.*; -import java.util.concurrent.ExecutorService; import java.util.stream.IntStream; @Slf4j public class KafkaChatHomeService implements ChatHomeService, Runnable, ConsumerRebalanceListener { - private final ExecutorService executorService; - 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; private final long[] currentOffset; private final long[] nextOffset; - private final Map[] chatRoomMaps; + private final Map[] chatrooms; private final KafkaLikeShardingStrategy shardingStrategy; private boolean running; @@ -44,24 +45,28 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer public KafkaChatHomeService( - ExecutorService executorService, - 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.executorService = executorService; - 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]; this.currentOffset = new long[numShards]; this.nextOffset = new long[numShards]; - this.chatRoomMaps = new Map[numShards]; + this.chatrooms = new Map[numShards]; this.shardingStrategy = new KafkaLikeShardingStrategy(numShards); } @@ -72,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; @@ -84,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 @@ -112,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; @@ -120,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) @@ -167,7 +172,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer Message message = new Message(key, record.offset(), timestamp, messageTo.getText()); - ChatRoom chatRoom = chatRoomMaps[record.partition()].get(chatRoomId); + ChatRoom chatRoom = chatrooms[record.partition()].get(chatRoomId); KafkaChatRoomService kafkaChatRoomService = (KafkaChatRoomService) chatRoom.getChatRoomService(); @@ -189,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()); } @@ -203,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 -> { @@ -215,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) { @@ -241,6 +246,14 @@ 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); + } + @Override public Mono getChatRoom(int shard, UUID id) { @@ -250,7 +263,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer } else { - return Mono.justOrEmpty(chatRoomMaps[shard].get(id)); + return Mono.justOrEmpty(chatrooms[shard].get(id)); } } @@ -263,7 +276,7 @@ public class KafkaChatHomeService implements ChatHomeService, Runnable, Consumer } else { - return Flux.fromStream(chatRoomMaps[shard].values().stream()); + return Flux.fromStream(chatrooms[shard].values().stream()); } } }