6

我用 main(args) 编写了一个 Groovy MainApp。

当我启动它时,JVM 直接退出(“结束 JVM 执行!”)。

import org.vertx.groovy.core.Vertx

class MainApp {

    public static void main(String[] args) {

        Vertx vertx = VertxFactory.newVertx();

        vertx.createHttpServer().requestHandler{ request -> 
            println "A request has arrived on the server!" 
        }.listen(8080)

        println "End of JVM execution !"
    }
}

如何正确运行带有 vert.x 的嵌入式HTTP 服务器?

4

5 回答 5

2

I had the same problem with Java. I ended up putting an object in .wait() after all the vert.x code. Looks horrible, but actually makes sense because it gives me a trigger to shutdown the server on demand (via .notify()).

This is non trivial, should be mentioned on the Vert.x official documentation.

于 2013-02-01T18:01:48.760 回答
0

从文档:

all Vert.x threads are daemon threads and they will not prevent the JVM for exiting. Therefore you must ensure that the main() method does not run to completion, e.g. in this example we have used System.in.read() to block the main thread waiting for IO from the console.

因此,您需要在 main 方法的末尾添加以下内容,如下所示:

// Prevent the JVM from exiting
System.in.read();
于 2015-03-02T07:28:39.327 回答
0

取自文档: http: //vertx.io/embedding_manual.html

只需等待 System.in 的输入:

// Prevent the JVM from exiting
System.in.read();
于 2014-08-31T07:52:27.097 回答
0

我遇到了这个。我尝试传递主机名,然后它工作正常。

Vertx vertx = Vertx.newVertx( "主机名" )

我想在本地运行时确定 IP 地址存在一些问题,但它失败了。

于 2013-03-20T07:13:28.427 回答
0
RouteMatcher routeMatcher = new RouteMatcher();

        // HTTP server
        HttpServer httpServer = vertx.createHttpServer();
        httpServer.requestHandler(routeMatcher);
httpServer.listen(7777);
于 2013-11-13T18:16:24.217 回答