
com.newrelic.agent.util.SafeWrappers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of newrelic-agent Show documentation
Show all versions of newrelic-agent Show documentation
Jar required to run with a java application to monitor performance.
The newest version!
/*
*
* * Copyright 2020 New Relic Corporation. All rights reserved.
* * SPDX-License-Identifier: Apache-2.0
*
*/
package com.newrelic.agent.util;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import com.newrelic.agent.Agent;
public class SafeWrappers {
private SafeWrappers() {
}
public static Runnable safeRunnable(final Runnable runnable) {
return new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Throwable t) {
try {
Agent.LOG.log(Level.SEVERE, t.toString());
Agent.LOG.log(Level.FINEST, t, t.toString());
} catch (Throwable t2) {
}
}
}
};
}
/**
* This wraps a scheduled executor so that provided Runnables squelch and log all exceptions. Right
* now the apis that use callables and all of the invokeAny / invokeAll methods are unsupported. The shutdown calls
* are also unsupported so that services can pass out a wrapped reference to their executor without a caller being
* able to shut it down.
*/
public static ScheduledExecutorService safeExecutor(final ScheduledExecutorService executor) {
return new ScheduledExecutorService() {
public ScheduledFuture> schedule(Runnable command, long delay, TimeUnit unit) {
return executor.schedule(safeRunnable(command), delay, unit);
}
public void execute(Runnable command) {
executor.execute(command);
}
public ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
public ScheduledFuture> scheduleAtFixedRate(Runnable command, long initialDelay, long period,
TimeUnit unit) {
return executor.scheduleAtFixedRate(safeRunnable(command), initialDelay, period, unit);
}
public void shutdown() {
throw new UnsupportedOperationException();
}
public List shutdownNow() {
throw new UnsupportedOperationException();
}
public ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,
TimeUnit unit) {
return executor.scheduleWithFixedDelay(safeRunnable(command), initialDelay, delay, unit);
}
public boolean isShutdown() {
return executor.isShutdown();
}
public boolean isTerminated() {
return executor.isTerminated();
}
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}
public Future submit(Callable task) {
throw new UnsupportedOperationException();
}
public Future submit(Runnable task, T result) {
return executor.submit(safeRunnable(task), result);
}
public Future> submit(Runnable task) {
return executor.submit(safeRunnable(task));
}
public List> invokeAll(Collection extends Callable> tasks) throws InterruptedException {
throw new UnsupportedOperationException();
}
public List> invokeAll(Collection extends Callable> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
throw new UnsupportedOperationException();
}
public T invokeAny(Collection extends Callable> tasks) throws InterruptedException,
ExecutionException {
throw new UnsupportedOperationException();
}
public T invokeAny(Collection extends Callable> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
throw new UnsupportedOperationException();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy