-
Notifications
You must be signed in to change notification settings - Fork 84
/
KafkaProducerImpl.java
336 lines (287 loc) · 11.5 KB
/
KafkaProducerImpl.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright 2016 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vertx.kafka.client.producer.impl;
import io.vertx.core.*;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.VertxInternal;
import io.vertx.kafka.client.common.KafkaClientOptions;
import io.vertx.kafka.client.common.PartitionInfo;
import io.vertx.kafka.client.common.impl.CloseHandler;
import io.vertx.kafka.client.common.impl.Helper;
import io.vertx.kafka.client.producer.KafkaProducer;
import io.vertx.kafka.client.producer.KafkaProducerRecord;
import io.vertx.kafka.client.producer.KafkaWriteStream;
import io.vertx.kafka.client.producer.RecordMetadata;
import io.vertx.kafka.client.serialization.VertxSerdes;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.common.serialization.Serializer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Vert.x Kafka producer implementation
*/
public class KafkaProducerImpl<K, V> implements KafkaProducer<K, V> {
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Properties config) {
return createShared(
vertx,
name,
() -> new org.apache.kafka.clients.producer.KafkaProducer<>(config),
KafkaClientOptions.fromProperties(config, true));
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Map<String, String> config) {
Map<String, Object> copy = new HashMap<>(config);
return createShared(
vertx,
name,
() -> new org.apache.kafka.clients.producer.KafkaProducer<>(copy),
KafkaClientOptions.fromMap(copy, true));
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, KafkaClientOptions options) {
Map<String, Object> config = new HashMap<>();
if (options.getConfig() != null) {
config.putAll(options.getConfig());
}
return createShared(vertx, name, () -> new org.apache.kafka.clients.producer.KafkaProducer<>(config), options);
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Properties config, Class<K> keyType, Class<V> valueType) {
Serializer<K> keySerializer = VertxSerdes.serdeFrom(keyType).serializer();
Serializer<V> valueSerializer = VertxSerdes.serdeFrom(valueType).serializer();
return createShared(vertx, name, config, keySerializer, valueSerializer);
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Properties config, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
KafkaClientOptions options = KafkaClientOptions.fromProperties(config, true);
return createShared(
vertx,
name,
() -> new org.apache.kafka.clients.producer.KafkaProducer<>(config, keySerializer, valueSerializer),
options);
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Map<String, String> config, Class<K> keyType, Class<V> valueType) {
Serializer<K> keySerializer = VertxSerdes.serdeFrom(keyType).serializer();
Serializer<V> valueSerializer = VertxSerdes.serdeFrom(valueType).serializer();
return createShared(vertx, name, config, keySerializer, valueSerializer);
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Map<String, String> config, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
Map<String, Object> copy = new HashMap<>(config);
return createShared(
vertx,
name,
() -> new org.apache.kafka.clients.producer.KafkaProducer<>(copy, keySerializer, valueSerializer),
KafkaClientOptions.fromMap(copy, true));
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, KafkaClientOptions options, Class<K> keyType, Class<V> valueType) {
Serializer<K> keySerializer = VertxSerdes.serdeFrom(keyType).serializer();
Serializer<V> valueSerializer = VertxSerdes.serdeFrom(valueType).serializer();
return createShared(vertx, name, options, keySerializer, valueSerializer);
}
public static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, KafkaClientOptions options, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
Map<String, Object> config = new HashMap<>();
if (options.getConfig() != null) {
config.putAll(options.getConfig());
}
return createShared(vertx, name, () -> new org.apache.kafka.clients.producer.KafkaProducer<>(config, keySerializer, valueSerializer), options);
}
private static class SharedProducer<K, V> extends HashMap<Object, KafkaProducer<K, V>> {
final Producer<K, V> producer;
final CloseHandler closeHandler;
public SharedProducer(KafkaWriteStream<K, V> stream) {
this.producer = stream.unwrap();
this.closeHandler = new CloseHandler(stream::close);
}
}
private static final Map<String, SharedProducer<?, ?>> sharedProducers = new HashMap<>();
private static <K, V> KafkaProducer<K, V> createShared(Vertx vertx, String name, Supplier<Producer<K, V>> producerFactory, KafkaClientOptions options) {
synchronized (sharedProducers) {
@SuppressWarnings("unchecked") SharedProducer<K, V> sharedProducer = (SharedProducer<K, V>) sharedProducers.computeIfAbsent(name, key -> {
Producer<K, V> producer = producerFactory.get();
SharedProducer<K, V> s = new SharedProducer<>(KafkaWriteStream.create(vertx, producer, options));
s.closeHandler.registerCloseHook((VertxInternal) vertx);
return s;
});
Object key = new Object();
KafkaProducerImpl<K, V> producer = new KafkaProducerImpl<>(vertx, KafkaWriteStream.create(vertx, sharedProducer.producer), new CloseHandler((timeout, ar) -> {
synchronized (sharedProducers) {
sharedProducer.remove(key);
if (sharedProducer.isEmpty()) {
sharedProducers.remove(name);
sharedProducer.closeHandler.close(timeout, ar);
return;
}
}
ar.handle(Future.succeededFuture());
}));
sharedProducer.put(key, producer);
return producer.registerCloseHook();
}
}
private final Vertx vertx;
private final KafkaWriteStream<K, V> stream;
private final CloseHandler closeHandler;
public KafkaProducerImpl(Vertx vertx, KafkaWriteStream<K, V> stream, CloseHandler closeHandler) {
this.vertx = vertx;
this.stream = stream;
this.closeHandler = closeHandler;
}
public KafkaProducerImpl(Vertx vertx, KafkaWriteStream<K, V> stream) {
this(vertx, stream, new CloseHandler(stream::close));
}
public KafkaProducerImpl<K, V> registerCloseHook() {
Context context = Vertx.currentContext();
if (context == null) {
return this;
}
closeHandler.registerCloseHook((ContextInternal) context);
return this;
}
@Override
public KafkaProducer<K, V> initTransactions(Handler<AsyncResult<Void>> handler) {
this.stream.initTransactions(handler);
return this;
}
@Override
public Future<Void> initTransactions() {
return this.stream.initTransactions();
}
@Override
public KafkaProducer<K, V> beginTransaction(Handler<AsyncResult<Void>> handler) {
this.stream.beginTransaction(handler);
return this;
}
@Override
public Future<Void> beginTransaction() {
return this.stream.beginTransaction();
}
@Override
public KafkaProducer<K, V> commitTransaction(Handler<AsyncResult<Void>> handler) {
this.stream.commitTransaction(handler);
return this;
}
@Override
public Future<Void> commitTransaction() {
return this.stream.commitTransaction();
}
@Override
public KafkaProducer<K, V> abortTransaction(Handler<AsyncResult<Void>> handler) {
this.stream.abortTransaction(handler);
return this;
}
@Override
public Future<Void> abortTransaction() {
return this.stream.abortTransaction();
}
@Override
public KafkaProducer<K, V> exceptionHandler(Handler<Throwable> handler) {
this.stream.exceptionHandler(handler);
return this;
}
@Override
public Future<Void> write(KafkaProducerRecord<K, V> kafkaProducerRecord) {
return this.stream.write(kafkaProducerRecord.record());
}
@Override
public void write(KafkaProducerRecord<K, V> record, Handler<AsyncResult<Void>> handler) {
this.stream.write(record.record(), handler);
}
@Override
public Future<RecordMetadata> send(KafkaProducerRecord<K, V> record) {
return this.stream.send(record.record()).map(Helper::from);
}
@Override
public KafkaProducer<K, V> send(KafkaProducerRecord<K, V> record, Handler<AsyncResult<RecordMetadata>> handler) {
this.send(record).onComplete(handler);
return this;
}
@Override
public Future<List<PartitionInfo>> partitionsFor(String topic) {
return this.stream.partitionsFor(topic).map(list ->
list.stream().map(kafkaPartitionInfo ->
new PartitionInfo()
.setInSyncReplicas(
Stream.of(kafkaPartitionInfo.inSyncReplicas()).map(Helper::from).collect(Collectors.toList()))
.setLeader(Helper.from(kafkaPartitionInfo.leader()))
.setPartition(kafkaPartitionInfo.partition())
.setReplicas(
Stream.of(kafkaPartitionInfo.replicas()).map(Helper::from).collect(Collectors.toList()))
.setTopic(kafkaPartitionInfo.topic())
).collect(Collectors.toList())
);
}
@Override
public KafkaProducer<K, V> partitionsFor(String topic, Handler<AsyncResult<List<PartitionInfo>>> handler) {
partitionsFor(topic).onComplete(handler);
return this;
}
@Override
public void end(Handler<AsyncResult<Void>> handler) {
this.stream.end(handler);
}
@Override
public KafkaProducer<K, V> setWriteQueueMaxSize(int size) {
this.stream.setWriteQueueMaxSize(size);
return this;
}
@Override
public boolean writeQueueFull() {
return this.stream.writeQueueFull();
}
@Override
public KafkaProducer<K, V> drainHandler(Handler<Void> handler) {
this.stream.drainHandler(handler);
return this;
}
@Override
public KafkaProducer<K, V> flush(Handler<AsyncResult<Void>> completionHandler) {
this.stream.flush(completionHandler);
return this;
}
@Override
public Future<Void> flush() {
return this.stream.flush();
}
@Override
public Future<Void> close(long timeout) {
Promise<Void> promise = Promise.promise();
closeHandler.close(timeout, promise);
return promise.future();
}
@Override
public Future<Void> close() {
Promise<Void> promise = Promise.promise();
closeHandler.close(promise);
return promise.future();
}
@Override
public void close(Handler<AsyncResult<Void>> completionHandler) {
closeHandler.close(completionHandler);
}
@Override
public void close(long timeout, Handler<AsyncResult<Void>> completionHandler) {
closeHandler.close(timeout, completionHandler);
}
@Override
public KafkaWriteStream<K, V> asStream() {
return this.stream;
}
@Override
public Producer<K, V> unwrap() {
return this.stream.unwrap();
}
}