Package io.camunda.zeebe.util.jar
Class ThreadContextUtil
java.lang.Object
io.camunda.zeebe.util.jar.ThreadContextUtil
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic <V> VcallWithClassLoader(Callable<V> callable, ClassLoader classLoader) Executes the givencallable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.static voidrunCheckedWithClassLoader(CheckedRunnable runnable, ClassLoader classLoader) Executes the givenrunnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.static voidrunWithClassLoader(Runnable runnable, ClassLoader classLoader) Executes the givenrunnable, swapping the thread context class loader for the given class loader, and swapping it back with the previous class loader afterwards.
-
Constructor Details
-
ThreadContextUtil
public ThreadContextUtil()
-
-
Method Details
-
runWithClassLoader
Executes the givenrunnable, 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 executeclassLoader- 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 givenrunnable, 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 executeclassLoader- 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 givencallable, 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 executeclassLoader- the class loader to temporarily assign to the current thread's context class loader- Throws:
Exception
-