add coroutines examples
This commit is contained in:
parent
edb92aa1a1
commit
f90e4f882a
20
build.gradle.kts
Normal file
20
build.gradle.kts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
plugins{
|
||||||
|
kotlin("jvm") version "2.0.20"
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories{
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
val ktorVersion = "3.0.1"
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0")
|
||||||
|
|
||||||
|
implementation("io.ktor:ktor-client-core:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-client-cio:$ktorVersion")
|
||||||
|
implementation("io.ktor:ktor-client-core-jvm:3.0.1")
|
||||||
|
implementation("io.ktor:ktor-client-apache:3.0.1")
|
||||||
|
|
||||||
|
implementation("ch.qos.logback:logback-classic:1.5.12")
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
34
src/main/kotlin/javaHttpClient.kt
Normal file
34
src/main/kotlin/javaHttpClient.kt
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import java.net.*
|
||||||
|
import java.net.http.*
|
||||||
|
import java.net.http.HttpResponse.*
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
val client: HttpClient = HttpClient.newHttpClient()
|
||||||
|
|
||||||
|
val request : HttpRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("https://sciprog.center"))
|
||||||
|
.GET().build()
|
||||||
|
|
||||||
|
val regex = """href=\"(.+\.png)\"""".toRegex()
|
||||||
|
|
||||||
|
client.sendAsync(request, BodyHandlers.ofString())
|
||||||
|
.thenApply{ it.body() }
|
||||||
|
.thenApply{
|
||||||
|
val resources = regex.findAll(it).map{it.groupValues[1]}
|
||||||
|
|
||||||
|
resources.map { resourceName->
|
||||||
|
println("Resource processing for $resourceName started")
|
||||||
|
val resourceRequest : HttpRequest = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("https://sciprog.center$resourceName"))
|
||||||
|
.GET().build()
|
||||||
|
client.sendAsync(resourceRequest, BodyHandlers.ofByteArray()).thenAccept{ resourceResponse ->
|
||||||
|
val bodyBytes = resourceResponse.body()
|
||||||
|
//do something with the body
|
||||||
|
println("The resource with name $resourceName has ${bodyBytes.size} bytes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resources
|
||||||
|
}
|
||||||
|
.thenAccept{ println(it.toList()) }
|
||||||
|
.join()
|
||||||
|
}
|
26
src/main/kotlin/ktorClient.kt
Normal file
26
src/main/kotlin/ktorClient.kt
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import io.ktor.client.HttpClient
|
||||||
|
import io.ktor.client.call.body
|
||||||
|
import io.ktor.client.engine.cio.CIO
|
||||||
|
import io.ktor.client.request.get
|
||||||
|
import io.ktor.client.statement.bodyAsBytes
|
||||||
|
import io.ktor.client.statement.bodyAsText
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
suspend fun main() = coroutineScope {
|
||||||
|
val client = HttpClient(CIO)
|
||||||
|
|
||||||
|
val regex = """href=\"(.+\.png)\"""".toRegex()
|
||||||
|
|
||||||
|
val initialResponse = client.get("https://sciprog.center")
|
||||||
|
val resources = regex.findAll(initialResponse.bodyAsText()).map { it.groupValues[1] }
|
||||||
|
resources.forEach { resourceName ->
|
||||||
|
launch {
|
||||||
|
println("Resource processing for $resourceName started")
|
||||||
|
val resourceResponse = client.get("https://sciprog.center$resourceName")
|
||||||
|
val bodyBytes: ByteArray = resourceResponse.bodyAsBytes()
|
||||||
|
//do something with the body
|
||||||
|
println("The resource with name $resourceName has ${bodyBytes.size} bytes")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user