-
Notifications
You must be signed in to change notification settings - Fork 214
/
AtMostOnceKafkaConsumerSourceSupplier.java
51 lines (43 loc) · 1.94 KB
/
AtMostOnceKafkaConsumerSourceSupplier.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
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.messaging.kafka;
import java.nio.ByteBuffer;
import java.util.function.Supplier;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.pekko.kafka.AutoSubscription;
import org.apache.pekko.kafka.ConsumerSettings;
import org.apache.pekko.kafka.Subscriptions;
import org.apache.pekko.kafka.javadsl.Consumer;
import org.apache.pekko.stream.javadsl.Source;
/**
* Supplies a {@link Source} of {@link org.apache.kafka.clients.consumer.ConsumerRecord}s retaining a
* {@link org.apache.pekko.kafka.javadsl.Consumer.Control} in order to be able to shutdown/terminate Kafka consumption.
*/
class AtMostOnceKafkaConsumerSourceSupplier
implements Supplier<Source<ConsumerRecord<String, ByteBuffer>, Consumer.Control>> {
private final PropertiesFactory propertiesFactory;
private final String sourceAddress;
private final boolean dryRun;
AtMostOnceKafkaConsumerSourceSupplier(
final PropertiesFactory propertiesFactory, final String sourceAddress, final boolean dryRun) {
this.propertiesFactory = propertiesFactory;
this.sourceAddress = sourceAddress;
this.dryRun = dryRun;
}
@Override
public Source<ConsumerRecord<String, ByteBuffer>, Consumer.Control> get() {
final ConsumerSettings<String, ByteBuffer> consumerSettings = propertiesFactory.getConsumerSettings(dryRun);
final AutoSubscription subscription = Subscriptions.topics(sourceAddress);
return Consumer.plainSource(consumerSettings, subscription);
}
}