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

io.ebean.config.MdcBackgroundExecutorWrapper Maven / Gradle / Ivy

There is a newer version: 15.8.0
Show newest version
package io.ebean.config;

import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.Callable;

/**
 * Propagates MDC context for tasks executed in the background.
 */
public final class MdcBackgroundExecutorWrapper implements BackgroundExecutorWrapper {

  /**
   * Wrap the task with MDC context if defined.
   */
  @Override
  public  Callable wrap(Callable task) {
    final Map map = MDC.getCopyOfContextMap();
    if (map == null) {
      return task;
    } else {
      return () -> {
        MDC.setContextMap(map);
        try {
          return task.call();
        } finally {
          MDC.clear();
        }
      };
    }
  }

  /**
   * Wrap the task with MDC context if defined.
   */
  @Override
  public Runnable wrap(Runnable task) {
    final Map map = MDC.getCopyOfContextMap();
    if (map == null) {
      return task;
    } else {
      return () -> {
        MDC.setContextMap(map);
        try {
          task.run();
        } finally {
          MDC.clear();
        }
      };
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy