Vert.x 3.1.0 Tutorial – 1 : Hello World
2015年11月7日
Vert.xの紹介は本家サイト(http://vertx.io/)に譲ってとりあえずHello Word!
まず、今回のソースコードはこちら
0.開発環境
- Mac OS X
- java 1.8 以上
- IntelliJ 14.
1 : IntelliJでmaven新規プロジェクト作成
- File ->New -> Project
- Mavenを選択しNext

- maven project 設定
- GroupId : totorial.vertx
- ArtifactId : hello-verticle
- Version : 1.0-SNAPSHOT
- Project name / location
- name : hello-virticle
- location : すきなところへ
2 : pom.xml に依存関係を追加
以下の依存関係およびbuildをpom.xml のversionの下に追加
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Maven Project Windowより[Reimport All Maven Projects]を実行
3.Hello Verticle
HelloVerticle 作成
package tutorial.vertx;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
/**
* Created by bhags on 15/11/07.
*/
public class HelloVerticle extends AbstractVerticle {
@Override
public void start(Future<Void> fut) {
vertx
.createHttpServer()
.requestHandler(r -> {
r.response().end(
"<h1>Hello Verticle!!! "</h1>");
})
.listen(8080, result -> {
if (result.succeeded()) {
fut.complete();
} else {
fut.fail(result.cause());
}
});
}
}
まず、HelloVerticleは AbstractVerticleを継承している。Vert.xの世界では 全てのverticleはコンポーネントである。AbstractVerticleを継承することでHelloVerticleよりvertxフィールドを取得することができる。
startメソッドはverticleがデプロイされると呼び出される。stopメソッドの実装も必要だがここではVert.xが代わりにGCを行ってくれる。また、startメッソドはFutureオブジェクトを受け取る。Futureメッソドはstartシーケンスの無事終わったか、エラーが発生したかなどをVert.x側に教えられる。Vert.xの特徴の一つとして非同期/non-blockingがある。verticleがデプロイされるとverticleはstart methodの終了を待たない。だからFutureパラメータの終了通知が大事である。
このHelloVerticleのstartではHTTPサーバーを生成しリクエストハンドラーをアタッチしている。request handlerはlambda式でrequestHandlerへ伝達されている。最後にサーバーは8080ポートにバウンドされポートのバウンドに問題なければfut.complete()を問題があれば(8080が使われているなど)fut.fail(失敗)を通知する。
4:Create fatjar
pom.xmlに以下ソースを追加
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>tutorial.vertx.HelloVerticle</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
Run/Debug Configurationsでmaven実行configurationを追加 (Command line : clean package)
5:結果確認
targetに生成されたhello-verticle-1.0-SNAPSHOT-fat.jarを実行
java -jar /path/to/fatjar/hello-verticle-1.0-SNAPSHOT-fat.jar *以下が表示されれば 11 07, 2015 10:31:44 午後 io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer 情報: Succeeded in deploying verticle
http://127.0.0.1:8080へアクセス後以下が表示されればOK
Hello Verticle!!!


Comments are closed.