-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract kafka topic, key and timestamp from a received record and add…
… them to the headers to make them available in payload and header mappings, implemented AbstractConsumerActorTest for Kafka, extracted source supplier from KafkaConsumerActor for better testability Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch.io>
- Loading branch information
Showing
14 changed files
with
508 additions
and
152 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...clipse/ditto/connectivity/service/messaging/kafka/DefaultKafkaConsumerSourceSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
import akka.kafka.AutoSubscription; | ||
import akka.kafka.ConsumerSettings; | ||
import akka.kafka.Subscriptions; | ||
import akka.kafka.javadsl.Consumer; | ||
import akka.stream.javadsl.Source; | ||
|
||
/** | ||
* Default implementation of {@code KafkaConsumerSourceProvider}. | ||
*/ | ||
public class DefaultKafkaConsumerSourceSupplier implements KafkaConsumerSourceSupplier { | ||
|
||
final PropertiesFactory propertiesFactory; | ||
final String sourceAddress; | ||
final boolean dryRun; | ||
|
||
DefaultKafkaConsumerSourceSupplier( | ||
final PropertiesFactory propertiesFactory, final String sourceAddress, final boolean dryRun) { | ||
this.propertiesFactory = propertiesFactory; | ||
this.sourceAddress = sourceAddress; | ||
this.dryRun = dryRun; | ||
} | ||
|
||
@Override | ||
public Source<ConsumerRecord<String, String>, Consumer.Control> get() { | ||
final ConsumerSettings<String, String> consumerSettings = propertiesFactory.getConsumerSettings(dryRun); | ||
final AutoSubscription subscription = Subscriptions.topics(sourceAddress); | ||
return Consumer.plainSource(consumerSettings, subscription); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...a/org/eclipse/ditto/connectivity/service/messaging/kafka/KafkaConsumerSourceSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.util.function.Supplier; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
import akka.kafka.javadsl.Consumer; | ||
import akka.stream.javadsl.Source; | ||
|
||
/** | ||
* Supplier of a {@code Source<ConsumerRecord<String, String>, Consumer.Control>} used by {@code KafkaConsumerActor} | ||
* to consume messages from a kafka topic. | ||
*/ | ||
@FunctionalInterface | ||
public interface KafkaConsumerSourceSupplier | ||
extends Supplier<Source<ConsumerRecord<String, String>, Consumer.Control>> { | ||
} |
64 changes: 64 additions & 0 deletions
64
...ice/src/main/java/org/eclipse/ditto/connectivity/service/messaging/kafka/KafkaHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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 static java.util.Optional.ofNullable; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
/** | ||
* Defines headers that are extracted from a consumed {@code ConsumerRecord} and made available to payload and/or | ||
* header mappings. | ||
*/ | ||
enum KafkaHeader implements Function<ConsumerRecord<String, String>, Optional<String>> { | ||
|
||
/** | ||
* The topic the record is received from. | ||
*/ | ||
KAFKA_TOPIC("kafka.topic", ConsumerRecord::topic), | ||
/** | ||
* The timestamp of the received record. | ||
*/ | ||
KAFKA_TIMESTAMP("kafka.timestamp", record -> Long.toString(record.timestamp())), | ||
/** | ||
* The key of the received record (or null if not specified). | ||
*/ | ||
KAFKA_KEY("kafka.key", ConsumerRecord::key); | ||
|
||
private final String name; | ||
private final Function<ConsumerRecord<String, String>, String> extractor; | ||
|
||
/** | ||
* @param name the header name to be used in source header mappings | ||
*/ | ||
KafkaHeader(final String name, | ||
final Function<ConsumerRecord<String, String>, String> extractor) { | ||
this.name = name; | ||
this.extractor = extractor; | ||
} | ||
|
||
/** | ||
* @return the header name | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Optional<String> apply(final ConsumerRecord<String, String> consumerRecord) { | ||
return ofNullable(extractor.apply(consumerRecord)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.