-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProducerAsync.java
25 lines (25 loc) · 1.04 KB
/
ProducerAsync.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
package arjun.kafka.lab;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class ProducerAsync
{
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>("producer", "String Key", "Hello! Async Send");
try {
producer.send(record, (recordMetadata, e) -> {
if (e != null)
e.printStackTrace();
});
} catch (Exception e) {
e.printStackTrace();
}
producer.close();
}
}