eolivelli commented on a change in pull request #12853:
URL: https://github.com/apache/pulsar/pull/12853#discussion_r751079058



##########
File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/Runnables.java
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.pulsar.common.util;
+
+import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class Runnables {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(Runnables.class);
+
+    /**
+     * Wraps a Runnable so that throwables are caught and logged when a 
Runnable is run.
+     *
+     * The main usecase for this method is to be used in {@link 
java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, 
long, long, TimeUnit)}
+     * calls to ensure that the scheduled task doesn't get cancelled as a 
result of an uncaught exception.
+     *
+     * @param runnable The runnable to wrap
+     * @return a wrapped Runnable
+     */
+    public static Runnable catchingAndLoggingThrowables(Runnable runnable) {

Review comment:
       What about 'safeRun'?
   This name sounds too long and verbose to me

##########
File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/Runnables.java
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.pulsar.common.util;
+
+import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class Runnables {

Review comment:
       What about adding a private constructor?

##########
File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/Runnables.java
##########
@@ -0,0 +1,46 @@
+/**
+ * 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.pulsar.common.util;
+
+import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class Runnables {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(Runnables.class);
+
+    /**
+     * Wraps a Runnable so that throwables are caught and logged when a 
Runnable is run.
+     *
+     * The main usecase for this method is to be used in {@link 
java.util.concurrent.ScheduledExecutorService#scheduleAtFixedRate(Runnable, 
long, long, TimeUnit)}
+     * calls to ensure that the scheduled task doesn't get cancelled as a 
result of an uncaught exception.
+     *
+     * @param runnable The runnable to wrap
+     * @return a wrapped Runnable
+     */
+    public static Runnable catchingAndLoggingThrowables(Runnable runnable) {
+        return () -> {

Review comment:
       Lambda usually generate ugly stack traces.
   What about using a regular static inner class?




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to