Class ThreadContextUtil

java.lang.Object
io.camunda.zeebe.util.jar.ThreadContextUtil

public final class ThreadContextUtil extends Object
A collection of utilities to run an arbitrary Runnable with a specific thread context class loader. This is required when side loading external code via the ExternalJarClassLoader, as that code may be using the Thread.getContextClassLoader().

As the same thread may be reused, it's also important to reset the thread afterwards to avoid operations being run on the wrong class loader.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V> V
    callWithClassLoader(Callable<V> callable, ClassLoader classLoader)
    Executes the given callable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
    static void
    Executes the given runnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
    static void
    runWithClassLoader(Runnable runnable, ClassLoader classLoader)
    Executes the given runnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
    static <V> V
    supplyWithClassLoader(Supplier<V> supplier, ClassLoader classLoader)
    Executes the given Supplier, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ThreadContextUtil

      public ThreadContextUtil()
  • Method Details

    • runWithClassLoader

      public static void runWithClassLoader(Runnable runnable, ClassLoader classLoader)
      Executes the given runnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
      Parameters:
      runnable - the operation to execute
      classLoader - the class loader to temporarily assign to the current thread's context class loader
    • runCheckedWithClassLoader

      public static void runCheckedWithClassLoader(CheckedRunnable runnable, ClassLoader classLoader) throws Exception
      Executes the given runnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.

      Use this method if you want your operation to throw exceptions; the class loader is guaranteed to be reset even if an exception is thrown.

      Parameters:
      runnable - the operation to execute
      classLoader - the class loader to temporarily assign to the current thread's context class loader
      Throws:
      Exception
    • callWithClassLoader

      public static <V> V callWithClassLoader(Callable<V> callable, ClassLoader classLoader) throws Exception
      Executes the given callable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
      Parameters:
      callable - the operation to execute
      classLoader - the class loader to temporarily assign to the current thread's context class loader
      Throws:
      Exception
    • supplyWithClassLoader

      public static <V> V supplyWithClassLoader(Supplier<V> supplier, ClassLoader classLoader)
      Executes the given Supplier, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
      Parameters:
      supplier - the operation to execute
      classLoader - the class loader to temporarily assign to the current thread's context class loader