I am having Kafka Listener which I want to enable or disable based on Value from DB. I am using @Conditional annotation in which I am trying to get value from the Database through CrudRepository. here is the code:
Listener class:
@Service
@Conditional(MyCondition.class)
public class TestKafkaListenerClass {
@KafkaListener(topicPattern = “(TestPattern)”, groupId = “TestGroupID”, containerFactory = “TestContainerFactory")
public void TestListener(String Event, Acknowledgment acknowledgment){
//proccesing event
}
MyCondition.class
public class MyCondition implements Condition{
@Autowired
TestRepository testRepository;
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
System.out.println("test repository value >>>"+testRepository);
// getting null value for testRepository
return testRepository.getValueFromDb();
}
}
with hardcoded values in the matches method, everything is working as expected but when trying to get value from testRepository it's throwing a null pointer exception
but the same testRepository is working fine in another place in the application.
how to resolve this, or is there any better way to achieve the same? Thanks!