Max
11/15/2024, 12:07 PMimport io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.delay
import java.util.concurrent.ConcurrentHashMap
private val testMap = ConcurrentHashMap<String, String>()
private var counter = 1
suspend fun main() {
testMap["MS"] = "1"
println("testMap from main setup: $testMap")
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::myTestApp).start(wait = false)
while (true) {
testMap["M${counter++}"] = "1"
println("testMap from Main: $testMap")
delay(3000)
}
}
fun Application.myTestApp() {
testMap["RS"] = "1"
println("testMap from routing setup: $testMap")
routing {
get("/") {
testMap["R" + counter++] = "1"
println("testMap from routing: $testMap")
call.respond(testMap)
}
}
}
Gives the output:
testMap from main setup: {MS=1}
testMap from routing setup: {RS=1}
13:04:51.102 │ main INFO ktor.application │ Application started in 0.775 seconds.
13:04:51.284 │ main INFO ktor.application │ Responding at <http://0.0.0.0:8080>
testMap from Main: {M1=1, MS=1}
testMap from Main: {M1=1, M2=1, MS=1}
testMap from routing: {RS=1, R1=1}
testMap from routing: {R2=1, RS=1, R1=1}
testMap from routing: {R2=1, RS=1, R3=1, R1=1}
testMap from Main: {M1=1, M2=1, MS=1, M3=1}
When my colleague runs the same code it does not show the same issue. For him the code adds entries in the same map. Running Mac M2 Pro with java 21 Corretto and Kotlin 2.0.21, Ktor: 2.3.11 How can this be and how do i fix it?Max
11/15/2024, 12:17 PMAleksei Tirman [JB]
11/15/2024, 12:22 PMMax
11/15/2024, 12:27 PMval ktor_version: String by project
plugins {
application
kotlin("jvm") version "2.0.21"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.23"
}
group = "se.svt"
version = "0.0.1"
application {
mainClass.set("se.svt.videocollab.gissa.TestAppKt")
}
val javaVersion = 21
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(javaVersion))
}
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlin {
jvmToolchain {
(this as JavaToolchainSpec).languageVersion.set(JavaLanguageVersion.of(javaVersion))
}
}
}
tasks {
compileKotlin {
kotlinOptions.jvmTarget = "$javaVersion"
}
}
repositories {
mavenCentral()
}
val jackson = "2.13.2"
dependencies {
implementation("io.ktor:ktor-server-core-jvm:$ktor_version")
implementation("io.ktor:ktor-server-netty-jvm:$ktor_version")
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-server-cors:$ktor_version")
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
implementation("io.ktor:ktor-client:$ktor_version")
implementation("io.ktor:ktor-client-cio-jvm:2.3.11")
implementation("org.bytedeco:javacv-platform:1.5.10")
implementation("org.bytedeco:javacpp-platform:1.5.10")
implementation("org.bytedeco:opencv-platform:4.9.0-1.5.10")
implementation("org.bytedeco:ffmpeg-platform-gpl:6.1.1-1.5.10")
implementation("org.bytedeco:openblas-platform:0.3.26-1.5.10")
implementation("io.github.crackthecodeabhi:kreds:0.9.1")
testImplementation(kotlin("test"))
implementation("org.slf4j:jul-to-slf4j:2.0.16")
implementation("ch.qos.logback:logback-classic") {
version {
strictly("1.5.8")
}
}
implementation("net.logstash.logback:logstash-logback-encoder:7.0.1")
implementation("ch.qos.logback.contrib:logback-json-classic:0.1.5")
implementation("ch.qos.logback.contrib:logback-jackson:0.1.5")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jackson")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jackson")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jackson")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-smile:$jackson")
implementation("org.apache.kafka:kafka-clients:3.4.0")
implementation("com.google.guava:guava:31.1-jre")
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
Aleksei Tirman [JB]
11/15/2024, 12:29 PMMax
11/15/2024, 12:31 PMAleksei Tirman [JB]
11/15/2024, 12:32 PMMax
11/15/2024, 12:37 PMMax
11/15/2024, 12:44 PMMax
11/15/2024, 12:52 PM