1 package de.juplo.kafka.chat.backend.persistence.kafka;
3 import jakarta.annotation.PreDestroy;
4 import lombok.extern.slf4j.Slf4j;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.boot.ApplicationArguments;
7 import org.springframework.boot.ApplicationRunner;
8 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
9 import org.springframework.context.ConfigurableApplicationContext;
10 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
11 import org.springframework.stereotype.Component;
13 import java.util.concurrent.CompletableFuture;
16 @ConditionalOnProperty(
17 prefix = "chat.backend",
19 havingValue = "kafka")
22 public class KafkaServicesApplicationRunner implements ApplicationRunner
25 ThreadPoolTaskExecutor taskExecutor;
27 ConfigurableApplicationContext context;
30 ChatMessageChannel chatMessageChannel;
32 ChatRoomChannel chatRoomChannel;
34 CompletableFuture<Void> chatRoomChannelConsumerJob;
35 CompletableFuture<Void> chatMessageChannelConsumerJob;
39 public void run(ApplicationArguments args) throws Exception
41 log.info("Starting the consumer for the ChatRoomChannel");
42 chatRoomChannelConsumerJob = taskExecutor
43 .submitCompletable(chatRoomChannel)
46 log.error("The consumer for the ChatRoomChannel exited abnormally!", e);
49 log.info("Starting the consumer for the ChatMessageChannel");
50 chatMessageChannelConsumerJob = taskExecutor
51 .submitCompletable(chatMessageChannel)
54 log.error("The consumer for the ChatMessageChannel exited abnormally!", e);
60 public void joinChatRoomChannelConsumerJob()
62 log.info("Waiting for the consumer of the ChatRoomChannel to finish its work");
63 chatRoomChannelConsumerJob.join();
64 log.info("Joined the consumer of the ChatRoomChannel");
68 public void joinChatMessageChannelConsumerJob()
70 log.info("Waiting for the consumer of the ChatMessageChannel to finish its work");
71 chatMessageChannelConsumerJob.join();
72 log.info("Joined the consumer of the ChatMessageChannel");