Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Kafka: How to insert an application.yml topic in Producer Kafka

I have a spring-kafka microservice to which I recently added a dead letter to be able to send the various error messages

//some code..
@Component
public class KafkaProducer {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendDeadLetter(String message) {
        kafkaTemplate.send("myDeadLetter", message);
    }
}

I would like to call the topic kafka of the dead letter as "messageTopic" + "_deadLetter", my main topic being "messageTopic". In my Consumer the topic name gives him the application.yml as follows:

@KafkaListener(topics = "${spring.kafka.topic.name}")

How can I set the same kafka topic by possibly inserting the "+ deadLetter" from the application.yml? I tried such a thing:

@Component
@KafkaListener(topics = "${spring.kafka.topic.name}"+"_deadLetter")
public class KafkaProducer {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendDeadLetter(String message) {
        kafkaTemplate.send("messageTopic_deadLetter", message);
    }
}

but it creates me two different topics with the same name. I am waiting for some advice, thanks for the help!

like image 431
Numero 21 Avatar asked Nov 16 '25 08:11

Numero 21


2 Answers

Kafka Listener accepts constant for the Topic name, we can't modify the TOPIC name here.

Ideally good to go with separate methods (Kafka listeners) for actual topic and dead letter topic, define two different properties in YAML to hold two topic names.

@KafkaListener(topics = "${spring.kafka.topic.name}")
public void listen(......){

}

@KafkaListener(topics = "${spring.kafka.deadletter.topic.name}")
public void listenDlt(......){

}

enter image description here

To refer topic name inside send(...) from yml or property file

@Component
@KafkaListener(topics = "${spring.kafka.deadletter.topic.name}")
public class KafkaProducer {

    @Value("${spring.kafka.deadletter.topic.name}")
    private String DLT_TOPIC_NAME;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendDeadLetter(String message) {
        kafkaTemplate.send(DLT_TOPIC_NAME, message);
    }
}
like image 72
shasr Avatar answered Nov 19 '25 10:11

shasr


You can construct the topic name with SpEL:

@KafkaListener(topics = "#{'${spring.kafka.topic.name}' + '_deadLetter'"})

Note the single quotes around the property placeholder and literal.

like image 36
Gary Russell Avatar answered Nov 19 '25 11:11

Gary Russell