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

com.arextest.schedule.mdc.AbstractTracedRunnable Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.arextest.schedule.mdc;

import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.MDC;

/**
 * @author jmo
 * @since 2021/11/5
 */
@Deprecated
@Slf4j
public abstract class AbstractTracedRunnable implements Runnable {

  private final Map traceMap;

  public AbstractTracedRunnable() {
    this.traceMap = MDC.getCopyOfContextMap();
  }

  @Override
  public final void run() {
    Map old = mark();
    try {
      this.doWithTracedRunning();
    } catch (Throwable ex) {
      LOGGER.error(ex.getMessage(), ex);
    } finally {
      removeMark(old);
    }
  }

  protected abstract void doWithTracedRunning();

  private Map mark() {
    if (MapUtils.isEmpty(this.traceMap)) {
      return null;
    } else {
      Map old = MDC.getCopyOfContextMap();
      MDC.setContextMap(this.traceMap);
      return old;
    }
  }

  private void removeMark(Map prev) {
    if (MapUtils.isEmpty(prev)) {
      MDC.clear();
    } else {
      MDC.setContextMap(prev);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy