com.opentable.kafka.builders.KafkaConsumerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otj-kafka Show documentation
Show all versions of otj-kafka Show documentation
Kafka integrations component
/*
* 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 com.opentable.kafka.builders;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.Deserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Main builder for KafkaConsumer. This is usually entered via a KafkaConsumerBuilderFactoryBean so some "sugar" is injected.
* @param Key
* @param Value
*/
public class KafkaConsumerBuilder extends KafkaConsumerBaseBuilder, K, V> {
private static final Logger LOG = LoggerFactory.getLogger(KafkaConsumerBuilder.class);
public KafkaConsumerBuilder(EnvironmentProvider environmentProvider) {
this(new HashMap<>(), environmentProvider);
}
public KafkaConsumerBuilder(Map prop, EnvironmentProvider environmentProvider) {
super(prop, environmentProvider);
}
@Override
protected KafkaConsumerBuilder self() {
return this;
}
@Override
public KafkaConsumerBuilder withDeserializers(Class extends Deserializer> keyDeSer, Class extends Deserializer> valDeSer) {
return (KafkaConsumerBuilder) super.withDeserializers(keyDeSer, valDeSer);
}
@Override
public KafkaConsumerBuilder withDeserializers(Deserializer keyDeSer, Deserializer valDeSer) {
return (KafkaConsumerBuilder) super.withDeserializers(keyDeSer, valDeSer);
}
private Consumer consumer(Deserializer keyDeserializer, Deserializer valuedeserializer) {
LOG.trace("Building KafkaConsumer with props {}", getFinalProperties());
if (keyDeserializer != null || valuedeserializer != null) {
LOG.warn("You passed an INSTANCE (as opposed to class) as a Deserializer. That's fine, but realize configure() will not be called on the Deserializer. That's per Kafka Design.");
}
return new KafkaConsumer<>(getFinalProperties(), keyDeserializer, valuedeserializer);
}
public Consumer build() {
internalBuild();
return consumer(keyDeserializerInstance, valueDeserializerInstance);
}
public Supplier> supplier() {
internalBuild();
return () -> consumer(keyDeserializerInstance, valueDeserializerInstance);
}
}