com.github.frtu.kafka.config.producer.ProducerConfiguration Maven / Gradle / Ivy
package com.github.frtu.kafka.config.producer;
import com.github.frtu.kafka.config.commons.BaseKafkaConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import javax.annotation.PreDestroy;
/**
* Configuration for Spring Kafka producer.
*
* @param Type of the key of the published message
* @param Type of the value of the published message
* @author fred
* @since 0.3.7
*/
@Configuration
public class ProducerConfiguration extends BaseKafkaConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(ProducerConfiguration.class);
private DefaultKafkaProducerFactory defaultKafkaProducerFactory;
@Bean
public ProducerFactory producerFactory() {
this.defaultKafkaProducerFactory = new DefaultKafkaProducerFactory(baseKafkaConfigs());
return this.defaultKafkaProducerFactory;
}
@Bean
public KafkaTemplate kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@PreDestroy
public void close() {
try {
this.defaultKafkaProducerFactory.destroy();
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}