Class MDCHelper
Provides usefull methods for working with MDC.
Use the various tryWithMDC(java.util.function.Supplier<? extends org.bonitasoft.engine.mdc.AbstractMDC>, org.bonitasoft.engine.mdc.MDCHelper.CheckedCallable4<V, E1, E2, E3, E4>) methods to execute a runnable or a callable with MDC information in the
context.
You may provide a context Supplier directly, or a key object for which a Supplier was previously
registered with supplyMDC(java.util.function.Supplier<? extends org.bonitasoft.engine.mdc.AbstractMDC>, java.lang.Object)
methods.
Use the appropriate Runnable or Callable implementation dependending on the number of Exception you want to
throw.
When used with lambdas, lambda will take only the common exception superclass. So instead of writing
Supplier<AbstractMDC> mdc = () -> new AbstractMDC(Map.of(key, value)) {};
MDCHelper.tryWithMDC(mdc, ()->{
// throws Exception1 or Exception2
...
});
You should declare the type and write
Supplier<AbstractMDC> mdc = () -> new AbstractMDC(Map.of(key, value)) {};
CheckedRunnable2<Exception1, Exception2> run = ()->{
// throws Exception1 or Exception2
...
};
MDCHelper.tryWithMDC(mdc, run);
When an exception is thrown within a tryWithMDC(java.util.function.Supplier<? extends org.bonitasoft.engine.mdc.AbstractMDC>, org.bonitasoft.engine.mdc.MDCHelper.CheckedCallable4<V, E1, E2, E3, E4>) method and not caught by the Runnable or Callable, the
current context is registered with
supplyMDC(java.util.function.Supplier<? extends org.bonitasoft.engine.mdc.AbstractMDC>, java.lang.Object) for the exception.
So calling tryWithMDC(Object, CheckedRunnable4) with the exception as usingObject key will
allow you to log the exception with the
original context.
You can even call this method when no context was supplied, as it would have no noticeable effect.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceMDCHelper.CheckedCallable<V,E extends Throwable> LikeCallablebut throwing a checked exceptionstatic interfaceLikeCallablebut throwing 2 checked exceptionsstatic interfaceLikeCallablebut throwing 3 checked exceptionsstatic interfaceMDCHelper.CheckedCallable4<V,E1 extends Throwable, E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> LikeCallablebut throwing 4 checked exceptionsstatic interfaceMDCHelper.CheckedRunnable<E extends Throwable>LikeRunnablebut throwing a checked exceptionstatic interfaceMDCHelper.CheckedRunnable2<E1 extends Throwable,E2 extends Throwable> LikeRunnablebut throwing 2 checked exceptionsstatic interfaceLikeRunnablebut throwing 3 checked exceptionsstatic interfaceMDCHelper.CheckedRunnable4<E1 extends Throwable,E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> LikeRunnablebut throwing 4 checked exceptions -
Method Summary
Modifier and TypeMethodDescriptionstatic AbstractMDCGet an MDC, to encapsulate in a try with.static Supplier<? extends AbstractMDC>Make a supplier that will get the current context, to create a similar MDC in its time (usually in another thread)static voidsupplyMDC(Supplier<? extends AbstractMDC> mdcSupplier, Object usingObject) Supply a MDC to an object that may use it later.static voidsupplyMDC(Supplier<? extends AbstractMDC> mdcSupplier, Object usingObject, boolean overwrite) Supply a MDC to an object that may use it later.tryWithMDC(Object usingObject, MDCHelper.CheckedCallable4<V, E1, E2, E3, E4> callable) Try and invoke a callable with MDCtryWithMDC(Object usingObject, MDCHelper.CheckedRunnable4<E1, E2, E3, E4> runnable) Try and invoke a runnable with MDCtryWithMDC(Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedCallable4<V, E1, E2, E3, E4> callable) Try and invoke a callable with MDCtryWithMDC(Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedRunnable4<E1, E2, E3, E4> runnable) Try and invoke a runnable with MDC
-
Method Details
-
makeCurrentContextSupplier
Make a supplier that will get the current context, to create a similar MDC in its time (usually in another thread)- Returns:
- MDC supplier holding information from current context in this thread
-
supplyMDC
Supply a MDC to an object that may use it later.- Parameters:
mdcSupplier- supplies a MDCusingObject- the object that will need MDC
-
supplyMDC
public static void supplyMDC(Supplier<? extends AbstractMDC> mdcSupplier, Object usingObject, boolean overwrite) Supply a MDC to an object that may use it later.- Parameters:
mdcSupplier- supplies a MDCusingObject- the object that will need MDCoverwrite- whether to overwrite an existing supplier
-
getMDC
Get an MDC, to encapsulate in a try with.- Parameters:
usingObject- the object using the MDC, for which a supplier may have been provided- Returns:
- the supplied MDC or null
-
tryWithMDC
public static <V,E1 extends Throwable, V tryWithMDCE2 extends Throwable, E3 extends Throwable, E4 extends Throwable> (Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedCallable4<V, E1, throws E1, E2, E3, E4E2, E3, E4> callable) Try and invoke a callable with MDC -
tryWithMDC
public static <V,E1 extends Throwable, V tryWithMDCE2 extends Throwable, E3 extends Throwable, E4 extends Throwable> (Object usingObject, MDCHelper.CheckedCallable4<V, E1, throws E1, E2, E3, E4E2, E3, E4> callable) Try and invoke a callable with MDC- Type Parameters:
V- the result type- Parameters:
usingObject- the object using the MDC, for which a supplier may have been providedcallable- the callable with result- Returns:
- the callable result
- Throws:
E- exception in callableE1 extends ThrowableE2 extends ThrowableE3 extends ThrowableE4 extends Throwable
-
tryWithMDC
public static <E1 extends Throwable,E2 extends Throwable, void tryWithMDCE3 extends Throwable, E4 extends Throwable> (Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedRunnable4<E1, E2, throws E1, E2, E3, E4E3, E4> runnable) Try and invoke a runnable with MDC -
tryWithMDC
public static <E1 extends Throwable,E2 extends Throwable, void tryWithMDCE3 extends Throwable, E4 extends Throwable> (Object usingObject, MDCHelper.CheckedRunnable4<E1, E2, throws E1, E2, E3, E4E3, E4> runnable) Try and invoke a runnable with MDC
-