Home > Net >  Running two servers in java Blade Framework
Running two servers in java Blade Framework

Time:10-09

I am using Java Blade Framework.

I'm trying to do that each server listens on its own port. However, when running two (or more) servers, only the first one works.

When I try to open the http://localhost:8082/world in browser (server 2), it looks for the necessary route in the first server (I found this out using logging). And I get 404 error. If I swap the start of the servers, then all paths will be searched in the first one.

This is my code:

public class App {
    public static void main(String[] args) {
        Blade.create()
            .get("/hello", ctx -> ctx.text("hello!"))
            .listen(8081)
            .start()
            .await();

        Blade.create()
            .get("/world", ctx -> ctx.text("world!"))
            .listen(8082)
            .start()
            .await();

        // http://localhost:8081/hello -> 202 ok
        // http://localhost:8082/world -> 404 not found -> 
        // -> searches path "/world" in the first server
    }
}

How can I start two servers?

CodePudding user response:

Thanks to Ilya Lapitan for giving complete answer to the question in the comments.

I will also add that I was able to run 2 servers in the application using the Javalin library. The code style is very similar to Blade.

public class Application {
    public static void main(String[] args) {
        Javalin.create()
            .get("/hello", ctx -> ctx.result("hello!"))
            .start(8080);

        Javalin.create()
            .get("/world", ctx -> ctx.result("world!"))
            .start(8081);
    }
}
  • Related