* `InfoChannel` and `DataChannel` must not know each other directly.
* This is necessary, to prevent a cyclic dependency, that would otherwise
be introduced, if `InfoChannel` also has to communicate with
`DataChannel`.
--- /dev/null
+package de.juplo.kafka.chat.backend.implementation.kafka;
+
+import de.juplo.kafka.chat.backend.domain.ChatRoomInfo;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+import reactor.core.publisher.Mono;
+
+import java.util.UUID;
+
+
+@RequiredArgsConstructor
+public class ChannelMediator
+{
+ @Setter
+ private InfoChannel infoChannel;
+
+
+ void shardAssigned(int shard)
+ {
+ infoChannel.sendShardAssignedEvent(shard);
+ }
+
+ void shardRevoked(int shard)
+ {
+ infoChannel.sendShardRevokedEvent(shard);
+ }
+
+ Mono<ChatRoomInfo> getChatRoomInfo(UUID id)
+ {
+ return infoChannel.getChatRoomInfo(id);
+ }
+}
private final long[] currentOffset;
private final long[] nextOffset;
private final Map<UUID, ChatRoomData>[] chatRoomData;
- private final InfoChannel infoChannel;
+ private final ChannelMediator channelMediator;
private final ShardingPublisherStrategy shardingPublisherStrategy;
private boolean running;
Duration pollingInterval,
int bufferSize,
Clock clock,
- InfoChannel infoChannel,
+ ChannelMediator channelMediator,
ShardingPublisherStrategy shardingPublisherStrategy)
{
log.debug(
IntStream
.range(0, numShards)
.forEach(shard -> this.chatRoomData[shard] = new HashMap<>());
- this.infoChannel = infoChannel;
+ this.channelMediator = channelMediator;
this.shardingPublisherStrategy = shardingPublisherStrategy;
}
currentOffset);
consumer.seek(topicPartition, nextOffset[partition]);
- infoChannel.sendShardAssignedEvent(partition);
+ channelMediator.shardAssigned(partition);
shardingPublisherStrategy
.publishOwnership(partition)
.doOnSuccess(instanceId -> log.info(
isShardOwned[partition] = false;
nextOffset[partition] = consumer.position(topicPartition);
log.info("Partition revoked: {} - next={}", partition, nextOffset[partition]);
- infoChannel.sendShardRevokedEvent(partition);
+ channelMediator.shardRevoked(partition);
});
}
return Mono.error(new ShardNotOwnedException(instanceId, shard));
}
- return infoChannel
+ return channelMediator
.getChatRoomInfo(id)
.map(chatRoomInfo ->
chatRoomData[shard].computeIfAbsent(id, this::computeChatRoomData));
InfoChannel infoChannel(
ChatBackendProperties properties,
Producer<String, AbstractMessageTo> producer,
- Consumer<String, AbstractMessageTo> infoChannelConsumer)
+ Consumer<String, AbstractMessageTo> infoChannelConsumer,
+ ChannelMediator channelMediator)
{
- return new InfoChannel(
+ InfoChannel infoChannel = new InfoChannel(
properties.getKafka().getInfoChannelTopic(),
producer,
infoChannelConsumer,
properties.getKafka().getPollingInterval(),
properties.getKafka().getNumPartitions(),
properties.getKafka().getInstanceUri());
+ channelMediator.setInfoChannel(infoChannel);
+ return infoChannel;
}
@Bean
Consumer<String, AbstractMessageTo> dataChannelConsumer,
ZoneId zoneId,
Clock clock,
- InfoChannel infoChannel,
+ ChannelMediator channelMediator,
ShardingPublisherStrategy shardingPublisherStrategy)
{
return new DataChannel(
properties.getKafka().getPollingInterval(),
properties.getChatroomBufferSize(),
clock,
- infoChannel,
+ channelMediator,
shardingPublisherStrategy);
}
+ @Bean
+ ChannelMediator channelMediator()
+ {
+ return new ChannelMediator();
+ }
+
@Bean
Producer<String, AbstractMessageTo> producer(
Properties defaultProducerProperties,