KafkaProducerConfig refactor (#1488)

This commit is contained in:
Grzegorz Piwowarek 2017-03-25 10:37:00 +01:00 committed by GitHub
parent b6e271f1f0
commit efc2043e52
2 changed files with 10 additions and 9 deletions

View File

@ -144,6 +144,7 @@
<module>spring-jms</module> <module>spring-jms</module>
<module>spring-jooq</module> <module>spring-jooq</module>
<module>spring-jpa</module> <module>spring-jpa</module>
<module>spring-kafka</module>
<module>spring-katharsis</module> <module>spring-katharsis</module>
<module>spring-ldap</module> <module>spring-ldap</module>
<module>spring-mockito</module> <module>spring-mockito</module>

View File

@ -1,8 +1,5 @@
package com.baeldung.spring.kafka; package com.baeldung.spring.kafka;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer; import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -13,6 +10,9 @@ import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory; import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer; import org.springframework.kafka.support.serializer.JsonSerializer;
import java.util.HashMap;
import java.util.Map;
@Configuration @Configuration
public class KafkaProducerConfig { public class KafkaProducerConfig {
@ -21,30 +21,30 @@ public class KafkaProducerConfig {
@Bean @Bean
public ProducerFactory<String, String> producerFactory() { public ProducerFactory<String, String> producerFactory() {
Map<String, Object> configProps = new HashMap<String, Object>(); Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress); configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<String, String>(configProps); return new DefaultKafkaProducerFactory<>(configProps);
} }
@Bean @Bean
public KafkaTemplate<String, String> kafkaTemplate() { public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<String, String>(producerFactory()); return new KafkaTemplate<>(producerFactory());
} }
@Bean @Bean
public ProducerFactory<String, Greeting> greetingProducerFactory() { public ProducerFactory<String, Greeting> greetingProducerFactory() {
Map<String, Object> configProps = new HashMap<String, Object>(); Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress); configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return new DefaultKafkaProducerFactory<String, Greeting>(configProps); return new DefaultKafkaProducerFactory<>(configProps);
} }
@Bean @Bean
public KafkaTemplate<String, Greeting> greetingKafkaTemplate() { public KafkaTemplate<String, Greeting> greetingKafkaTemplate() {
return new KafkaTemplate<String, Greeting>(greetingProducerFactory()); return new KafkaTemplate<>(greetingProducerFactory());
} }
} }