package de.juplo.kafka;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
+
+import javax.annotation.PreDestroy;
@SpringBootApplication
@Slf4j
-public class Application
+public class Application implements ApplicationRunner
{
+ @Autowired
+ KafkaListenerEndpointRegistry registry;
+ @Value("${consumer.client-id}")
+ String clientId;
+
+
+ @Override
+ public void run(ApplicationArguments args) throws Exception
+ {
+ log.info("Starting EndlessConsumer");
+ this.registry.getListenerContainer(clientId).start();
+ }
+
+ @PreDestroy
+ public void stopExecutor()
+ {
+ log.info("Stopping EndlessConsumer");
+ this.registry.getListenerContainer(clientId).stop();
+ }
+
+
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
Consumer<ConsumerRecord<K, V>> handler;
- @KafkaListener(topics = "${consumer.topic}", containerFactory = "batchFactory")
+ @KafkaListener(
+ id = "${consumer.client-id}",
+ idIsGroup = false,
+ topics = "${consumer.topic}",
+ containerFactory = "batchFactory",
+ autoStartup = "false")
public void receive(List<ConsumerRecord<K, V>> records)
{
// Do something with the data...