spring-kafkadead-letter

How to Implement beanprocessor to examine the attribute of meta listener?


My meta listener code

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@KafkaListener(containerFactory = "myListenerContainerFactory", autoStartup = "false")
public @interface mylistener1 {

    @AliasFor(annotation = KafkaListener.class, attribute = "topics")
    String[] topics();
    String myattr() default "";
}

Consume method:

@Service
Class ConsumerService(){

@mylistener1(topics = "new.topic",myattr="new.myatr.topic")
    public void consume(String message) {
        LOG.info("consumer-> " + message);
    }
}

I tried to get value from ApplicationContext, but it was not getting the listener.

@Autowired
ApplicationContext ctx;

Map<String, Object> allBeansWithNames = ctx.getBeansWithAnnotation(mylistener1.class);
allBeansWithNames - 0, and I am not getting class list which is having @mylistener1 annotation`your text`

I want to implement beanpostprocessor to check myattr at runtime and use it to send message`


Solution

  • getBeansWithAnnotation() will only find beans with classes that are so annotated (or @Bean factory methods). It doesn't look at the class methods.

    Take a look at KafkaListenerAnnotationBeanPostProcessor for an example of a BPP that examines annotations and their attributes.