I am writing the listener for kafka topic and using @kafkaListener annotation to do the same. Till now i hard-coded the topic name (topic1) and it was working fine.Here was a working code :-
@Component
public class KafkaConsumer {
@Autowired
private KafkaProperties kafkaProps;
@Autowired
@Qualifier("CustomObjectMapper")
private ObjectMapper objectMapper;
@KafkaListener(topics = "topic1", containerFactory = "createPokafkaListenerContainerFactory")
public void CreatePoListener(PurchaseOrder po, Acknowledgment ack)
throws JsonProcessingException {
LOG.info("Received message for po create from kafka topic {} is {}",
kafkaProps.getOmsCreateTopicName(), objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(po));
ack.acknowledge();
}
}
Now, when i am trying to change the code to get the topic name from code, it is not working. Code which i tried to get the value from code after following this page in stack-overflow (How to pass dynamic topic name to @kafkalistener(topics from environment variable) is :-
@Component
public class KafkaConsumer {
@Autowired
private KafkaProperties kafkaProps;
public KafkaProperties getKafkaProps() {
return kafkaProps;
}
@Autowired
@Qualifier("CustomObjectMapper")
private ObjectMapper objectMapper;
@KafkaListener(topics = "#{__listener.kafkaProps.getOmsCreateTopicName()}", containerFactory = "omsCreatePokafkaListenerContainerFactory")
public void CreatePoListener(PurchaseOrder po, Acknowledgment ack)
throws JsonProcessingException {
LOG.info("Received message for po create from kafka topic {} is {}",
kafkaProps.getOmsCreateTopicName(), objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(po));
ack.acknowledge();
}
}
When i am trying to bring the server, it is throwing the error :-
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field '__listener' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
can someone help me here what i am doing wrong here ?