Stop consumer on demand
Sometimes, you may want to stop your consumer based on a given message or any other condition.
You can do it by calling the stopConsuming() method of the Junges\Kafka\Contracts\Consumer instance that is passed as the second argument of your message handler:
use Junges\Kafka\Contracts\Consumer; use Junges\Kafka\Contracts\ConsumerMessage; $consumer = \Junges\Kafka\Facades\Kafka::consumer(['topic']) ->withHandler(static function (ConsumerMessage $message, Consumer $consumer) { if ($someCondition) { $consumer->stopConsuming(); } }) ->build(); $consumer->consume();
The consumer finishes handling the current message before it stops, and the onStopConsuming callback is executed before it closes.
Sponsorship
Support Laravel Kafka by sponsoring me!
Laravel Kafka is free and Open Source software, built to empower developers like you. Your support helps maintain and enhance the project. If you find it valuable, please consider sponsoring me on GitHub. Every contribution makes a difference and keeps the development going strong! Thank you!
Become a Sponsor
Want to hide this message? Sponsor at any tier of $10/month or more!