From: Kai Moritz Date: Fri, 4 Nov 2022 09:13:36 +0000 (+0100) Subject: WIP:TaskExecutor X-Git-Tag: simple-consumer--json-DEPRECATED~7 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=25a7ef246b07fe03070dd7922a585a34b485fef1;p=demos%2Fkafka%2Ftraining WIP:TaskExecutor --- diff --git a/src/main/java/de/juplo/kafka/Application.java b/src/main/java/de/juplo/kafka/Application.java index d4c0885..3157ef6 100644 --- a/src/main/java/de/juplo/kafka/Application.java +++ b/src/main/java/de/juplo/kafka/Application.java @@ -27,7 +27,7 @@ public class Application implements ApplicationRunner public void run(ApplicationArguments args) throws Exception { log.info("Starting SimpleConsumer"); - simpleConsumer.run(); + simpleConsumer.start(); } @PreDestroy diff --git a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java index de77c60..648bb9d 100644 --- a/src/main/java/de/juplo/kafka/ApplicationConfiguration.java +++ b/src/main/java/de/juplo/kafka/ApplicationConfiguration.java @@ -6,6 +6,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; import org.springframework.kafka.core.ConsumerFactory; @@ -15,12 +16,14 @@ public class ApplicationConfiguration { @Bean public SimpleConsumer endlessConsumer( + TaskExecutor taskExecutor, Consumer kafkaConsumer, KafkaProperties kafkaProperties, ApplicationProperties applicationProperties) { return new SimpleConsumer( + taskExecutor, kafkaProperties.getClientId(), applicationProperties.getTopic(), kafkaConsumer); diff --git a/src/main/java/de/juplo/kafka/SimpleConsumer.java b/src/main/java/de/juplo/kafka/SimpleConsumer.java index dac22a2..64d5176 100644 --- a/src/main/java/de/juplo/kafka/SimpleConsumer.java +++ b/src/main/java/de/juplo/kafka/SimpleConsumer.java @@ -6,7 +6,7 @@ import org.apache.kafka.clients.consumer.Consumer; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.common.errors.WakeupException; -import org.springframework.scheduling.annotation.Async; +import org.springframework.core.task.TaskExecutor; import java.time.Duration; import java.util.Arrays; @@ -14,8 +14,9 @@ import java.util.Arrays; @Slf4j @RequiredArgsConstructor -public class SimpleConsumer +public class SimpleConsumer implements Runnable { + private final TaskExecutor taskExecutor; private final String id; private final String topic; private final Consumer consumer; @@ -23,7 +24,12 @@ public class SimpleConsumer private long consumed = 0; - @Async + public void start() + { + taskExecutor.execute(this); + } + + @Override public void run() { try