https://kotlinlang.org logo
Title
d

Daniel

05/09/2023, 3:26 PM
Hi. I am creating
ApplicationEngineEnvironment
with multiple
sslConnector
calls on different ports, plus one connector on
localhost
with port set to 0 (ie. let it select random available port). I know from here that I can use
resolvedConnectors()
function to determine what port was selected, however I also want to use the`start(wait = true)` . Is there a way to call start without waiting, get the selected port value and then wait in the same way as it would with
start(wait = true)
?
a

Aleksei Tirman [JB]

05/10/2023, 9:16 AM
You can start a server without blocking a thread to resolve connectors and then block it manually. Here is example:
val engine = embeddedServer(Netty, port = 0) {}

val serverJob = CompletableDeferred<Unit>()
engine.application.environment.monitor.subscribe(ApplicationStopped) {
    serverJob.complete(Unit)
}

engine.start(wait = false)

val port = engine.resolvedConnectors().first().port
println(port)

serverJob.await()
d

Daniel

05/10/2023, 9:19 AM
Thank you 🙂 Is this functionally the same what ktor would do under the hood?
a

Aleksei Tirman [JB]

05/10/2023, 9:19 AM
No