-
-
Save kasramp/243d404f96d2e264d87bd77eb089b850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.madadipouya.springkafkatest.kafka.producer; | |
import com.madadipouya.springkafkatest.dto.User; | |
import org.apache.kafka.clients.admin.NewTopic; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.core.annotation.Order; | |
import org.springframework.kafka.core.KafkaTemplate; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class UserKafkaProducer { | |
private final KafkaTemplate<String, User> kafkaTemplate; | |
@Value("${spring.kafka.topic.name}") | |
private String topic; | |
@Value("${spring.kafka.replication.factor:1}") | |
private int replicationFactor; | |
@Value("${spring.kafka.partition.number:1}") | |
private int partitionNumber; | |
public UserKafkaProducer(KafkaTemplate<String, User> kafkaTemplate) { | |
this.kafkaTemplate = kafkaTemplate; | |
} | |
public void writeToKafka(User user) { | |
kafkaTemplate.send(topic, user.getUuid(), user); | |
} | |
@Bean | |
@Order(-1) | |
public NewTopic createNewTopic() { | |
return new NewTopic(topic, partitionNumber, (short) replicationFactor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment