A KafkaListenerContainer is the runtime bridge between a Spring message-driven POJO and Kafka. Applying @KafkaListener causes ConcurrentKafkaListenerContainerFactory to create a listener container that wraps a KafkaConsumer subscribed to the declared topic or topics. section 12.2.1
Configure the container through listener annotations and externalized properties:
- Set topics with a property placeholder such as ${loan.app.input.topic}; the underlying consumer subscribes to the resolved topic. section 12.2.1
- Set groupId with ${application.group} to configure the underlying consumer group without recompiling when deployment properties change. section 12.2.1
- Place @KafkaListener on methods for direct handling, or place it on a class and route payloads through @KafkaHandler methods, including a default handler for unknown objects. section 12.2.1
- Use separate listeners and groups to process accepted, rejected, and quality-assurance loan topics independently. section 12.2.1
Listing 12.6 NewLoanApplicationProcessor declaration and constructor
@Component #1
public class NewLoanApplicationProcessor {
@Value("${accepted.loans.topic}") #2
private String acceptedLoansTopic;
@Value("${rejected.loans.topic}")
private String rejectedLoansTopic;
@Value("${qa.application.topic}")
private String qaLoansTopic;
private final KafkaTemplate<String, LoanApplication>
kafkaTemplate; #3
@Autowired #4
public NewLoanApplicationProcessor(
KafkaTemplate<String, LoanApplication> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
The declaration and constructor show the component that receives a KafkaTemplate; the listener method supplies the container’s topic and group configuration. section 12.2.1
