cd.connect.context.ContextAsync Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connect-java-logging Show documentation
Show all versions of connect-java-logging Show documentation
core functional logging for platform - loads services to enhance each log.
we recommend async logger with log4j2.
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();
}
}
};
}
}