perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Running port() returning 0 if called to quickly

weeryan17 opened this issue · comments

When trying to get the port spark is actually listening on with the port being set to 0, it can return 0 if called too quickly.
This seams to be because the server is initializing in a separate thread and therefor not setting the port number to the actual port number before calling port().
See example

public int initWeb() {
    port(0);
    get("/thing", (req, res) -> ...);
    return port(); // returns 0
}

There also seams to be no way to check for the server being fully initialized as far as I can tell, but I could be wrong about that.

Try:
Spark.awaitInitialization();