forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIO async IO uses a custom thread pool, this allows us to update the TCCL in dev mode. Fixes quarkusio#20973
- Loading branch information
1 parent
834ed9d
commit 6c708fe
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
.../deployment/src/main/java/io/quarkus/deployment/dev/io/NioThreadPoolDevModeProcessor.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,21 @@ | ||
package io.quarkus.deployment.dev.io; | ||
|
||
import io.quarkus.deployment.IsNormal; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.ExecutionTime; | ||
import io.quarkus.deployment.annotations.Produce; | ||
import io.quarkus.deployment.annotations.Record; | ||
import io.quarkus.deployment.builditem.ShutdownContextBuildItem; | ||
import io.quarkus.deployment.dev.testing.TestSetupBuildItem; | ||
import io.quarkus.runtime.dev.io.NioThreadPoolRecorder; | ||
|
||
public class NioThreadPoolDevModeProcessor { | ||
|
||
@Produce(TestSetupBuildItem.class) | ||
@BuildStep(onlyIfNot = IsNormal.class) | ||
@Record(ExecutionTime.STATIC_INIT) | ||
void setupTCCL(NioThreadPoolRecorder recorder, ShutdownContextBuildItem shutdownContextBuildItem) { | ||
recorder.updateTccl(shutdownContextBuildItem); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
core/devmode-spi/src/main/java/io/quarkus/dev/io/NioThreadPoolThreadFactory.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,45 @@ | ||
package io.quarkus.dev.io; | ||
|
||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* We need to be able to update the TCCL of the NIO default thread pool in dev/test mode | ||
* <p> | ||
* This class lets us to that. | ||
*/ | ||
public class NioThreadPoolThreadFactory implements ThreadFactory { | ||
|
||
private static final CopyOnWriteArrayList<Thread> allThreads = new CopyOnWriteArrayList<>(); | ||
private static volatile ClassLoader currentCl = Thread.currentThread().getContextClassLoader(); | ||
|
||
@Override | ||
public Thread newThread(Runnable r) { | ||
AtomicReference<Thread> t = new AtomicReference<>(); | ||
Thread thread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
r.run(); | ||
} finally { | ||
allThreads.remove(t.get()); | ||
} | ||
} | ||
}, "NIO IO Thread (created by Quarkus)"); | ||
t.set(thread); | ||
thread.setDaemon(true); | ||
allThreads.add(thread); | ||
thread.setContextClassLoader(currentCl); | ||
return thread; | ||
} | ||
|
||
public static ClassLoader updateTccl(ClassLoader cl) { | ||
ClassLoader old = currentCl; | ||
currentCl = cl; | ||
for (Thread i : allThreads) { | ||
i.setContextClassLoader(cl); | ||
} | ||
return old; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/runtime/src/main/java/io/quarkus/runtime/dev/io/NioThreadPoolRecorder.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,19 @@ | ||
package io.quarkus.runtime.dev.io; | ||
|
||
import io.quarkus.dev.io.NioThreadPoolThreadFactory; | ||
import io.quarkus.runtime.ShutdownContext; | ||
import io.quarkus.runtime.annotations.Recorder; | ||
|
||
@Recorder | ||
public class NioThreadPoolRecorder { | ||
|
||
public void updateTccl(ShutdownContext context) { | ||
ClassLoader old = NioThreadPoolThreadFactory.updateTccl(Thread.currentThread().getContextClassLoader()); | ||
context.addLastShutdownTask(new Runnable() { | ||
@Override | ||
public void run() { | ||
NioThreadPoolThreadFactory.updateTccl(old); | ||
} | ||
}); | ||
} | ||
} |