-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kafka-consume command (without avro support yet) (#27)
support plain kafka consumption (without avro support yet)
- Loading branch information
1 parent
596f060
commit 7acc07c
Showing
24 changed files
with
570 additions
and
49 deletions.
There are no files selected for viewing
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
Binary file not shown.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Sat Jan 21 03:35:37 CET 2017 | ||
#Mon Jul 31 14:32:43 CEST 2017 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.2-bin.zip |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1 @@ | ||
/* | ||
* This settings file was auto generated by the Gradle buildInit task | ||
* by 'rafal.kluszczynski' at '12/9/16 12:57 AM' with Gradle 3.2.1 | ||
* | ||
* The settings file is used to specify which projects to include in your build. | ||
* In a single project build this file can be empty or even removed. | ||
* | ||
* Detailed information about configuring a multi-project build in Gradle can be found | ||
* in the user guide at https://docs.gradle.org/3.2.1/userguide/multi_project_builds.html | ||
*/ | ||
|
||
/* | ||
// To declare projects as part of a multi-project build use the 'include' method | ||
include 'shared' | ||
include 'api' | ||
include 'services:webservice' | ||
*/ | ||
|
||
rootProject.name = 'avro-cli' |
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
85 changes: 85 additions & 0 deletions
85
src/main/java/io/github/rkluszczynski/avro/cli/command/kafka/ConsumeParameters.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,85 @@ | ||
package io.github.rkluszczynski.avro.cli.command.kafka; | ||
|
||
import avro.shaded.com.google.common.collect.Lists; | ||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.beust.jcommander.converters.EnumConverter; | ||
import io.github.rkluszczynski.avro.cli.command.CliCommandParameters; | ||
import io.github.rkluszczynski.avro.cli.util.DurationGuessConverter; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import static io.github.rkluszczynski.avro.cli.command.kafka.MessageTypeParameter.TEXT; | ||
import static io.github.rkluszczynski.avro.cli.command.kafka.OffsetResetParameter.LATEST; | ||
|
||
@Parameters( | ||
commandDescription = "Consume records from Kafka." | ||
) | ||
class ConsumeParameters extends CliCommandParameters { | ||
@Parameter( | ||
names = {"--bootstrap-servers", "-b"}, | ||
description = "Bootstrap servers." | ||
) | ||
private String bootstrapServers = "localhost:9092"; | ||
|
||
@Parameter( | ||
names = {"--topic", "-t"}, | ||
description = "Kafka topic name.", | ||
required = true | ||
) | ||
private List<String> topics = Lists.newArrayList(); | ||
|
||
@Parameter( | ||
names = {"--message-type", "-m"}, | ||
converter = MessageTypeParameterConverter.class, | ||
description = "Topic message type." | ||
) | ||
private MessageTypeParameter messageType = TEXT; | ||
|
||
@Parameter( | ||
names = {"--offset-reset", "-o"}, | ||
converter = OffsetResetParameterConverter.class, | ||
description = "Offset reset consumer value." | ||
) | ||
private OffsetResetParameter offsetReset = LATEST; | ||
|
||
@Parameter( | ||
names = {"--duration"}, | ||
converter = DurationGuessConverter.class, | ||
description = "Read duration in ISO-8601 format (PnDTnHnMn.nS)." | ||
) | ||
private Duration duration; | ||
|
||
public String getBootstrapServers() { | ||
return bootstrapServers; | ||
} | ||
|
||
public List<String> getTopics() { | ||
return topics; | ||
} | ||
|
||
public Duration getDuration() { | ||
return duration; | ||
} | ||
|
||
public MessageTypeParameter getMessageType() { | ||
return messageType; | ||
} | ||
|
||
public OffsetResetParameter getOffsetReset() { | ||
return offsetReset; | ||
} | ||
|
||
private static class MessageTypeParameterConverter extends EnumConverter<MessageTypeParameter> { | ||
private MessageTypeParameterConverter(String optionName, Class<MessageTypeParameter> clazz) { | ||
super(optionName, clazz); | ||
} | ||
} | ||
|
||
private static class OffsetResetParameterConverter extends EnumConverter<OffsetResetParameter> { | ||
private OffsetResetParameterConverter(String optionName, Class<OffsetResetParameter> clazz) { | ||
super(optionName, clazz); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/io/github/rkluszczynski/avro/cli/command/kafka/ExtendedMessageListener.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,11 @@ | ||
package io.github.rkluszczynski.avro.cli.command.kafka; | ||
|
||
import org.springframework.kafka.listener.MessageListener; | ||
|
||
public abstract class ExtendedMessageListener<K, V> implements MessageListener<K, V> { | ||
private volatile long count = 0L; | ||
|
||
protected long incrementAndGet() { | ||
return ++count; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/rkluszczynski/avro/cli/command/kafka/KafkaConsumption.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,65 @@ | ||
package io.github.rkluszczynski.avro.cli.command.kafka; | ||
|
||
import io.github.rkluszczynski.avro.cli.CliMainParameters; | ||
import io.github.rkluszczynski.avro.cli.CommandException; | ||
import io.github.rkluszczynski.avro.cli.command.CliCommand; | ||
import io.github.rkluszczynski.avro.cli.command.CliCommandParameters; | ||
import org.springframework.kafka.listener.KafkaMessageListenerContainer; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static io.github.rkluszczynski.avro.cli.command.CommandNames.KAFKA_CONSUME; | ||
import static io.github.rkluszczynski.avro.cli.command.kafka.KafkaMessageConsumer.ofConsumeParameters; | ||
import static java.util.Objects.isNull; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
@Component | ||
public class KafkaConsumption implements CliCommand { | ||
private final ConsumeParameters consumeParameters = new ConsumeParameters(); | ||
private final CountDownLatch awaitLatch = new CountDownLatch(1); | ||
|
||
@Override | ||
public String execute(CliMainParameters mainParameters) { | ||
final KafkaMessageConsumer messageConsumer = ofConsumeParameters(consumeParameters); | ||
|
||
final KafkaMessageListenerContainer<String, String> listenerContainer = messageConsumer.getListenerContainer(); | ||
listenerContainer.start(); | ||
|
||
registerContainerShutdownHook(listenerContainer); | ||
|
||
try { | ||
final Duration consumeDuration = consumeParameters.getDuration(); | ||
|
||
if (isNull(consumeDuration)) { | ||
awaitLatch.await(); | ||
} else { | ||
awaitLatch.await(consumeDuration.toMillis(), MILLISECONDS); | ||
} | ||
} catch (InterruptedException ex) { | ||
throw new CommandException("Kafka consumer interrupted!", ex); | ||
} finally { | ||
listenerContainer.stop(); | ||
} | ||
return ""; | ||
} | ||
|
||
private void registerContainerShutdownHook(KafkaMessageListenerContainer<String, String> listenerContainer) { | ||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
if (listenerContainer.isRunning()) { | ||
listenerContainer.stop(); | ||
} | ||
})); | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return KAFKA_CONSUME.getCliCommand(); | ||
} | ||
|
||
@Override | ||
public CliCommandParameters getParameters() { | ||
return consumeParameters; | ||
} | ||
} |
Oops, something went wrong.