001package io.ebean.config;
002
003import java.util.concurrent.Callable;
004
005/**
006 * BackgroundExecutorWrapper that can be used to wrap tasks that are sent to background (i.e. another thread).
007 * It should copy all necessary thread-local variables. See {@link MdcBackgroundExecutorWrapper} for implementation details.
008 *
009 * @author Roland Praml, FOCONIS AG
010 */
011public interface BackgroundExecutorWrapper {
012
013  /**
014   * Wrap the task with MDC context if defined.
015   */
016  <T> Callable<T> wrap(Callable<T> task);
017
018  /**
019   * Wrap the task with MDC context if defined.
020   */
021  Runnable wrap(Runnable task);
022
023  /**
024   * Combines two wrappers by nesting them.
025   */
026  default BackgroundExecutorWrapper with(BackgroundExecutorWrapper inner) {
027    return new BackgroundExecutorWrapper() {
028
029      @Override
030      public Runnable wrap(Runnable task) {
031        return BackgroundExecutorWrapper.this.wrap(inner.wrap(task));
032      }
033
034      @Override
035      public <T> Callable<T> wrap(Callable<T> task) {
036        return BackgroundExecutorWrapper.this.wrap(inner.wrap(task));
037      }
038    };
039
040  }
041
042}