现在是编写第一个Verticle代码的时候了。修改src/main/java/io/vertx/sample/MyFirstVerticle.java文件,内容如下:

import io.vertx.core.AbstractVerticle;
/**
* A verticle extends the AbstractVerticle class.
*/
public class MainVerticle extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        vertx.createHttpServer()
        // The requestHandler is called for each incoming
        // HTTP request, we print the name of the thread
        .requestHandler(req -> {
            req.response()
                    .putHeader("content-type", "text/plain")
                    .end("Hello from Vert.x!");
        }).listen(8080);
        System.out.println("HTTP server started on port 8080"); //// start the server on port 8080
    }

}

运行这个应用:

mvn compile vertx:run

如果一切顺利,您应该可以通过在浏览器中打开http://localhost:8080来查看应用程序。vertx:run目标是启动这个Vert.x应用程序并观察代码的更改。因此,如果编辑了源代码,应用程序将自动重新编译并重新启动。

现再我们来看看应用启动的输出:

Hello from vert.x-eventloop-thread-0

请求已被 event loop 0.处理。您可以尝试发出更多的请求。请求将始终由相同的事件回环处理,强制执行Vert.x的并发模型。按Ctrl+C停止执行。

results matching ""

    No results matching ""