mirror of https://github.com/apache/kafka.git
KAFKA-14012: Add warning to closeQuietly documentation about method references of null objects (#12321)
Reviewers: Kvicii <42023367+Kvicii@users.noreply.github.com>, Chris Egerton <fearthecellos@gmail.com>
This commit is contained in:
parent
a5d71e1550
commit
3ddb62316f
|
@ -998,6 +998,14 @@ public final class Utils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes {@code closeable} and if an exception is thrown, it is logged at the WARN level.
|
* Closes {@code closeable} and if an exception is thrown, it is logged at the WARN level.
|
||||||
|
* <b>Be cautious when passing method references as an argument.</b> For example:
|
||||||
|
* <p>
|
||||||
|
* {@code closeQuietly(task::stop, "source task");}
|
||||||
|
* <p>
|
||||||
|
* Although this method gracefully handles null {@link AutoCloseable} objects, attempts to take a method
|
||||||
|
* reference from a null object will result in a {@link NullPointerException}. In the example code above,
|
||||||
|
* it would be the caller's responsibility to ensure that {@code task} was non-null before attempting to
|
||||||
|
* use a method reference from it.
|
||||||
*/
|
*/
|
||||||
public static void closeQuietly(AutoCloseable closeable, String name) {
|
public static void closeQuietly(AutoCloseable closeable, String name) {
|
||||||
if (closeable != null) {
|
if (closeable != null) {
|
||||||
|
@ -1009,6 +1017,17 @@ public final class Utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closes {@code closeable} and if an exception is thrown, it is registered to the firstException parameter.
|
||||||
|
* <b>Be cautious when passing method references as an argument.</b> For example:
|
||||||
|
* <p>
|
||||||
|
* {@code closeQuietly(task::stop, "source task");}
|
||||||
|
* <p>
|
||||||
|
* Although this method gracefully handles null {@link AutoCloseable} objects, attempts to take a method
|
||||||
|
* reference from a null object will result in a {@link NullPointerException}. In the example code above,
|
||||||
|
* it would be the caller's responsibility to ensure that {@code task} was non-null before attempting to
|
||||||
|
* use a method reference from it.
|
||||||
|
*/
|
||||||
public static void closeQuietly(AutoCloseable closeable, String name, AtomicReference<Throwable> firstException) {
|
public static void closeQuietly(AutoCloseable closeable, String name, AtomicReference<Throwable> firstException) {
|
||||||
if (closeable != null) {
|
if (closeable != null) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -59,6 +59,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
@ -235,7 +236,7 @@ public abstract class AbstractWorkerSourceTask extends WorkerTask {
|
||||||
this.admin = admin;
|
this.admin = admin;
|
||||||
this.offsetReader = offsetReader;
|
this.offsetReader = offsetReader;
|
||||||
this.offsetWriter = offsetWriter;
|
this.offsetWriter = offsetWriter;
|
||||||
this.offsetStore = offsetStore;
|
this.offsetStore = Objects.requireNonNull(offsetStore, "offset store cannot be null for source tasks");
|
||||||
this.closeExecutor = closeExecutor;
|
this.closeExecutor = closeExecutor;
|
||||||
this.sourceTaskContext = sourceTaskContext;
|
this.sourceTaskContext = sourceTaskContext;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue