I am trying to implement a PollableConsumer that starts polling messages from Kafka under a certain condition, in this case, when I hit an endpoint in my SpringBoot app.
I tried multiple ways of triggering the poller under a certain condition but apparently it only works if it is constantly polling from the kafka topic. (like all the examples in the spring-cloud-stream docs)
I am looking for something like this:
public interface CustomProcessor {
@Input
PollableMessageSource input();
}
public void run() {
boolean result = true;
while (result) {
result = input.poll(m -> {
Event event = (Event) m.getPayload();
GenericMessage<Event> genericMessage = new GenericMessage<>(event, m.getHeaders());
eventMessageConsumer.consume(genericMessage);
}, new ParameterizedTypeReference<Event>() {
});
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
if (result) {
System.out.println("Success");
}
}
}
That could be triggered when I hit a endpoint like this:
@GetMapping("/process")
public void process() {
SomeClass.run();
}