1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.Consumer;
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.SpringApplication;
9 import org.springframework.boot.autoconfigure.SpringBootApplication;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.kafka.core.ConsumerFactory;
13 import javax.annotation.PreDestroy;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Future;
19 @SpringBootApplication
21 public class Application implements ApplicationRunner
24 ExecutorService executorService;
26 Consumer<String, Message> consumer;
28 SimpleConsumer simpleConsumer;
30 Future<?> consumerJob;
33 public void run(ApplicationArguments args) throws Exception
35 log.info("Starting SimpleConsumer");
36 consumerJob = executorService.submit(simpleConsumer);
40 public void shutdown() throws ExecutionException, InterruptedException
42 log.info("Signaling SimpleConsumer to quit its work");
44 log.info("Waiting for SimpleConsumer to finish its work");
46 log.info("SimpleConsumer finished its work");
49 @Bean(destroyMethod = "close")
50 public Consumer<String, Message> kafkaConsumer(ConsumerFactory<String, Message> factory)
52 return factory.createConsumer();
56 public static void main(String[] args)
58 SpringApplication.run(Application.class, args);