import reactor.core.publisher.Mono;
import java.time.*;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
+import java.util.*;
+import java.util.concurrent.Callable;
import java.util.stream.IntStream;
@Slf4j
-public class ChatMessageChannel implements Runnable, ConsumerRebalanceListener
+public class ChatMessageChannel implements Callable<Optional<Exception>>, ConsumerRebalanceListener
{
private final String topic;
private final Producer<String, MessageTo> producer;
}
@Override
- public void run()
+ public Optional<Exception> call()
{
consumer.subscribe(List.of(topic));
log.info("Received WakeupException, exiting!");
running = false;
}
+ catch (Exception e)
+ {
+ log.error("Exiting abnormally!");
+ return Optional.of(e);
+ }
}
+
+ log.info("Exiting normally");
+ return Optional.empty();
}
void loadMessages(ConsumerRecords<String, MessageTo> records)
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
-import org.apache.kafka.common.errors.RecordDeserializationException;
import org.apache.kafka.common.errors.WakeupException;
import reactor.core.publisher.Mono;
import java.time.*;
import java.util.List;
+import java.util.Optional;
import java.util.UUID;
+import java.util.concurrent.Callable;
@RequiredArgsConstructor
@Slf4j
-public class ChatRoomChannel implements Runnable
+public class ChatRoomChannel implements Callable<Optional<Exception>>
{
private final String topic;
private final Producer<Integer, ChatRoomTo> producer;
}
@Override
- public void run()
+ public Optional<Exception> call()
{
consumer.assign(List.of(new TopicPartition(topic, 0)));
log.info("Received WakeupException, exiting!");
running = false;
}
+ catch (Exception e)
+ {
+ log.error("Exiting abnormally!");
+ return Optional.of(e);
+ }
}
+
+ log.info("Exiting normally");
+ return Optional.empty();
}
+
void createChatRoom(ChatRoomInfo chatRoomInfo)
{
UUID id = chatRoomInfo.getId();
import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatHomeService;
import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomFactory;
+import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.producer.Producer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.util.concurrent.ListenableFuture;
import java.time.Clock;
import java.time.ZoneId;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
@ConditionalOnProperty(
name = "services",
havingValue = "kafka")
@Configuration
+@Slf4j
public class KafkaServicesConfiguration implements ApplicationRunner
{
+ @Autowired
+ ThreadPoolTaskExecutor taskExecutor;
+ @Autowired
+ ConfigurableApplicationContext context;
+
+ @Autowired
+ ChatMessageChannel chatMessageChannel;
+
+ CompletableFuture<Optional<Exception>> chatRoomChannelConsumerJob;
+ CompletableFuture<Optional<Exception>> chatMessageChannelConsumerJob;
+
+
+ @Override
+ public void run(ApplicationArguments args) throws Exception
+ {
+ log.info("Starting the consumer for the ChatRoomChannel");
+ chatRoomChannelConsumerJob = taskExecutor.submitCompletable(chatMessageChannel);
+ chatRoomChannelConsumerJob.thenAccept(exceptionOptional ->
+ {
+ exceptionOptional.ifPresent();
+ log.info("SimpleConsumer exited normally, exit-status: {}", exitStatus);
+ SpringApplication.exit(context, () -> exitStatus);
+ },
+ t ->
+ {
+ log.error("SimpleConsumer exited abnormally!", t);
+ SpringApplication.exit(context, () -> 2);
+ });
+ }
+
+ @PreDestroy
+ public void shutdown() throws ExecutionException, InterruptedException
+ {
+ log.info("Signaling SimpleConsumer to quit its work");
+ kafkaConsumer.wakeup();
+ log.info("Waiting for SimpleConsumer to finish its work");
+ consumerJob.get();
+ log.info("SimpleConsumer finished its work");
+ }
+
+
@Bean
ChatHome kafkaChatHome(
ShardingStrategy shardingStrategy,