How to run Kafka consumer after Openshift Pod started?

5/29/2020

How to start consuming messages from Kafka only after full start of application in OpenShift. I use Spring Boot 2.2.2, Spring Kafka 2.3.4 and OpenShift 4. There is an idea to create a REST controller that will launch Kafka on call.

@RestController("/kafka")
public class KafkaController {

    private final KafkaListenerEndpointRegistry endpointRegistry;

    @Autowired
    public KafkaController(KafkaListenerEndpointRegistry endpointRegistry) {
        this.endpointRegistry = endpointRegistry;
    }

    @GetMapping(value = "/start")
    public ResponseEntity<String> startupKafka() {
        try {
            endpointRegistry.getListenerContainer("listener").start();
            return new ResponseEntity<>("listener successfully started", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

But I don’t understand how to force OpenShift to call this endpoint after starting the pod.

-- Дмитрий Муравьев
apache-kafka
java
kubernetes
openshift

0 Answers