C0urante commented on code in PR #14304:
URL: https://github.com/apache/kafka/pull/14304#discussion_r1309290588


##########
clients/src/main/java/org/apache/kafka/common/utils/Utils.java:
##########
@@ -1090,6 +1090,23 @@ public interface UncheckedCloseable extends 
AutoCloseable {
         void close();
     }
 
+    /**
+     * Closes {@code maybeCloseable} if it implements the {@link 
AutoCloseable} interface,
+     * 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 maybeCloseQuietly(Object maybeCloseable, String name) {

Review Comment:
   This is overkill for a single use (which is the case in this PR), but a 
second use case is added for it in https://github.com/apache/kafka/pull/14309.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to