import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
@SpringBootApplication
-@EnableAsync
@Slf4j
public class Application implements ApplicationRunner
{
+ @Autowired
+ ThreadPoolTaskExecutor taskExecutor;
@Autowired
Consumer<?, ?> kafkaConsumer;
@Autowired
SimpleConsumer simpleConsumer;
+ Future<?> consumerJob;
+
@Override
public void run(ApplicationArguments args) throws Exception
{
log.info("Starting SimpleConsumer");
- simpleConsumer.run();
+ consumerJob = taskExecutor.submit(simpleConsumer);
}
@PreDestroy
- public void stop() throws ExecutionException, InterruptedException
+ public void shutdown() throws ExecutionException, InterruptedException
{
log.info("Signaling SimpleConsumer to quit its work");
kafkaConsumer.wakeup();
-
- while (simpleConsumer.isRunning())
- {
- log.info("Waiting for SimpleConsumer to finish its work");
- try
- {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {}
- }
+ log.info("Waiting for SimpleConsumer to finish its work");
+ consumerJob.get();
log.info("SimpleConsumer finished its work");
}