Skip to content

Instantly share code, notes, and snippets.

@kasramp
Created October 3, 2020 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasramp/243d404f96d2e264d87bd77eb089b850 to your computer and use it in GitHub Desktop.
Save kasramp/243d404f96d2e264d87bd77eb089b850 to your computer and use it in GitHub Desktop.
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