xyk blog

最近は iOS 開発の記事が多めです。

Vert.x で HelloWorld

環境:
Windows 7
JDK 1.7

Vert.x は多言語での実装をサポートしている。
現在デフォルトで使えるすべての言語で HelloWorld を書いて比べてみる。

インストール

ここからバイナリをダウンロードする。
http://vertx.io/downloads.html

現時点の最新である2.1M2をダウンロードしてパスを通した。
以下を Cygwin から実行。

$ mkdir /cygdrive/c/vertx && cd /cygdrive/c/vertx
$ wget http://dl.bintray.com/vertx/downloads/vert.x-2.1M2.tar.gz
$ tar zxvf vert.x-2.1M2.tar.gz
$ ln -s vert.x-2.1M2 vert.x
$ export PATH=/cygdrive/c/vertx/vert.x/bin:$PATH

Vert.x 実行

$ vertx version
2.1M2 (built 2013-12-01 13:02:34)

JavaScript で HelloWorld

var vertx = require('vertx');

vertx.createHttpServer().requestHandler(function(req) {
    req.response.end("Hello World!");
}).listen(8080);

起動

$ vertx run server.js
Downloading io.vertx~lang-rhino~2.0.0-final. Please wait...
Downloading 100%
Module io.vertx~lang-rhino~2.0.0-final successfully installed
Succeeded in deploying verticle

確認
http://localhost:8080/

Java で HelloWorld

import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;

public class Server extends Verticle {
    public void start() {
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest req) {
                req.response().end("Hello World!");
            }
        }).listen(8080);
    }
}

起動

$ vertx run Server.java 

.java のままコンパイルせずに実行できる。

Groovy で HelloWorld

vertx.createHttpServer().requestHandler { req ->
    req.response.end "Hello World!"
}.listen(8080)

起動

$ vertx run Server.groovy

Ruby で HelloWorld

require "vertx"

Vertx::HttpServer.new.request_handler do |req|
    req.response.end "Hello World!"
end.listen(8080)

起動

$ vertx run server.rb
Downloading io.vertx~lang-jruby~2.0.0-final. Please wait...
Downloading 100%
Module io.vertx~lang-jruby~2.0.0-final successfully installed
Succeeded in deploying verticle

Python で HelloWorld

import vertx

server = vertx.create_http_server()

@server.request_handler
def request_handler(req):
    req.response.end("Hello World!")
server.listen(8080)

起動

$ vertx run server.py
Downloading io.vertx~lang-jython~2.0.0-final. Please wait...
Downloading 100%
Module io.vertx~lang-jython~2.0.0-final successfully installed
Succeeded in deploying verticle