WIP:TaskExecutor
authorKai Moritz <kai@juplo.de>
Fri, 4 Nov 2022 09:13:36 +0000 (10:13 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 4 Nov 2022 11:52:13 +0000 (12:52 +0100)
src/main/java/de/juplo/kafka/Application.java
src/main/java/de/juplo/kafka/ApplicationConfiguration.java
src/main/java/de/juplo/kafka/SimpleConsumer.java

index d4c0885..3157ef6 100644 (file)
@@ -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
index de77c60..648bb9d 100644 (file)
@@ -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<String, String> kafkaConsumer,
       KafkaProperties kafkaProperties,
       ApplicationProperties applicationProperties)
   {
     return
         new SimpleConsumer(
+            taskExecutor,
             kafkaProperties.getClientId(),
             applicationProperties.getTopic(),
             kafkaConsumer);
index dac22a2..64d5176 100644 (file)
@@ -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<String, String> consumer;
@@ -23,7 +24,12 @@ public class SimpleConsumer
   private long consumed = 0;
 
 
-  @Async
+  public void start()
+  {
+    taskExecutor.execute(this);
+  }
+
+  @Override
   public void run()
   {
     try