0

I am implementing a use case where I listen to messages coming from a Kafka topic in a quarkus application in a reactive manner. I followed as base code the KafkaDeadLetterTopic class from this Gist included on this blogpost https://quarkus.io/blog/kafka-failure-strategy/

When I alter the code to be reactive and return an Uni like this

    @Incoming("movies")
    public Uni<Void> consume1(IncomingKafkaRecord<String, String> movie) {
        return Uni.createFrom().item(movie)
            .onItem()
            .transform(item -> {
                if (movie.getPayload().contains(",")) {
                    throw new IllegalArgumentException(
                        "I don't like movie with , in their title: " + movie);
                }

                return item;
            })
            .onItem().invoke(() -> movie.ack())
            .onFailure().invoke(throwable -> movie.nack(throwable))
            .onItem()
            .ignore().andContinueWithNull();
    }

The messages are still being sent to configured Dead Letter Queue, but the health check is marked as unhealthy, making the application to be restarted by the container orchestrator.

enter image description here

Is this a bug? Am I using incorrectly the Uni on this case? Is the use of case of sending to DLQ from a reactive code supported?

jesantana
  • 1,241
  • 2
  • 14
  • 27
  • 1
    `.onFailure().invoke(throwable -> movie.nack(throwable))` does not handle the failure. It's still propagated. You need `.onFailure().recoverWithX` to handle the failure and continue the processing. – Clement Jan 31 '23 at 07:53

1 Answers1

0

I tried what Clement suggest and it works nicely. It seems that I wasn't correctly handling error recovery on Mutiny. This is my final code

        @Incoming("movies")
        public Uni<Void> consume1(IncomingKafkaRecord<String, String> movie) {
            return Uni.createFrom().item(movie)
                .onItem()
                .transform(item -> {
                    if (movie.getPayload().contains(",")) {
                        throw new IllegalArgumentException(
                            "I don't like movie with , in their title: " + movie);
                    }

                    return item;
                })
                .onItem().invoke(() -> movie.ack())
                .onItem().ignore().andContinueWithNull()
                .onFailure().recoverWithUni(throwable -> Uni.createFrom().completionStage(movie.nack(throwable)));
        }
jesantana
  • 1,241
  • 2
  • 14
  • 27