-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a custom QuarkusForkJoinWorkerThread to controll the classl…
…oader in the Fork/Join common pool
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
core/deployment/src/main/java/io/quarkus/deployment/ForkJoinPoolProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.quarkus.deployment; | ||
|
||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.SystemPropertyBuildItem; | ||
|
||
public final class ForkJoinPoolProcessor { | ||
|
||
@BuildStep | ||
SystemPropertyBuildItem setProperty() { | ||
return new SystemPropertyBuildItem("java.util.concurrent.ForkJoinPool.common.threadFactory", | ||
"io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...strap/runner/src/main/java/io/quarkus/bootstrap/forkjoin/QuarkusForkJoinWorkerThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.bootstrap.forkjoin; | ||
|
||
import io.quarkus.bootstrap.runner.RunnerClassLoader; | ||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.ForkJoinWorkerThread; | ||
|
||
public class QuarkusForkJoinWorkerThread extends ForkJoinWorkerThread { | ||
|
||
private static volatile ClassLoader qClassloader; | ||
|
||
protected QuarkusForkJoinWorkerThread(ForkJoinPool pool) { | ||
super(pool); | ||
} | ||
|
||
public static synchronized void setQuarkusAppClassloader(RunnerClassLoader runnerClassLoader) { | ||
if (qClassloader != null) { | ||
throw new IllegalStateException("Attempting to set the Quarkus root classloader while it was already set"); | ||
} | ||
if (runnerClassLoader == null) { | ||
throw new IllegalStateException("Attempting to set the Quarkus root classloader to null"); | ||
} | ||
qClassloader = runnerClassLoader; | ||
} | ||
|
||
protected void onStart() { | ||
super.onStart(); | ||
if (qClassloader == null) { | ||
throw new IllegalStateException( | ||
"Fork Join pool initialization has been triggered before the application classloader has been initialized. " | ||
+ | ||
"Use this stacktrace to figure out what triggered it, and avoid using the Fork Join pool this early during the boot process."); | ||
} else { | ||
super.setContextClassLoader(qClassloader); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...unner/src/main/java/io/quarkus/bootstrap/forkjoin/QuarkusForkJoinWorkerThreadFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.bootstrap.forkjoin; | ||
|
||
import java.util.concurrent.ForkJoinPool; | ||
import java.util.concurrent.ForkJoinWorkerThread; | ||
|
||
/** | ||
* This implementation of JDK's ForkJoinPool.ForkJoinWorkerThreadFactory | ||
* needs to be enabled by setting system property {@code java.util.concurrent.ForkJoinPool.common.threadFactory} | ||
* to this class name, so to allow Quarkus to set the contextual classloader to the correct | ||
* runtime classloader for each thread being started by the common pool. | ||
* Otherwise the system classloader will be set, which is unable to load all application resources. | ||
*/ | ||
public final class QuarkusForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { | ||
|
||
public QuarkusForkJoinWorkerThreadFactory() { | ||
} | ||
|
||
@Override | ||
public ForkJoinWorkerThread newThread(ForkJoinPool pool) { | ||
return new QuarkusForkJoinWorkerThread(pool); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters