20
20
import com .telkomdev .producer .serializer .ProductAvroSerializer ;
21
21
import com .telkomdev .producer .serializer .ProductJsonSerializer ;
22
22
import com .telkomdev .producer .serializer .ProductProtobufSerializer ;
23
- import org .apache .kafka .clients .producer .KafkaProducer ;
24
- import org .apache .kafka .clients .producer .Producer ;
25
- import org .apache .kafka .clients .producer .ProducerConfig ;
26
- import org .apache .kafka .clients .producer .ProducerRecord ;
27
23
28
24
import java .util .ArrayList ;
29
25
import java .util .List ;
30
- import java .util .Properties ;
31
26
import java .util .Scanner ;
32
27
33
28
public class App {
@@ -48,34 +43,27 @@ public static void main(String[] args) {
48
43
System .exit (0 );
49
44
}
50
45
51
- Properties producerConfig = new Properties ();
52
-
53
- producerConfig .put (ProducerConfig .CLIENT_ID_CONFIG , "client-1" );
54
- producerConfig .put (ProducerConfig .RETRIES_CONFIG , 0 );
55
- producerConfig .put (ProducerConfig .ACKS_CONFIG , "all" );
56
-
57
- // kafka brokers
58
- producerConfig .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , brokers );
59
- producerConfig .put (ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG , org .apache .kafka .common .serialization .ByteArraySerializer .class .getName ());
60
-
61
46
// send String data
62
- //producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, org.apache.kafka.common.serialization.StringSerializer.class.getName() );
47
+ //String valueSerializer = org.apache.kafka.common.serialization.StringSerializer.class.getName();
63
48
64
49
// send Protocol Buffer data
65
- producerConfig . put ( ProducerConfig . VALUE_SERIALIZER_CLASS_CONFIG , ProductProtobufSerializer .class .getName () );
50
+ String valueSerializer = ProductProtobufSerializer .class .getName ();
66
51
67
52
// send JSON data
68
- //producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ProductJsonSerializer.class.getName());
53
+ // String valueSerializer = ProductJsonSerializer.class.getName());
69
54
70
55
// send AVRO data
71
- //producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ProductAvroSerializer.class.getName());
56
+ // String valueSerializer = producerConfig.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ProductAvroSerializer.class.getName();
57
+
58
+ final MyKafkaProducer <String , Product > producer = new MyKafkaProducer ("client-1" ,
59
+ org .apache .kafka .common .serialization .StringSerializer .class .getName (),
60
+ valueSerializer , brokers );
72
61
73
- Producer producer = new KafkaProducer <String , String >(producerConfig );
74
62
75
63
// read input
76
64
Scanner in = new Scanner (System .in );
77
65
78
- System .out .println ("Type Message (type 'exit' to quit)" );
66
+ System .out .println ("Click Enter to send message (type 'exit' to quit)" );
79
67
String input = in .nextLine ();
80
68
81
69
while (!input .equals ("exit" )) {
@@ -91,12 +79,11 @@ public static void main(String[] args) {
91
79
images .add ("wuriyanto.com/img2" );
92
80
p .setImages (images );
93
81
94
- ProducerRecord <String , Product > record = new ProducerRecord <String , Product >(topic , p );
95
82
96
83
try {
97
84
System .out .println (input );
98
85
99
- producer .send (record );
86
+ producer .send (topic , p );
100
87
input = in .nextLine ();
101
88
} catch (Exception ex ) {
102
89
System .out .println ("error send data to kafka: " + ex .getMessage ());
@@ -105,6 +92,6 @@ public static void main(String[] args) {
105
92
}
106
93
107
94
in .close ();
108
- producer .close ();
95
+ producer .getProducer (). close ();
109
96
}
110
97
}
0 commit comments