Skip to content

Instantly share code, notes, and snippets.

@joeljames
Last active April 20, 2020 21:16
Show Gist options
  • Save joeljames/89e8951ce5efcd44d684ef82ef56f760 to your computer and use it in GitHub Desktop.
Save joeljames/89e8951ce5efcd44d684ef82ef56f760 to your computer and use it in GitHub Desktop.
@Slf4j
public class KafkaProducerApp {
private static final String topic = "my-topic";
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
try (KafkaProducer<String, String> producer = new KafkaProducer(props)) {
int counter = 0;
while (counter <= 100) {
String msg = "Message " + counter;
//Use key if you want all the messages to go to a single partition
ProducerRecord<String, String> message1 = new ProducerRecord<>(topic, msg);
producer.send(message1);
counter++;
}
} catch (Exception e) {
log.error("Failed to send message by the producer", e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment