com.spotify.helios.agent.KafkaClientProvider Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2014 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.spotify.helios.agent;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.util.List;
public class KafkaClientProvider {
private static final Logger log = LoggerFactory.getLogger(KafkaClientProvider.class);
private static final String KAFKA_HELIOS_CLIENT_ID = "Helios";
private static final String KAFKA_QUORUM_PARAMETER = "1";
private final Optional> partialConfigs;
public KafkaClientProvider(@Nullable final List brokerList) {
partialConfigs = Optional.fromNullable(brokerList).transform(
new Function, ImmutableMap>() {
@Nullable
@Override
public ImmutableMap apply(List input) {
return ImmutableMap.of(
"bootstrap.servers", Joiner.on(',').join(input),
"acks", KAFKA_QUORUM_PARAMETER,
"client.id", KAFKA_HELIOS_CLIENT_ID);
}
});
}
static KafkaClientProvider getTestingProvider() {
return new KafkaClientProvider(null);
}
public Optional> getProducer(@NotNull final Serializer ks,
@NotNull final Serializer vs) {
try {
return partialConfigs.transform(
new Function, KafkaProducer>() {
@Nullable
@Override
public KafkaProducer apply(ImmutableMap input) {
return new KafkaProducer<>(input, ks, vs);
}
});
} catch (final Exception e) {
log.warn("error while generating KafkaProducer - {}", e);
return Optional.absent();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy