public class ChatRoomChannel implements Runnable
{
private final String topic;
- private final Producer<Integer, ChatRoomTo> producer;
- private final Consumer<Integer, ChatRoomTo> consumer;
+ private final Producer<Integer, CreateChatRoomRequestTo> producer;
+ private final Consumer<Integer, CreateChatRoomRequestTo> consumer;
private final ShardingStrategy shardingStrategy;
private final ChatMessageChannel chatMessageChannel;
private final Clock clock;
private boolean running;
- Mono<ChatRoomInfo> sendCreateChatRoomRequest(
- UUID chatRoomId,
- String name)
- {
- int shard = this.shardingStrategy.selectShard(chatRoomId);
- ChatRoomTo chatRoomTo = ChatRoomTo.of(chatRoomId, name, shard);
- return Mono.create(sink ->
- {
- ProducerRecord<Integer, ChatRoomTo> record =
- new ProducerRecord<>(
- topic,
- shard,
- chatRoomTo);
-
- producer.send(record, ((metadata, exception) ->
- {
- if (metadata != null)
- {
- log.info("Successfully send chreate-request for chat room: {}", chatRoomTo);
- sink.success(chatRoomTo.toChatRoomInfo());
- }
- else
- {
- // On send-failure
- log.error(
- "Could not send create-request for chat room (id={}, name={}): {}",
- chatRoomId,
- name,
- exception);
- sink.error(exception);
- }
- }));
- });
- }
-
@Override
public void run()
{
{
try
{
- ConsumerRecords<Integer, ChatRoomTo> records = consumer.poll(Duration.ofMinutes(5));
+ ConsumerRecords<Integer, CreateChatRoomRequestTo> records = consumer.poll(Duration.ofMinutes(5));
log.info("Fetched {} messages", records.count());
- for (ConsumerRecord<Integer, ChatRoomTo> record : records)
+ for (ConsumerRecord<Integer, CreateChatRoomRequestTo> record : records)
{
createChatRoom(record.value().toChatRoomInfo());
}