org.apache.kafka.common.KafkaFuture Maven / Gradle / Ivy
/*
* 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 org.apache.kafka.common;
import org.apache.kafka.common.annotation.InterfaceStability;
import org.apache.kafka.common.internals.KafkaFutureImpl;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A flexible future which supports call chaining and other asynchronous programming patterns. This will
* eventually become a thin shim on top of Java 8's CompletableFuture.
*
* The API for this class is still evolving and we may break compatibility in minor releases, if necessary.
*/
@InterfaceStability.Evolving
public abstract class KafkaFuture implements Future {
/**
* A function which takes objects of type A and returns objects of type B.
*/
public interface BaseFunction {
B apply(A a);
}
/**
* A function which takes objects of type A and returns objects of type B.
*
* Prefer the functional interface {@link BaseFunction} over the class {@link Function}. This class is here for
* backwards compatibility reasons and might be deprecated/removed in a future release.
*/
public static abstract class Function implements BaseFunction { }
/**
* A consumer of two different types of object.
*/
public interface BiConsumer {
void accept(A a, B b);
}
private static class AllOfAdapter implements BiConsumer {
private int remainingResponses;
private KafkaFuture> future;
public AllOfAdapter(int remainingResponses, KafkaFuture> future) {
this.remainingResponses = remainingResponses;
this.future = future;
maybeComplete();
}
@Override
public synchronized void accept(R newValue, Throwable exception) {
if (remainingResponses <= 0)
return;
if (exception != null) {
remainingResponses = 0;
future.completeExceptionally(exception);
} else {
remainingResponses--;
maybeComplete();
}
}
private void maybeComplete() {
if (remainingResponses <= 0)
future.complete(null);
}
}
/**
* Returns a new KafkaFuture that is already completed with the given value.
*/
public static KafkaFuture completedFuture(U value) {
KafkaFuture future = new KafkaFutureImpl();
future.complete(value);
return future;
}
/**
* Returns a new KafkaFuture that is completed when all the given futures have completed. If
* any future throws an exception, the returned future returns it. If multiple futures throw
* an exception, which one gets returned is arbitrarily chosen.
*/
public static KafkaFuture allOf(KafkaFuture>... futures) {
KafkaFuture allOfFuture = new KafkaFutureImpl<>();
AllOfAdapter