Michael
Michael

Reputation: 111

Howto obtain a ThreadFactory in Quarkus?

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.

Upvotes: 5

Views: 5623

Answers (1)

Andy Guibert
Andy Guibert

Reputation: 42926

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;

Upvotes: 4

Related Questions