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.
Ensure Flow return type works with Request Scope in RESTEasy Reactive
Fixes: quarkusio#18993
- Loading branch information
Showing
5 changed files
with
38 additions
and
11 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
23 changes: 20 additions & 3 deletions
23
...c/main/kotlin/org/jboss/resteasy/reactive/server/runtime/kotlin/FlowToPublisherHandler.kt
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 |
---|---|---|
@@ -1,17 +1,34 @@ | ||
package org.jboss.resteasy.reactive.server.runtime.kotlin | ||
|
||
import io.smallrye.mutiny.coroutines.asMulti | ||
import io.vertx.core.Vertx | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.reactive.asPublisher | ||
import kotlinx.coroutines.launch | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler | ||
import javax.enterprise.inject.spi.CDI | ||
|
||
class FlowToPublisherHandler : ServerRestHandler { | ||
|
||
private val originalTCCL: ClassLoader = Thread.currentThread().contextClassLoader | ||
|
||
override fun handle(requestContext: ResteasyReactiveRequestContext?) { | ||
val result = requestContext!!.result | ||
if (result is Flow<*>) { | ||
requestContext.result = (result as Flow<Any>) // cast needed for extension function | ||
.asPublisher() | ||
|
||
val requestScope = requestContext.captureCDIRequestScope() | ||
val dispatcher: CoroutineDispatcher = Vertx.currentContext()?.let {VertxDispatcher(it,requestScope)} | ||
?: throw IllegalStateException("No Vertx context found") | ||
|
||
val coroutineScope = CDI.current().select(ApplicationCoroutineScope::class.java) | ||
requestContext.suspend() | ||
coroutineScope.get().launch(context = dispatcher) { | ||
// ensure the proper CL is not lost in dev-mode | ||
Thread.currentThread().contextClassLoader = originalTCCL | ||
requestContext.result = result.asMulti() | ||
requestContext.resume() | ||
} | ||
} | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
...otlin/standard/src/main/kotlin/io/quarkus/it/resteasy/reactive/kotlin/UppercaseService.kt
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,10 @@ | ||
package io.quarkus.it.resteasy.reactive.kotlin | ||
|
||
import java.util.Locale | ||
import javax.enterprise.context.RequestScoped | ||
|
||
@RequestScoped | ||
class UppercaseService { | ||
|
||
fun convert(input: String) = input.uppercase(Locale.ROOT) | ||
} |
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