问题描述
我正在尝试将 JEE 服务迁移到 Quarkus 并想知道如何在 Quarkus 中获取线程工厂应用程序.简单地创建一个像 javaExecutors.defaultThreadFactory();
在 JavaSE 中?
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?
在 Java EE 环境中,您通常会使用托管线程工厂来创建要执行的线程:
In a Java EE environment you would normally use a managed thread factory for creating threads for execution:
@Resource
private ManagedThreadFactory mtf;
知道如何在 Quarkus 应用程序中正确执行此操作吗?
Any idea how to do this correctly within a Quarkus app?
补充:不幸的是,无法使用 ManagedExecutor,因为某些库(如 Apache HttpAsyncClient)需要 ThreadFactory 来进行配置.
Addition: Using a ManagedExecutor is unfortunately not possible as some libraries like Apache HttpAsyncClient requires a ThreadFactory for it's configuration.
推荐答案
除非您有需要创建实际线程的特殊用例,否则我建议使用 Executor
而不是 线程工厂
.这通常更好,因为您可以将轻量级工作对象(Runnable
/Callable
/etc)提交给 Executor,它将在 Executor 的线程池(由 Quarkus 管理)上运行),而不是创建重量级线程.
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 提供对 MicroProfile Context Propagation 的支持,它基本上是 Java EE Concurrency 的扩展.要使用它,您可以像这样注入 ManagedExecutor
:
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;
这篇关于如何在 Quarkus 中获取 ThreadFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!