Mitro
Mitro

Reputation: 1260

Correctly manage DLQ in Spring Cloud Stream Kafka

I want manage a DLQ in Spring Cloud Stream using kafka.

application.yaml

server:
    port: 8091
eureka:
    client:
        serviceUrl:
            defaultZone: http://IP:8761/eureka
spring:
    application:
        name: employee-consumer
    cloud:
        stream:
            kafka:
                binder:
                    brokers: IP:9092
                bindings:
                    greetings-in:
                        destination: greetings
                        contentType: application/json
                    greetings-out:
                        destination: greetings
                        contentType: application/json
            bindings:
                greetings-out:
                    consumer:
                        enableDlq: true
                        dlqName: dead-out
    kafka:
      consumer:
        group-id: A

As you can see in my configuration I enable dlq and set a name to the dlq topic.

To test DLQ behaviour I throw an exception on certain messages

My listener component

@StreamListener("greetings-out")
    public void handleGreetingsInput(@Payload Greetings greetings) throws Exception {
        logger.info("Greetings input -> {}", greetings);
        if (greetings.getMessage().equals("ciao")) {
            throw new Exception("eer");
        }
    }

In this way, the message that is equal to "ciao" throws an exception and in logs I see that it gets processed three times

2018-07-09 13:19:57.256  INFO 1 --- [container-0-C-1] com.mitro.service.GreetingsListener      : Greetings input -> com.mitro.model.Greetings@3da9d701[timestamp=0,message=ciao]
2018-07-09 13:19:58.259  INFO 1 --- [container-0-C-1] com.mitro.service.GreetingsListener      : Greetings input -> com.mitro.model.Greetings@5bd62aaf[timestamp=0,message=ciao]
2018-07-09 13:20:00.262  INFO 1 --- [container-0-C-1] com.mitro.service.GreetingsListener      : Greetings input -> com.mitro.model.Greetings@c26f92b[timestamp=0,message=ciao]
2018-07-09 13:20:00.266 ERROR 1 --- [container-0-C-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Exception thrown while invoking com.mitro.service.GreetingsListener#handleGreetingsInput[1 args]; nested exception is java.lang.Exception: eer, failedMessage=GenericMessage [payload=byte[32], headers={kafka_offset=3, scst_nativeHeadersPresent=true, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@510302cb, deliveryAttempt=3, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, contentType=application/json, kafka_receivedTopic=greetings-out, kafka_receivedTimestamp=1531142397248}]

This is fine for me, but I don't understand why a topic called dead-out is created as result (take a look at the image below). topics' view

What am I doing wrong?

EDIT 1: (still doesn't create topic for DLQ)

server:
    port: 8091
eureka:
    client:
        serviceUrl:
            defaultZone: http://IP:8761/eureka
spring:
    application:
        name: employee-consumer
    cloud:
        stream:
            kafka:
                streams:
                    binder:
                        serdeError: sendToDlq
                binder:
                    brokers: IP:9092
                    auto-create-topics: true
                bindings:
                    greetings-out:
                        destination: greetings-out
                        contentType: application/json
                        consumer:
                          enableDql: true
                          dlqName: dead-out
                          autoCommitOnError: true
                          autoCommitOffset: true
            bindings:
                greetings-out:
                    destination: greetings-out
                    contentType: application/json
                    consumer:
                        enableDlq: true
                        dlqName: dead-out
                        autoCommitOnError: true
                        autoCommitOffset: true
    kafka:
      consumer:
        group-id: A

enter image description here

Upvotes: 3

Views: 12476

Answers (1)

Gary Russell
Gary Russell

Reputation: 174759

Looks like your properties are reversed; the common properties - destination, contentType - must be under spring.cloud.stream.bindings. The kafka-specific properties (enableDlq, dlqName) must be under spring.clound.stream.kafka.bindings.

You have them reversed.

EDIT

There are two problems with your (modified) config.

  1. typo enableDql instead of enableDlq
  2. no group - you can't have a DLQ with an anonymous consumer:

Caused by: java.lang.IllegalArgumentException: DLQ support is not available for anonymous subscriptions

This works fine:

spring:
  application:
    name: employee-consumer
  cloud:
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          auto-create-topics: true
        bindings:
          input:
            consumer:
              enableDlq: true
              dlqName: dead-out
              autoCommitOnError: true
              autoCommitOffset: true
      bindings:
        input:
          group: so51247113
          destination: greetings-out
          contentType: application/json

and

@SpringBootApplication
@EnableBinding(Sink.class)
public class So51247113Application {

    public static void main(String[] args) {
        SpringApplication.run(So51247113Application.class, args);
    }

    @StreamListener(Sink.INPUT)
    public void in(String in) {
        System.out.println(in);
        throw new RuntimeException("fail");
    }

    @KafkaListener(id = "foo", topics = "dead-out")
    public void dlq(Message<?> in) {
        System.out.println("DLQ:" + in);
    }

}

Upvotes: 8

Related Questions