import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executor;
-import java.util.concurrent.Future;
@SpringBootApplication
+@EnableAsync
@Slf4j
public class Application implements ApplicationRunner
{
- @Autowired
- Executor executor;
@Autowired
Consumer<?, ?> consumer;
@Autowired
@Override
public void run(ApplicationArguments args) throws Exception
+ {
+ start();
+ }
+
+ @Async
+ public void start()
{
log.info("Starting SimpleConsumer");
- executor.execute(simpleConsumer);
+ simpleConsumer.run();
}
@PreDestroy
- public void shutdown() throws ExecutionException, InterruptedException
+ public void stop() throws ExecutionException, InterruptedException
{
log.info("Signaling SimpleConsumer to quit its work");
consumer.wakeup();
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.ConsumerFactory;
-import org.springframework.scheduling.annotation.EnableAsync;
-
-import java.util.concurrent.Executor;
@Configuration
-@EnableAsync
@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
public class ApplicationConfiguration
{
@Slf4j
@RequiredArgsConstructor
-public class SimpleConsumer implements Runnable
+public class SimpleConsumer
{
private final String id;
private final String topic;
private long consumed = 0;
- @Override
public void run()
{
try