java.net.http.HttpClient inside Kotlin Suspend Function: Unresolved reference: await

3.1k views Asked by At

I am trying to code Http Request with Kotlin Coroutines as explained in

this guide and that other guide

As far as I can see, my code is quite close to both example:

package com.tolearn.service

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.http.HttpResponse.BodyHandlers
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton


@Singleton
class DemoService {

    fun postDemo(key: String, msg: String){

        suspend fun getCoroutine(){
    
            val someData = getData()
    
            print(someData)
        }
    
        suspend fun getData(): String {
    
    
            val client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_2)
                    .build();
    
            val request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:3000/employees"))
                    .build();
    
            val response = client.sendAsync(request, BodyHandlers.ofString());
    
            return response.await().body() ***here I get Unresolved reference: await
            //return response.get().body() ***this line works but I understand I should prefer use previous line with "awai()" instead of "get()"
        }
    }
}

My final goal is expose an endpoint (Controller) which will call another Endpoint throw java.net.http.HttpClient inside of a Suspend function. My Microservice is totally stateless and nedd to be faster and less heavy as possible. It is my first time coding Kotlin Coroutine. Based on theorical study, I understand Coroutine is cheaper than thread and I did a good choice for Coroutine. Nevertheless, I am new to it and am struggling with doubts like Error/Exceptions handle and, more immedially why I am getting Unresolved reference: await

So my straight question is: why return response.await().body() caused Unresolved reference: await? Should I replace by response.get().body()?

Any other suggestion or tip will be highly appreciatted.

*** edit

here is my build.gradle. I have just added kotlinx-coroutines-jdk8:1.4.2

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
    id("com.google.protobuf") version "0.8.13"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.grpc:micronaut-grpc-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut.kafka:micronaut-kafka")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.2")

    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    testImplementation("io.micronaut:micronaut-http-client")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

sourceSets {
    main {
        java {
            srcDirs("build/generated/source/proto/main/grpc")
            //srcDirs 'build/generated/source/proto/main/grpckt'
            srcDirs("build/generated/source/proto/main/java")
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.14.0" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.33.1" }
        //grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0" }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            //grpckt {}
        }
    }
}
1

There are 1 answers

0
Rubydesic On BEST ANSWER

Ensure you have the kotlinx-coroutines-jdk8 library on your classpath, and then use the following import statement:

import kotlinx.coroutines.future.await