You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by mm...@apache.org on 2017/05/10 20:47:07 UTC
bookkeeper git commit: BOOKKEEPER-1060: Add utility to use
SafeRunnable from Java8 Lambda
Repository: bookkeeper
Updated Branches:
refs/heads/master d8a7ac310 -> af397e224
BOOKKEEPER-1060: Add utility to use SafeRunnable from Java8 Lambda
Since BK-4.5.0 is already switched to Java8, we should have a simple and concise way to pass lambdas where a `SafeRunnable` is required.
Author: Matteo Merli <mm...@apache.org>
Reviewers: Enrico Olivelli <None>
Closes #148 from merlimat/safe-runnable
Project: http://git-wip-us.apache.org/repos/asf/bookkeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/bookkeeper/commit/af397e22
Tree: http://git-wip-us.apache.org/repos/asf/bookkeeper/tree/af397e22
Diff: http://git-wip-us.apache.org/repos/asf/bookkeeper/diff/af397e22
Branch: refs/heads/master
Commit: af397e2247d453af25db6a1f33426fb9c69e2cdf
Parents: d8a7ac3
Author: Matteo Merli <mm...@apache.org>
Authored: Wed May 10 13:47:02 2017 -0700
Committer: Matteo Merli <mm...@apache.org>
Committed: Wed May 10 13:47:02 2017 -0700
----------------------------------------------------------------------
.../apache/bookkeeper/util/SafeRunnable.java | 56 ++++++++++++++++++++
1 file changed, 56 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/bookkeeper/blob/af397e22/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/SafeRunnable.java
----------------------------------------------------------------------
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/SafeRunnable.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/SafeRunnable.java
index b3b5d36..8b1e0d0 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/SafeRunnable.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/SafeRunnable.java
@@ -1,5 +1,7 @@
package org.apache.bookkeeper.util;
+import java.util.function.Consumer;
+
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -36,4 +38,58 @@ public abstract class SafeRunnable implements Runnable {
public abstract void safeRun();
+ /**
+ * Utility method to use SafeRunnable from lambdas
+ * <p>
+ * Eg:
+ * <pre>
+ * <code>
+ * executor.submit(SafeRunnable.safeRun(() -> {
+ * // My not-safe code
+ * });
+ * </code>
+ * </pre>
+ */
+ public static SafeRunnable safeRun(Runnable runnable) {
+ return new SafeRunnable() {
+ @Override
+ public void safeRun() {
+ runnable.run();
+ }
+ };
+ }
+
+ /**
+ * Utility method to use SafeRunnable from lambdas with
+ * a custom exception handler
+ * <p>
+ * Eg:
+ * <pre>
+ * <code>
+ * executor.submit(SafeRunnable.safeRun(() -> {
+ * // My not-safe code
+ * }, exception -> {
+ * // Handle exception
+ * );
+ * </code>
+ * </pre>
+ *
+ * @param runnable
+ * @param exceptionHandler
+ * handler that will be called when there are any exception
+ * @return
+ */
+ public static SafeRunnable safeRun(Runnable runnable, Consumer<Throwable> exceptionHandler) {
+ return new SafeRunnable() {
+ @Override
+ public void safeRun() {
+ try {
+ runnable.run();
+ } catch (Throwable t) {
+ exceptionHandler.accept(t);
+ throw t;
+ }
+ }
+ };
+ }
}