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());
    }
}