import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-import org.springframework.kafka.core.ConsumerFactory;
import javax.annotation.PreDestroy;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Future;
@SpringBootApplication
public class Application implements ApplicationRunner
{
@Autowired
- Consumer<String, Message> consumer;
+ Executor executor;
+ @Autowired
+ Consumer<?, ?> kafkaConsumer;
@Autowired
SimpleConsumer simpleConsumer;
-
@Override
public void run(ApplicationArguments args) throws Exception
{
- log.info("Starting EndlessConsumer");
- simpleConsumer.start();
+ log.info("Starting SimpleConsumer");
+ executor.execute(simpleConsumer);
}
@PreDestroy
- public void shutdown()
- {
- log.info("Signaling the consumer to quit its work");
- consumer.wakeup();
- }
-
- @Bean(destroyMethod = "close")
- public Consumer<String, Message> kafkaConsumer(ConsumerFactory<String, Message> factory)
+ public void shutdown() throws ExecutionException, InterruptedException
{
- return factory.createConsumer();
+ log.info("Signaling SimpleConsumer to quit its work");
+ kafkaConsumer.wakeup();
}