You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/microservices/kafka.md
+102
Original file line number
Diff line number
Diff line change
@@ -433,6 +433,108 @@ throw new KafkaRetriableException('...');
433
433
434
434
> info **Hint**`KafkaRetriableException` class is exported from the `@nestjs/microservices` package.
435
435
436
+
### Custom exception handling
437
+
438
+
Along with the default error handling mechanisms, you can create a custom Exception Filter for Kafka events to manage retry logic. For instance, the example below demonstrates how to skip a problematic event after a configurable number of retries:
if (!topic||partition===undefined||offset===undefined) {
507
+
thrownewError(
508
+
'Incomplete Kafka message context for committing offset.',
509
+
);
510
+
}
511
+
512
+
awaitconsumer.commitOffsets([
513
+
{
514
+
topic,
515
+
partition,
516
+
// When committing an offset, commit the next number (i.e., current offset + 1)
517
+
offset: (Number(offset) +1).toString(),
518
+
},
519
+
]);
520
+
}
521
+
}
522
+
```
523
+
524
+
This filter offers a way to retry processing a Kafka event up to a configurable number of times. Once the maximum retries are reached, it triggers a custom `skipHandler` (if provided) and commits the offset, effectively skipping the problematic event. This allows subsequent events to be processed without interruption.
525
+
526
+
You can integrate this filter by adding it to your event handlers:
Committing offsets is essential when working with Kafka. Per default, messages will be automatically committed after a specific time. For more information visit [KafkaJS docs](https://kafka.js.org/docs/consuming#autocommit). `KafkaContext` offers a way to access the active consumer for manually committing offsets. The consumer is the KafkaJS consumer and works as the [native KafkaJS implementation](https://kafka.js.org/docs/consuming#manual-committing).
0 commit comments