-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80898ff
commit 4e8fc38
Showing
2 changed files
with
28 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Stop consumer on demand | ||
weight: 6 | ||
--- | ||
|
||
Sometimes, you may want to stop your consumer based on a given message or any other condition. | ||
|
||
You can do it by adding a calling `stopConsuming()` method on the `MessageConsumer` instance that is passed as the | ||
second argument of your message handler: | ||
|
||
```php | ||
$consumer = \Junges\Kafka\Facades\Kafka::consumer(['topic']) | ||
->withConsumerGroupId('group') | ||
->stopAfterLastMessage() | ||
->withHandler(static function (\Junges\Kafka\Contracts\ConsumerMessage $message, \Junges\Kafka\Contracts\MessageConsumer $consumer) { | ||
if ($someCondition) { | ||
$consumer->stopConsuming(); | ||
} | ||
}) | ||
->build(); | ||
|
||
$consumer->consume(); | ||
``` | ||
|
||
The `onStopConsuming` callback will be executed before stopping your consumer. |