* Die Logik für Start/Stop liegt jetzt wieder wie vor der Umstellung in
dem EndlessConsumer
* Der `ApplicationRunner` kenn den `EndlessConsumer` und ruft für diese
`start()` auf
* Entsprechende Aufrufe im `DriverController` wiederbelebt.
* Das Stoppen beim Herunterfahren der App wird wieder über eine
`@PreDestroy`-Methode im `EndlessConsumer` realisiert.
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
-
-import javax.annotation.PreDestroy;
public class Application implements ApplicationRunner
{
@Autowired
public class Application implements ApplicationRunner
{
@Autowired
- KafkaListenerEndpointRegistry registry;
- @Value("${consumer.client-id}")
- String clientId;
+ EndlessConsumer endlessConsumer;
@Override
public void run(ApplicationArguments args) throws Exception
{
log.info("Starting EndlessConsumer");
@Override
public void run(ApplicationArguments args) throws Exception
{
log.info("Starting EndlessConsumer");
- this.registry.getListenerContainer(clientId).start();
- }
-
- @PreDestroy
- public void stopExecutor()
- {
- log.info("Stopping EndlessConsumer");
- this.registry.getListenerContainer(clientId).stop();
+ endlessConsumer.start();
@RequiredArgsConstructor
public class DriverController
{
@RequiredArgsConstructor
public class DriverController
{
+ private final EndlessConsumer consumer;
+
+
@PostMapping("start")
public void start()
{
@PostMapping("start")
public void start()
{
- public void stop() throws ExecutionException, InterruptedException
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Component;
+import javax.annotation.PreDestroy;
import java.util.List;
import java.util.function.Consumer;
import java.util.List;
import java.util.function.Consumer;
@RequiredArgsConstructor
public class EndlessConsumer<K, V>
{
@RequiredArgsConstructor
public class EndlessConsumer<K, V>
{
+ @Autowired
+ private KafkaListenerEndpointRegistry registry;
@Value("${consumer.client-id}")
String id;
@Autowired
Consumer<ConsumerRecord<K, V>> handler;
@Value("${consumer.client-id}")
String id;
@Autowired
Consumer<ConsumerRecord<K, V>> handler;
+ private long consumed = 0;
@KafkaListener(
id = "${consumer.client-id}",
@KafkaListener(
id = "${consumer.client-id}",
);
handler.accept(record);
);
handler.accept(record);
+
+
+ public synchronized void start()
+ {
+ log.info("{} - Starting - consumed {} messages before", id, consumed);
+ registry.getListenerContainer(id).start();
+ }
+
+ public synchronized void stop()
+ {
+ log.info("{} - Stopping", id);
+ registry.getListenerContainer(id).stop();
+ log.info("{} - Stopped - consumed {} messages so far", id, consumed);
+ }
+
+ @PreDestroy
+ public void destroy()
+ {
+ log.info("{} - Destroy!", id);
+ stop();
+ }