File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed
java/com/madadipouya/springkafkatest/producer Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change 11package com .madadipouya .springkafkatest .producer ;
22
33import com .madadipouya .springkafkatest .dto .User ;
4+ import org .apache .kafka .clients .admin .NewTopic ;
45import org .springframework .beans .factory .annotation .Value ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .core .annotation .Order ;
58import org .springframework .kafka .core .KafkaTemplate ;
69import org .springframework .stereotype .Component ;
710
@@ -13,11 +16,23 @@ public class UserKafkaProducer {
1316 @ Value ("${spring.kafka.topic.name}" )
1417 private String topic ;
1518
19+ @ Value ("${spring.kafka.replication.factor:1}" )
20+ private int replicationFactor ;
21+
22+ @ Value ("${spring.kafka.partition.number:1}" )
23+ private int partitionNumber ;
24+
1625 public UserKafkaProducer (KafkaTemplate <String , User > kafkaTemplate ) {
1726 this .kafkaTemplate = kafkaTemplate ;
1827 }
1928
2029 public void writeToKafka (User user ) {
2130 kafkaTemplate .send (topic , user .getUuid (), user );
2231 }
32+
33+ @ Bean
34+ @ Order (-1 )
35+ public NewTopic createNewTopic () {
36+ return new NewTopic (topic , partitionNumber , (short ) replicationFactor );
37+ }
2338}
Original file line number Diff line number Diff line change 11spring.kafka.bootstrap-servers =localhost:9092
22spring.kafka.topic.name =com.madadipouya.kafka.user
3+ spring.kafka.replication.factor =3
4+ spring.kafka.partition.number =2
35spring.kafka.consumer.bootstrap-servers =localhost:9092
46spring.kafka.consumer.group-id =kafka-user-listener
57spring.kafka.consumer.auto-offset-reset =earliest
You can’t perform that action at this time.
0 commit comments