Skip to content

Commit

Permalink
Preserve TCCL when using coroutines in RESTEasy Reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jun 1, 2021
1 parent 56b3d65 commit 1c1177a
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.vertx.core.Vertx
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.eclipse.microprofile.context.ManagedExecutor
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext
import org.jboss.resteasy.reactive.server.spi.EndpointInvoker
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler
Expand All @@ -15,6 +14,9 @@ private val logger = LoggerFactory.getLogger(CoroutineInvocationHandler::class.j
class CoroutineInvocationHandler(private val invoker: EndpointInvoker,
private val coroutineScope: CoroutineScope) : ServerRestHandler {

private val originalTCCL: ClassLoader = Thread.currentThread().contextClassLoader


override fun handle(requestContext: ResteasyReactiveRequestContext) {
if (requestContext.result != null) {
return
Expand All @@ -32,6 +34,8 @@ class CoroutineInvocationHandler(private val invoker: EndpointInvoker,

requestContext.suspend()
coroutineScope.launch(context = dispatcher) {
// ensure the proper CL is not lost in dev-mode
Thread.currentThread().contextClassLoader = originalTCCL
try {
requestContext.result = invoker.invokeCoroutine(requestContext.endpointInstance, requestContext.parameters)
} catch (t: Throwable) {
Expand Down

0 comments on commit 1c1177a

Please sign in to comment.