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

cd.connect.context.ContextAsync Maven / Gradle / Ivy

Go to download

core functional logging for platform - loads services to enhance each log. we recommend async logger with log4j2.

There is a newer version: 2.2
Show newest version
package cd.connect.context;

import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author Richard Vowles - https://plus.google.com/+RichardVowles
 */
public class ContextAsync {
  public static  Consumer chainContext(final Consumer original) {
    final Map originalContext = ConnectContext.getContext();

    return new Consumer() {
      @Override
      public void accept(T t) {
        try {
          ConnectContext.setContext(originalContext);

          original.accept(t);
        } finally {
          ConnectContext.clear();
        }
      }
    };
  }

  public static Supplier chainContext(final Supplier original) {
    final Map originalContext = ConnectContext.getContext();

    return new Supplier() {
      @Override
      public T get() {
        try {
          ConnectContext.setContext(originalContext);
          return original.get();
        } finally {
          ConnectContext.clear();
        }
      }
    };
  }

  public static Runnable chainContext(final Runnable runnable) {
    final Map originalContext = ConnectContext.getContext();

    return new Runnable() {
      @Override
      public void run() {
        try {
          ConnectContext.setContext(originalContext);
          runnable.run();
        } finally {
          ConnectContext.clear();
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy