问题
I'm trying to migrate a JEE service to Quarkus and wonder how to obtain a thread factory in a Quarkus app. Simply create one like javaExecutors.defaultThreadFactory();
as in JavaSE?
In a Java EE environment you would normally use a managed thread factory for creating threads for execution:
@Resource
private ManagedThreadFactory mtf;
Any idea how to do this correctly within a Quarkus app?
Addition: Using a ManagedExecutor is unfortunately not possible as some libraries like Apache HttpAsyncClient requires a ThreadFactory for it's configuration.
回答1:
Unless you have a special use-case that requires creating actual Threads, I would recommend using an Executor
instead of a ThreadFactory
. This is typically better because you can submit lightweight work objects (Runnable
/Callable
/etc) to an Executor and it will run on the Executor's thread pool (which is managed by Quarkus), as opposed to creating heavyweight threads.
Quarkus provides support for MicroProfile Context Propagation, which is basically an extension of Java EE Concurrency. To use it, you can inject a ManagedExecutor
like this:
import org.eclipse.microprofile.context.ManagedExecutor;
// ...
@Inject
ManagedExecutor exec;
来源:https://stackoverflow.com/questions/59442653/howto-obtain-a-threadfactory-in-quarkus