Search code examples
spring-bootapache-kafkaspring-kafkaspring-el

@KafkaListener setting properties using SpEL expression


Is it possible to use SpEL to inject a list of properties into the @KafkaListner annotation?

Something like this:

@KafkaListener(topics = "topic-name",
            containerFactory = "myContainerFactory",
            groupId = "group-id",
            properties = "#{kafka.consumer.custom.properties}")
    public void onMessage(String msg) {
        log.info(msg);
    }

application.yml

kafka:
  consumer:
    custom:
      properties:
        key.deserializer: org.apache.kafka.common.serialization.IntegerDeserializer
        value.deserializer: org.apache.kafka.common.serialization.StringDeserializer

Solution

  • See https://github.com/spring-projects/spring-kafka/issues/1880#issuecomment-887677779

    some:
      prop: |
        max.poll.records: 10
        fetch.min.bytes: 123456
    
    @KafkaListener(id = "kgh1880", topics = "kgh1880",
                properties = "${some.prop}")