Class MDCHelper

java.lang.Object
org.bonitasoft.engine.mdc.MDCHelper

public class MDCHelper extends Object

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.

See Also:
  • Method Details

    • makeCurrentContextSupplier

      public static Supplier<? extends AbstractMDC> 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

      public static void supplyMDC(Supplier<? extends AbstractMDC> mdcSupplier, Object usingObject)
      Supply a MDC to an object that may use it later.
      Parameters:
      mdcSupplier - supplies a MDC
      usingObject - 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 MDC
      usingObject - the object that will need MDC
      overwrite - whether to overwrite an existing supplier
    • getMDC

      public static AbstractMDC getMDC(Object usingObject)
      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, E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> V tryWithMDC(Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedCallable4<V,E1,E2,E3,E4> callable) throws E1, E2, E3, E4
      Try and invoke a callable with MDC
      Type Parameters:
      V - the result type
      Parameters:
      mdcSupplier - supplies a MDC
      callable - the callable with result
      Returns:
      the callable result
      Throws:
      E - exception in callable
      E1 extends Throwable
      E2 extends Throwable
      E3 extends Throwable
      E4 extends Throwable
    • tryWithMDC

      public static <V, E1 extends Throwable, E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> V tryWithMDC(Object usingObject, MDCHelper.CheckedCallable4<V,E1,E2,E3,E4> callable) throws E1, E2, E3, E4
      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 provided
      callable - the callable with result
      Returns:
      the callable result
      Throws:
      E - exception in callable
      E1 extends Throwable
      E2 extends Throwable
      E3 extends Throwable
      E4 extends Throwable
    • tryWithMDC

      public static <E1 extends Throwable, E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> void tryWithMDC(Supplier<? extends AbstractMDC> mdcSupplier, MDCHelper.CheckedRunnable4<E1,E2,E3,E4> runnable) throws E1, E2, E3, E4
      Try and invoke a runnable with MDC
      Parameters:
      mdcSupplier - supplies a MDC
      runnable - the runnable (without result)
      Throws:
      E - exception in runnable
      E1 extends Throwable
      E2 extends Throwable
      E3 extends Throwable
      E4 extends Throwable
    • tryWithMDC

      public static <E1 extends Throwable, E2 extends Throwable, E3 extends Throwable, E4 extends Throwable> void tryWithMDC(Object usingObject, MDCHelper.CheckedRunnable4<E1,E2,E3,E4> runnable) throws E1, E2, E3, E4
      Try and invoke a runnable with MDC
      Parameters:
      usingObject - the object using the MDC, for which a supplier may have been provided
      runnable - the runnable (without result)
      Throws:
      E - exception in runnable
      E1 extends Throwable
      E2 extends Throwable
      E3 extends Throwable
      E4 extends Throwable