diff --git a/ebean-api/src/main/java/io/ebean/config/BackgroundExecutorWrapper.java b/ebean-api/src/main/java/io/ebean/config/BackgroundExecutorWrapper.java index 306b7e88e..b290e5863 100644 --- a/ebean-api/src/main/java/io/ebean/config/BackgroundExecutorWrapper.java +++ b/ebean-api/src/main/java/io/ebean/config/BackgroundExecutorWrapper.java @@ -3,9 +3,9 @@ package io.ebean.config; import java.util.concurrent.Callable; /** - * BackgroundExecutorWrapper that can be used to wrap tasks that are sent to background (i.e. an other thread). - * It should copy all neccessary thread-local variables. See {@link MdcBackgroundExecutorWrapper} for implementation details. - * + * BackgroundExecutorWrapper that can be used to wrap tasks that are sent to background (i.e. another thread). + * It should copy all necessary thread-local variables. See {@link MdcBackgroundExecutorWrapper} for implementation details. + * * @author Roland Praml, FOCONIS AG */ public interface BackgroundExecutorWrapper { @@ -21,22 +21,22 @@ public interface BackgroundExecutorWrapper { Runnable wrap(Runnable task); /** - * Combines two wrappers by joining them. + * Combines two wrappers by nesting them. */ default BackgroundExecutorWrapper with(BackgroundExecutorWrapper inner) { return new BackgroundExecutorWrapper() { - + @Override public Runnable wrap(Runnable task) { return BackgroundExecutorWrapper.this.wrap(inner.wrap(task)); } - + @Override public Callable wrap(Callable task) { return BackgroundExecutorWrapper.this.wrap(inner.wrap(task)); } }; - + } } diff --git a/ebean-api/src/main/java/io/ebean/config/MdcBackgroundExecutorWrapper.java b/ebean-api/src/main/java/io/ebean/config/MdcBackgroundExecutorWrapper.java index 5a0fa7980..af6a3ac26 100644 --- a/ebean-api/src/main/java/io/ebean/config/MdcBackgroundExecutorWrapper.java +++ b/ebean-api/src/main/java/io/ebean/config/MdcBackgroundExecutorWrapper.java @@ -1,13 +1,15 @@ package io.ebean.config; +import org.slf4j.MDC; + import java.util.Map; import java.util.concurrent.Callable; -import org.slf4j.MDC; +/** + * Propagates MDC context for tasks executed in the background. + */ +public final class MdcBackgroundExecutorWrapper implements BackgroundExecutorWrapper { -public class MdcBackgroundExecutorWrapper implements BackgroundExecutorWrapper { - - /** * Wrap the task with MDC context if defined. */ diff --git a/ebean-core/src/main/java/io/ebeaninternal/server/executor/DefaultBackgroundExecutor.java b/ebean-core/src/main/java/io/ebeaninternal/server/executor/DefaultBackgroundExecutor.java index 468f397ff..6e49ff255 100644 --- a/ebean-core/src/main/java/io/ebeaninternal/server/executor/DefaultBackgroundExecutor.java +++ b/ebean-core/src/main/java/io/ebeaninternal/server/executor/DefaultBackgroundExecutor.java @@ -15,8 +15,8 @@ import org.slf4j.LoggerFactory; @NonNullApi public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { - protected static final Logger logger = LoggerFactory.getLogger("io.ebean.BackgroundExecutor"); - + private static final Logger log = LoggerFactory.getLogger("io.ebean.BackgroundExecutor"); + private final ScheduledExecutorService schedulePool; private final DaemonExecutorService pool; private final BackgroundExecutorWrapper wrapper; @@ -28,7 +28,7 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { this.schedulePool = new DaemonScheduleThreadPool(schedulePoolSize, shutdownWaitSeconds, namePrefix + "-periodic-"); this.pool = new DaemonExecutorService(shutdownWaitSeconds, namePrefix); this.wrapper = wrapper; - logger.debug("Created backgroundExecutor {} (schedulePoolSize={}, shutdownWaitSeconds={})", namePrefix, schedulePoolSize, shutdownWaitSeconds); + log.debug("Created backgroundExecutor {} (schedulePoolSize={}, shutdownWaitSeconds={})", namePrefix, schedulePoolSize, shutdownWaitSeconds); } /** @@ -52,27 +52,27 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { return wrapper.wrap(task); } } - + /** * Decorates a runnable by adding an exception handler and some timing metrics. * This is used in methods that accepts a Runnable and return * either void or ScheduledFuture, as there is * normally no Future.get() call. - * + * * Note: When submitting a Callable, you must check * Future.get() for exceptions. */ private Runnable logExceptions(Runnable task) { long queued = System.nanoTime(); - logger.trace("Queued {}", task); + log.trace("Queued {}", task); return () -> { try { - if (logger.isTraceEnabled()) { + if (log.isTraceEnabled()) { long start = System.nanoTime(); - logger.trace("Start {} (delay time {} us)", task, (start - queued) / 1000L); + log.trace("Start {} (delay time {} us)", task, (start - queued) / 1000L); task.run(); - logger.trace("Stop {} (exec time {} us)", task, (System.nanoTime() - start) / 1000L); + log.trace("Stop {} (exec time {} us)", task, (System.nanoTime() - start) / 1000L); } else { task.run(); } @@ -80,7 +80,7 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { // log any exception here. Note they will not bubble up to the calling user // unless Future.get() is checked. (Which is almost never done on scheduled // background executions) - logger.error("Error while executing the task {}", task, t); + log.error("Error while executing the task {}", task, t); throw t; } }; @@ -88,6 +88,7 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { @Override public Future submit(Callable task) { + // Note: No "logExceptions" as we expect Future.get() by the invoker return pool.submit(wrap(task)); } @@ -99,7 +100,7 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { return pool.submit(wrap(task)); } - + @Override public void execute(Runnable task) { submit(logExceptions(task)); @@ -132,16 +133,16 @@ public final class DefaultBackgroundExecutor implements SpiBackgroundExecutor { @Override public ScheduledFuture schedule(Callable task, long delay, TimeUnit unit) { - // Note: Here is no "logExceptions", because it is intended to check Future.get() by the invoker + // Note: No "logExceptions" as we expect Future.get() by the invoker return schedulePool.schedule(wrap(task), delay, unit); } @Override public void shutdown() { - logger.trace("Shutting down backgroundExecutor"); + log.trace("BackgroundExecutor shutting down"); schedulePool.shutdown(); pool.shutdown(); - logger.debug("BackgroundExecutor stopped"); + log.debug("BackgroundExecutor stopped"); } }