All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.driver.internal.core.util.concurrent.UncaughtExceptions Maven / Gradle / Ivy

The newest version!
/*
 * Copyright DataStax, Inc.
 *
 * Licensed 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 com.datastax.oss.driver.internal.core.util.concurrent;

import com.datastax.oss.driver.internal.core.util.Loggers;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility methods to log unexpected exceptions in asynchronous tasks.
 *
 * 

Use this whenever you execute a future callback to apply side effects, but throw away the * future itself: * *

{@code
 * CompletionStage futureFoo = FooFactory.build();
 *
 * futureFoo
 *   .whenComplete((f, error) -> { handler code with side effects })
 *   // futureFoo is not propagated, do this or any unexpected error in the handler will be
 *   // swallowed
 *   .exceptionally(UncaughtExceptions::log);
 *
 * // If you return the future, you don't need it (but it's up to the caller to handle a failed
 * // future)
 * return futureFoo.whenComplete(...)
 * }
*/ public class UncaughtExceptions { private static final Logger LOG = LoggerFactory.getLogger(UncaughtExceptions.class); public static void log(Future future) { if (!future.isSuccess() && !future.isCancelled()) { Loggers.warnWithException(LOG, "Uncaught exception in scheduled task", future.cause()); } } @SuppressWarnings("TypeParameterUnusedInFormals") // type parameter is only needed for chaining public static T log(Throwable t) { Loggers.warnWithException(LOG, "Uncaught exception in scheduled task", t); return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy