Update 2025-06-23

This commit is contained in:
2025-06-23 12:10:11 +03:00
parent 42906788d6
commit 74ffa4f7f4
11 changed files with 93 additions and 9 deletions

View File

@@ -34,6 +34,9 @@ dependencies {
implementation(libs.ktor.server.call.logging)
implementation(libs.ktor.server.cors)
implementation(libs.ktor.server.host.common)
implementation(libs.ktor.server.auth)
implementation(libs.ktor.server.contentnegotiation)
implementation(libs.ktor.kotlinx.json)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.cio)

View File

@@ -19,6 +19,10 @@ ktor-server-call-logging = { module = "io.ktor:ktor-server-call-logging-jvm", ve
ktor-server-cors = { module = "io.ktor:ktor-server-cors-jvm", version.ref = "ktor-version" }
ktor-server-host-common = { module = "io.ktor:ktor-server-host-common-jvm", version.ref = "ktor-version" }
ktor-server-cio = { module = "io.ktor:ktor-server-cio-jvm", version.ref = "ktor-version" }
ktor-server-auth = { module = "io.ktor:ktor-server-auth", version.ref = "ktor-version" }
ktor-server-contentnegotiation = { module = "io.ktor:ktor-server-content-negotiation", version.ref = "ktor-version" }
ktor-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor-version" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback-version" }
ktor-server-test-host = { module = "io.ktor:ktor-server-test-host-jvm", version.ref = "ktor-version" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin-version" }

View File

@@ -193,6 +193,10 @@
"hide_output_from_viewers": true,
"node_id": "r1yVVtgMhexxzx7gGVmeIg",
"type": "CODE"
},
"ExecuteTime": {
"end_time": "2025-06-16T08:57:43.828698500Z",
"start_time": "2025-06-16T08:57:43.558816900Z"
}
},
"source": [
@@ -203,7 +207,7 @@
"}"
],
"outputs": [],
"execution_count": null
"execution_count": 1
},
{
"cell_type": "code",
@@ -213,6 +217,10 @@
"hide_output_from_viewers": true,
"node_id": "8YvIKfD1I7cfbiPRMpk0DC",
"type": "CODE"
},
"ExecuteTime": {
"end_time": "2025-06-16T09:00:29.663078800Z",
"start_time": "2025-06-16T09:00:29.162586700Z"
}
},
"source": [
@@ -228,8 +236,28 @@
"\n",
"future.get()"
],
"outputs": [],
"execution_count": null
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Task complete: 8\r\n",
"8\r\n",
"false\r\n"
]
},
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"execution_count": 2
},
{
"attachments": {},

View File

@@ -3,12 +3,15 @@ package examples
import kotlinx.coroutines.*
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
class ApplicationWithScope(
parentContext: CoroutineContext
parentContext: CoroutineContext = EmptyCoroutineContext
) : CoroutineScope, AutoCloseable {
override val coroutineContext: CoroutineContext = parentContext +
Executors.newSingleThreadExecutor().asCoroutineDispatcher() +
//Executors.newSingleThreadExecutor().asCoroutineDispatcher() +
newFixedThreadPoolContext(4, "ApplicationPool") +
SupervisorJob(parentContext[Job]) +
CoroutineExceptionHandler { _, exception -> } +
CoroutineName("ApplicationWithScope")

View File

@@ -1,6 +1,7 @@
package examples
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
@@ -25,4 +26,4 @@ suspend fun main(): Unit {
}
}.join()
}.also { println(it) }
}
}

View File

@@ -9,6 +9,8 @@ suspend fun main(): Unit = coroutineScope {
println(throwable)
}
) {
// withContext(SupervisorJob(coroutineContext[Job])){ }
supervisorScope {
val subJob = launch {
// println(coroutineContext[Job])
@@ -17,18 +19,20 @@ suspend fun main(): Unit = coroutineScope {
error("BOOM!")
}
}
val subDeferred = async {
delay(200)
// error("Boom2")
println("Task completed")
"Completed"
}
// delay(50)
// cancel()
// subJob.await()
println(subDeferred.await())
}
}

View File

@@ -1,6 +1,7 @@
package examples
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

View File

@@ -52,7 +52,7 @@ fun CoroutineScope.launchPriceUpdate(
val now = Clock.System.now()
val price = reader.readPrice()
launch(Dispatchers.IO) {
withContext (Dispatchers.IO) {
db.storePrice(now, price)
}
mutex.withLock {

View File

@@ -43,7 +43,7 @@ suspend fun main() = coroutineScope {
val body = response.bodyAsText()
val deserialized = Json.decodeFromString<OpenNotifyISSLocation>(body)
val deserialized = Json.decodeFromString<OpenNotifyISSLocation>(OpenNotifyISSLocation.serializer(), body)
println(deserialized.message)

View File

@@ -1,8 +1,15 @@
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
import io.ktor.server.auth.bearer
import io.ktor.server.cio.CIO
import io.ktor.server.engine.embeddedServer
import io.ktor.server.html.respondHtml
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.request.receiveText
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
@@ -13,6 +20,8 @@ import kotlinx.coroutines.newFixedThreadPoolContext
import kotlinx.coroutines.withContext
import kotlinx.html.body
import kotlinx.html.h1
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.coroutines.CoroutineContext
fun Route.subroute(key: String) {
@@ -36,11 +45,22 @@ suspend fun executeQuery(queryName: String, ioContext: CoroutineContext = Dispat
return queryName
}
@Serializable
data class HelloWorld(val hello: String, val name: String)
fun main(): Unit {
embeddedServer(CIO, port = 8080, host = "localhost") {
install(WebSockets)
install(ContentNegotiation) {
json(Json { prettyPrint = true })
}
// install(CallLogging)
install(Authentication) {
bearer {
// Configure bearer authentication
}
}
val ioContext = newFixedThreadPoolContext(12, "DB") //Dispatchers.IO
@@ -69,6 +89,14 @@ fun main(): Unit {
subroute("1")
}
val client = HttpClient()
get("complex") {
val res0 = client.get("http://localhost:8080/subroute/get")
val res1 = client.get("http://localhost:8080/subroute1/get")
call.respondText("${res0.bodyAsText()}|${res1.bodyAsText()}")
}
route("producer") {
streamingModule()
}
@@ -78,6 +106,18 @@ fun main(): Unit {
call.respondText(result.toString())
}
route("hello") {
get {
call.respond(HelloWorld("hello", "world"))
}
}
// authenticate("auth-bearer") {
// get("authenticated") {
// call.respondText("Authenticated")
// }
// }
}
}.start(true)
}