0070. Override Kafka Properties for Producer - dkkahm/study-kafka-with-spring GitHub Wiki

Configuration for ProducerFactory

@Configuration
public class KafkaConfig {

    @Autowired
    private KafkaProperties kafkaProperties;

    @Bean
    public ProducerFactory<Object, Object> producerFactory() {
        var properties = kafkaProperties.buildProducerProperties();

        properties.put(ProducerConfig.METADATA_MAX_AGE_CONFIG, "180000");

        return new DefaultKafkaProducerFactory<Object, Object>(properties);
    }
}

KafkaTemplate Bean까지 설정하는 경우는 Generic을 더 상세히 설정해야 함

  • <Object, Object> 사용하면 오류 발생
@Configuration
public class KafkaConfig {

    @Autowired
    private KafkaProperties kafkaProperties;

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        var properties = kafkaProperties.buildProducerProperties();

        properties.put(ProducerConfig.METADATA_MAX_AGE_CONFIG, "180000");

        return new DefaultKafkaProducerFactory<String, String>(properties);
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<String, String>(producerFactory());
    }
}