blog podcast

Coroutines and Quarkus

Quarkus supports Coroutines from Kotlin, but it’s not a very documented feature. What you need to do is you need to include

    implementation 'io.quarkus:quarkus-resteasy-reactive'
    implementation 'io.quarkus:quarkus-resteasy-reactive-jackson'

instead of

    implementation 'io.quarkus:quarkus-resteasy'
    implementation 'io.quarkus:quarkus-resteasy-jackson'

That’s pretty much it.

Now you can use suspend in your controllers. Looking at the logs confirms that the execution occurs on the Vertx threads as one would have hoped and wished.

Coroutines are really a powerful tool to handle futures, giving you the same kind of revolution that async/await brought you in the Javascript world.

Code of the Day

Here’s an example using the coroutines in action:

@Path("/things")
class ThingController {

    val logger = Logger.getLogger(this.javaClass)

    @Inject
    lateinit var thingService: ThingService

    @POST
    suspend fun createAd(request: CreateThingRequestDto) : CreateThingResponseDto {
        logger.debugf("Create thing request recieved %s", request)
        return thingService.requestThing(request)
    }
}