import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executor;
import java.util.concurrent.Future;
public class Application implements ApplicationRunner
{
@Autowired
- ExecutorService executorService;
+ Executor executor;
@Autowired
Consumer<?, ?> consumer;
@Autowired
SimpleConsumer simpleConsumer;
- Future<?> consumerJob;
-
@Override
public void run(ApplicationArguments args) throws Exception
{
log.info("Starting SimpleConsumer");
- consumerJob = executorService.submit(simpleConsumer);
+ executor.execute(simpleConsumer);
}
@PreDestroy
{
log.info("Signaling SimpleConsumer to quit its work");
consumer.wakeup();
- log.info("Waiting for SimpleConsumer to finish its work");
- consumerJob.get();
- log.info("SimpleConsumer finished its work");
}
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.ConsumerFactory;
+import org.springframework.scheduling.annotation.EnableAsync;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.concurrent.Executor;
@Configuration
+@EnableAsync
@EnableConfigurationProperties({ KafkaProperties.class, ApplicationProperties.class })
public class ApplicationConfiguration
{
@Bean
public SimpleConsumer endlessConsumer(
Consumer<String, String> kafkaConsumer,
- ExecutorService executor,
KafkaProperties kafkaProperties,
ApplicationProperties applicationProperties)
{
return
new SimpleConsumer(
- executor,
kafkaProperties.getClientId(),
applicationProperties.getTopic(),
kafkaConsumer);
}
- @Bean
- public ExecutorService executor()
- {
- return Executors.newSingleThreadExecutor();
- }
-
@Bean(destroyMethod = "close")
public Consumer<?, ?> kafkaConsumer(ConsumerFactory<?, ?> factory)
{
import java.time.Duration;
import java.util.Arrays;
-import java.util.concurrent.ExecutorService;
@Slf4j
@RequiredArgsConstructor
public class SimpleConsumer implements Runnable
{
- private final ExecutorService executor;
private final String id;
private final String topic;
private final Consumer<String, String> consumer;