Howto obtain a ThreadFactory in Quarkus?

若如初见. 提交于 2021-01-03 07:11:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!