com.epam.eco.commons.kafka.consumer.bootstrap.BootstrapConsumerExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-kafka Show documentation
Show all versions of commons-kafka Show documentation
A library of utilities, helpers and higher-level APIs for the
Kafka client library
/*
* Copyright 2019 EPAM Systems
*
* 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.epam.eco.commons.kafka.consumer.bootstrap;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.commons.lang3.Validate;
/**
* @author Andrei_Tytsik
*/
public class BootstrapConsumerExecutor {
private static final String THREAD_NAME_PATTERN = "btsp-%s(%d)-%d";
private static final AtomicInteger EXECUTOR_INDEX = new AtomicInteger(0);
private final List> threads;
public BootstrapConsumerExecutor(
BootstrapConsumer.Builder consumerBuilder,
Supplier> recordCollectorSupplier,
int instanceCount,
Consumer handler) {
Validate.notNull(consumerBuilder, "Bootstrap Consumer builder is null");
Validate.notNull(recordCollectorSupplier, "RecordCollector supplier is null");
Validate.isTrue(instanceCount > 0, "Instance count is invalid");
Validate.notNull(handler, "Handler is null");
threads = initThreads(
consumerBuilder,
recordCollectorSupplier,
instanceCount,
handler);
}
public void start() {
for (BootstrapConsumerThread thread : threads) {
thread.start();
}
}
public void waitForBootstrap() throws InterruptedException {
for (BootstrapConsumerThread thread : threads) {
thread.waitForBootstrap();
}
}
public void shutdown() throws InterruptedException {
for (BootstrapConsumerThread thread : threads) {
thread.shutdown();
}
}
private List> initThreads(
BootstrapConsumer.Builder builder,
Supplier> recordCollectorSupplier,
int instanceCount,
Consumer handler) {
int executorIndex = getExecutorIndex();
List> threads = new ArrayList<>(instanceCount);
for (int instanceIndex = 0; instanceIndex < instanceCount; instanceIndex++) {
BootstrapConsumer consumer = builder.
recordCollector(recordCollectorSupplier.get()).
instanceCount(instanceCount).
instanceIndex(instanceIndex).
build();
BootstrapConsumerThread thread = new BootstrapConsumerThread<>(
consumer,
handler);
thread.setName(
formatThreadName(consumer.getTopicName(), executorIndex, instanceIndex));
threads.add(thread);
}
return threads;
}
private static String formatThreadName(String topicName, int executorIndex, int instanceIndex) {
return String.format(THREAD_NAME_PATTERN, topicName, executorIndex, instanceIndex);
}
private static int getExecutorIndex() {
return EXECUTOR_INDEX.getAndIncrement();
}
public static BootstrapConsumerExecutor with(
BootstrapConsumer.Builder consumerBuilder,
Supplier> recordCollectorSupplier,
int instanceCount,
Consumer handler) {
return new BootstrapConsumerExecutor<>(
consumerBuilder,
recordCollectorSupplier,
instanceCount,
handler);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy