forked from bkimminich/apache-kafka-book-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleProducer.java
28 lines (23 loc) · 890 Bytes
/
SimpleProducer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package test.kafka;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import java.util.Properties;
public class SimpleProducer {
private static Producer<Integer, String> producer;
private final Properties properties = new Properties();
public SimpleProducer() {
properties.put("metadata.broker.list", "localhost:9092");
properties.put("serializer.class", "kafka.serializer.StringEncoder");
properties.put("request.required.acks", "1");
producer = new Producer<>(new ProducerConfig(properties));
}
public static void main(String[] args) {
new SimpleProducer();
String topic = args[0];
String msg = args[1];
KeyedMessage<Integer, String> data = new KeyedMessage<>(topic, msg);
producer.send(data);
producer.close();
}
}