|
| 1 | +package org.apache.helix.util; |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +import java.util.concurrent.Callable; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +public class ExecutorTaskUtil { |
| 28 | + |
| 29 | + private static final Logger LOG = LoggerFactory.getLogger(ExecutorTaskUtil.class); |
| 30 | + |
| 31 | + /** |
| 32 | + * Wrap a callable so that any raised exception is logged |
| 33 | + * (can be interesting in case the callable is used as a completely asynchronous task |
| 34 | + * fed to an {@link java.util.concurrent.ExecutorService}), for which we are never |
| 35 | + * calling any of the {@link java.util.concurrent.Future#get()} or {@link java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)} |
| 36 | + * methods. |
| 37 | + */ |
| 38 | + public static <T> Callable<T> wrap(Callable<T> callable) { |
| 39 | + return () -> { |
| 40 | + try { |
| 41 | + return callable.call(); |
| 42 | + } catch (Throwable t) { |
| 43 | + LOG.error("Callable run on thread {} raised an exception and exited", Thread.currentThread().getName(), t); |
| 44 | + throw t; |
| 45 | + } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Wrap a runnable so that any raised exception is logged |
| 51 | + * (can be interesting in case the callable is used as a completely asynchronous task |
| 52 | + * fed to an {@link java.util.concurrent.ExecutorService}), for which we are never |
| 53 | + * calling any of the {@link java.util.concurrent.Future#get()} or {@link java.util.concurrent.Future#get(long, java.util.concurrent.TimeUnit)} |
| 54 | + * methods. |
| 55 | + */ |
| 56 | + public static Runnable wrap(Runnable runnable) { |
| 57 | + return () -> { |
| 58 | + try { |
| 59 | + runnable.run(); |
| 60 | + } catch (Throwable t) { |
| 61 | + LOG.error("Runnable run on thread {} raised an exception and exited", Thread.currentThread().getName(), t); |
| 62 | + throw t; |
| 63 | + } |
| 64 | + }; |
| 65 | + } |
| 66 | +} |
0 commit comments