やっつけ不定記

好きなときに好きなことをちゃっちゃと書いてます

WildFlyでServer Side JavaScriptをやってみた

chocopurin2015-11-29

ちょっと前に書いたWildFlyでServer Side JavaScript
週末を使って試してみたところ、公式ブログ
http://wildfly.org/news/2015/08/10/Javascript-Support-In-Wildfly/
のHelloWorldを実際に動かすところまでいけたっぽいです。
もっといいやり方があると思いますが、気にせずメモってみます。


【環境】
・OS:Linux Mint 17.2
・APサーバ:WildFly 10 CR4
JDKMaven:下記参照。WildFlyの仕様上、JDKは1.8以上が必須。事前にPATHやJAVA_HOMEは設定済み。
$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T01:41:47+09:00)
Maven home: (mavenのインストールディレクトリ)
Java version: 1.8.0_66, vendor: Oracle Corporation
Java home: (JDKのインストールディレクトリ)/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-24-generic", arch: "amd64", family: "unix"


【手順】
(1)MavenのWebアプリプロジェクトを作る。
プロジェクト名は「testpj」としました。
$ mvn archetype:generate -DgroupId=testpj -DartifactId=testpj -DarchetypeArtifactId=maven-archetype-webapp

[INFO] Using property: artifactId = testpj
Define value for property 'version': 1.0-SNAPSHOT: : (Enterキー)
[INFO] Using property: package = testpj
Confirm properties configuration:
groupId: testpj
artifactId: testpj
version: 1.0-SNAPSHOT
package: testpj
Y: : (Enterキー)

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------


(2)undertow-scripts.confの作成
実行するJavaScriptファイルを設定するようです。ここではhello.jsとしてみました。
$ vi (testpjのディレクトリ)/src/main/webapp/WEB-INF/undertow-scripts.conf
$ cat (testpjのディレクトリ)/src/main/webapp/WEB-INF/undertow-scripts.conf
hello.js


(3)hello.jsの作成
実際にJavaScriptファイルを書きます。
$ vi (testpjのディレクトリ)/src/main/webapp/hello.js
公式ブログにある「A simple HTTP endpoint」のtext/plain版をそのままコピーペースト。
リターンメッセージだけ少し変えました。


(4)プロジェクトのビルド
$ cd (testpjのディレクトリ)
$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testpj Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

$ ls (testpjのディレクトリ)/target/testpj
META-INF WEB-INF hello.js index.jsp
$ ls (testpjのディレクトリ)/target/testpj/WEB-INF
classes undertow-scripts.conf web.xml
$ ls (testpjのディレクトリ)/target
classes maven-archiver testpj testpj.war


(5)WildFly起動
$ (WildFlyのインストールディレクトリ)/bin/standalone.sh


(6)WildFlyにアプリをデプロイ
$ (WildFlyのインストールディレクトリ)/bin/jboss-cli.sh
You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands.
[disconnected /] connect
[standalone@localhost:9990 /] deploy testpj.war
デプロイすると、WildFlyのログがこんな感じになる。

22:01:58,781 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0027: Starting deployment of "testpj.war" (runtime-name: "testpj.war")
22:02:01,586 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 63) WFLYUT0021: Registered web context: /testpj
22:02:01,682 INFO [org.jboss.as.server] (management-handler-thread - 2) WFLYSRV0010: Deployed "testpj.war" (runtime-name : "testpj.war")


(7)動作確認
ブラウザで
http://localhost:8080/testpj/hello
にアクセスする。


【その他】
同様のやり方で、公式ブログ内の他サンプルやリンクされているundertow.js-examplesも動きそうな気がします。
日本語表示も試してみたのですが、自分の環境では文字化けしてうまく行きませんでした。
文字コード設定を何かしらしなければいけないのかもしれません。


<戦利品>
いぶり暮らし(3)