Search code examples
javaspringspring-bootspring-cloud-streamspring-cloud-stream-binder-kafka

How to tell if every consumer finished in Spring Cloud Stream Kafka?


I'm trying to black-box test a Spring Boot application which is using Spring Cloud Stream Kafka. The expected results (in the DB) may differ based on the message processing order. How could I reliably tell if one message was processed and I can send in the next? One important factor is that one message from the test can generate multiple events (messages) within the application.

I did the following methods:

  • wait fixed amount of time: usually works, but if someone's PC is hot and throttling, it can become flaky, and to be honest this is just ugly
  • create an aspect to count the method invocations, serve it through a controller, query it multiple times, send the next message when we're "settled": timing of querying matters, unreliable
  • periodically check Kafka consumer lag, either from code or by querying actuator, with multiple samples: this is mixture of the above two, sometimes slower than the first but more reliable

Is there any official way of doing this?


Solution

  • Configure the container to emit ListenerContainerIdleEvents.

    See https://docs.spring.io/spring-kafka/docs/current/reference/html/#idle-containers