WildFly: MySQLをデータソースとして利用 (CLI設定)
CLIツールにこだわって、WildFlyへのMySQLのドライバインストール、データソース追加までやってみます。
WildFlyのバージョンは8.1.0.Finalです。
ドライバのダウンロード
WildFlyにはドライバは標準ではついてこない(?)ため、ダウンロードしてきます。
$ cd $WILDFLY_HOME/bin $ curl -LO http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.31.tar.gz $ tar xvfz mysql-connector-java-5.1.31.tar.gz
ドライバのデプロイ
さきほどダウンロードしたドライバファイルをWildFlyにデプロイします。
$ ./jboss-cli.sh -c [standalone@localhost:9990 /] module add --name=org.mysql --resources=mysql-connector-java-5.1.31/mysql-connector-java-5.1.31-bin.jar --dependencies=javax.api,javax.transaction.api [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:add(driver-module-name=org.mysql,driver-name=mysql,driver-class-name=com.mysql.jdbc.Driver) {"outcome" => "success"} [standalone@localhost:9990 /] /subsystem=datasources/jdbc-driver=mysql:read-resource { "outcome" => "success", "result" => { "deployment-name" => undefined, "driver-class-name" => "com.mysql.jdbc.Driver", "driver-datasource-class-name" => undefined, "driver-major-version" => undefined, "driver-minor-version" => undefined, "driver-module-name" => "org.mysql", "driver-name" => "mysql", "driver-xa-datasource-class-name" => undefined, "jdbc-compliant" => undefined, "module-slot" => undefined, "xa-datasource-class" => undefined } }
データソースの追加
データソースの追加から、接続テストまでやります。
[standalone@localhost:9990 /] /subsystem=datasources/data-source=mysqlds:add(jndi-name=java:jboss/datasources/mysqlds,driver-name=mysql,connection-url=jdbc:mysql://localhost:3306/dbname,user-name=dbuser,password=dbpass) {"outcome" => "success"} [standalone@localhost:9990 /] /subsystem=datasources/data-source=mysqlds:test-connection-in-pool { "outcome" => "success", "result" => [true] } [standalone@localhost:9990 /] /subsystem=datasources/data-source=mysqlds:read-resource { "outcome" => "success", "result" => { "allocation-retry" => undefined, "allocation-retry-wait-millis" => undefined, "allow-multiple-users" => false, "background-validation" => undefined, "background-validation-millis" => undefined, "blocking-timeout-wait-millis" => undefined, "capacity-decrementer-class" => undefined, "capacity-decrementer-properties" => undefined, "capacity-incrementer-class" => undefined, "capacity-incrementer-properties" => undefined, "check-valid-connection-sql" => undefined, "connection-listener-class" => undefined, "connection-listener-property" => undefined, "connection-properties" => undefined, "connection-url" => "jdbc:mysql://localhost:3306/dbname", "datasource-class" => undefined, "driver-class" => undefined, "driver-name" => "mysql", "enabled" => true, "exception-sorter-class-name" => undefined, "exception-sorter-properties" => undefined, "flush-strategy" => undefined, "idle-timeout-minutes" => undefined, "initial-pool-size" => undefined, "jndi-name" => "java:jboss/datasources/mysqlds", "jta" => true, "max-pool-size" => undefined, "min-pool-size" => undefined, "new-connection-sql" => undefined, "password" => "dbpass", "pool-prefill" => undefined, "pool-use-strict-min" => undefined, "prepared-statements-cache-size" => undefined, "query-timeout" => undefined, "reauth-plugin-class-name" => undefined, "reauth-plugin-properties" => undefined, "security-domain" => undefined, "set-tx-query-timeout" => false, "share-prepared-statements" => false, "spy" => false, "stale-connection-checker-class-name" => undefined, "stale-connection-checker-properties" => undefined, "track-statements" => "NOWARN", "transaction-isolation" => undefined, "url-delimiter" => undefined, "url-selector-strategy-class-name" => undefined, "use-ccm" => true, "use-fast-fail" => false, "use-java-context" => true, "use-try-lock" => undefined, "user-name" => "dbuser", "valid-connection-checker-class-name" => undefined, "valid-connection-checker-properties" => undefined, "validate-on-match" => false, "statistics" => { "jdbc" => undefined, "pool" => undefined } } }
追加された設定
$ diff standalone/configuration/standalone.xml{.org,} 148a149,156 > <datasource jndi-name="java:jboss/datasources/mysqlds" pool-name="mysqlds" enabled="true"> > <connection-url>jdbc:mysql://localhost:3306/dbname</connection-url> > <driver>mysql</driver> > <security> > <user-name>dbuser</user-name> > <password>dbpass</password> > </security> > </datasource> 152a161,163 > <driver name="mysql" module="org.mysql"> > <driver-class>com.mysql.jdbc.Driver</driver-class> > </driver>
サンプルアプリケーション
MySQL内のデータを読み書きする小さなWebアプリを作ってみます。
テーブル作成
id
フィールドとname
フィールドだけを持つbook
テーブルを作成。
create table book ( id int primary key auto_increment, name varchar(128) not null );
プロジェクト作成
今回はNetBeans8.0を使います。
Webサービス作成
- 新規
- データベースからのRESTful Webサービス
- データソースは
mysqlds
を選択 - テーブルは
book
を選択 - パッケージ名は今回は
com.mythosil
とする - ApplicationConfigの作成
- BookFacadeRESTクラス上で「Alt+Enter」で作成
デプロイ・動作確認
デプロイはNetBeans上で「実行」ボタンを押すだけ。
動作確認はcurlで行います。
$ curl 'http://localhost:8080/SampleApp/webresources/com.mythosil.book' -X POST -H "Content-Type: application/json" -d '{"name":"Book1"}' $ curl 'http://localhost:8080/SampleApp/webresources/com.mythosil.book' -X POST -H "Content-Type: application/json" -d '{"name":"Book2"}' $ curl 'http://localhost:8080/SampleApp/webresources/com.mythosil.book' -H "Accept: application/json" | python -mjson.tool [ { "id": 1, "name": "Book1" }, { "id": 2, "name": "Book2" } ]
補足1: 設定変更
write-attribute
コマンドを利用します。
設定変更後は:reload
が必要です。
[standalone@localhost:9990 /] /subsystem=datasources/data-source=mysqlds:write-attribute(name=connection-url,value=jdbc:mysql://localhost:3306/otherdb) { "outcome" => "success", "response-headers" => { "operation-requires-reload" => true, "process-state" => "reload-required" } } [standalone@localhost:9990 /] :reload { "outcome" => "success", "result" => undefined }
補足2: データソースの削除
データソースに対してremove
コマンドを実行するだけです。
[standalone@localhost:9990 /] /subsystem=datasources/data-source=mysqlds:remove {"outcome" => "success"}
参考資料
WildFly: デプロイ時のnameとruntime-nameの役割
WildFlyへのCLIツールを利用したデプロイ時に--name
オプションと--runtime-name
オプションがあり、それぞれが何を意味しているのか分からなかったため、実験しつつ調べてみました。
まずはヘルプを読む
ざっくり把握した内容
name
はユニークでなければならない- 明示的に指定されなければ、ファイル名(war)がそのまま
name
になる runtime-name
はユニークである必要はないruntime-name
が重複している場合、どれか一つしか起動できない
[standalone@localhost:9990 /] deploy --help SYNOPSIS deploy ((file_path | --url=deployment_url) [--script=script_name] [--name=deployment_name] [--runtime-name=deployment_runtime_name] [--force | --disabled] [--unmanaged]) | --name=deployment_name [--server-groups=group_name (,group_name)* | --all-server-groups] [--headers={operation_header (;operation_header)*}] DESCRIPTION Deploys the application designated by the file_path or enables an already existing but disabled in the repository deployment designated by the name argument. If executed w/o arguments, will list all the existing deployments. ARGUMENTS ...(中略)... --name - the unique name of the deployment. If the file path argument is specified the name argument is optional with the file name been the default value. If the file path argument isn't specified then the command is supposed to enable an already existing but disabled deployment, and in this case the name argument is required. --runtime-name - optional, the runtime name for the deployment. This will form the basis for such things as default Java EE application and module names. This would typically be the same as --name, and if not specified the value used for --name will be used. In some cases users may wish to have two deployments with the same 'runtime-name' (e.g. two versions of "example.war") both available in the management configuration, in which case the deployments would need to have distinct 'name' values but would have the same 'runtime-name'. Within an individual server, only one deployment with a given 'runtime-name' can deployed. However, multiple deployments with the same 'runtime-name' can exist in the configuration, so long as only one is enabled. ...(略)...
サンプルアプリケーション
2つのごく単純なアプリケーション(war)を用意
- app-a.war: 「Hello, A」と表示
- app-b.war: 「Hello, B」と表示
何もオプションを指定せずデプロイ
NAME
とRUNTIME-NAME
はどちらもそのままwarファイル名となります。
アプリケーションにアクセスする際も、warファイル名がURLに含まれます(suffixは除く)。
- http://localhost:8080/app-a/
- 「Hello, A」と表示される
- http://localhost:8080/app-b/
- 「Hello, B」と表示される
[standalone@localhost:9990 /] deploy app-a.war [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS app-a.war app-a.war true true OK app-b.war app-b.war true true OK
アンデプロイする時はNAME
(=warファイル名)を指定します。
ファイルは残してdisabled
状態にしたい場合は、--keep-content
オプションを付けます。
この状態でアクセスすると、どちらも「404 - Not Found」と表示されます。
[standalone@localhost:9990 /] undeploy app-a.war [standalone@localhost:9990 /] undeploy app-b.war --keep-content [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS app-b.war app-b.war true false STOPPED
--name
オプションを指定してデプロイ
hoge.war
にすると/hoge
でアクセス出来るようになります。
- http://localhost:8080/A/
- 「Hello, A」と表示される
- http://localhost:8080/B/
- 「Hello, B」と表示される
[standalone@localhost:9990 /] deploy app-a.war --name=A.war [standalone@localhost:9990 /] deploy app-b.war --name=B.war [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS A.war A.war true true OK B.war B.war true true OK
アンデプロイは自分で指定したNAME
でやります。
[standalone@localhost:9990 /] undeploy A.war [standalone@localhost:9990 /] undeploy B.war
--runtime-name
オプションを指定してデプロイ
どちらもapp.war
というruntime-name
で起動しようとしてみます。
[standalone@localhost:9990 /] deploy app-a.war --runtime-name=app.war [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS app-a.war app.war true true OK [standalone@localhost:9990 /] deploy app-b.war --runtime-name=app.war {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-1" => "JBAS018785: There is already a deployment called app-a.war with the same runtime name app.war"}}
app-b.war
のほうは、runtime-name
がかぶってしまったため、デプロイ失敗しました。
この状態で http://localhost:8080/app/ にアクセスすると「Hello, A」と表示されます。
次に、app-a.war
をdisabled
状態に変えて、app-b.war
をデプロイしてみます。
[standalone@localhost:9990 /] undeploy app-a.war --keep-content [standalone@localhost:9990 /] deploy app-b.war --runtime-name=app.war [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS app-a.war app.war true false STOPPED app-b.war app.war true true OK
この状態で http://localhost:8080/app/ にアクセスすると「Hello, B」という表示に変わっています。
まとめ
name
は管理上利用する名称- デプロイ・アンデプロイ時のコマンドの引数として与える
- ユニークでなければならない
runtime-name
はURLの一部となる- ユニークである必要はない
- 同一の
runtime-name
のものは、どれか1つしかデプロイ状態にできない
WildFly 8.1.0.Finalのインストールからアプリケーションのデプロイまで
環境
- MacBookAir
- MacOSX 10.9.3
インストール
ダウンロードしてきて展開するだけ
$ curl -LO http://download.jboss.org/wildfly/8.1.0.Final/wildfly-8.1.0.Final.tar.gz $ tar xvfz wildfly-8.1.0.Final.tar.gz
起動
standalone.shを実行するだけ
$ cd wildfly-8.1.0.Final/bin $ ./standalone.sh ...(略)... 01:37:43,003 INFO [org.wildfly.extension.undertow] (MSC service thread 1-6) JBAS017519: Undertow HTTP listener default listening on /127.0.0.1:8080 01:37:43,463 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS] 01:37:43,558 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) JBAS015012: Started FileSystemDeploymentService for directory /Users/tabira/local/app/wildfly-8.1.0.Final/standalone/deployments 01:37:44,038 INFO [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.2.4.Final 01:37:44,132 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management 01:37:44,133 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990 01:37:44,134 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.1.0.Final "Kenny" started in 5951ms - Started 184 of 233 services (81 services are lazy, passive or on-demand)
http://localhost:8080/ にアクセスして「Welcome to WildFly 8. Your WildFly 8 is running.」と出たらOK
設定
今回は、アクセスログの有効化を行ってみる
管理コンソールにつなぐ
$ ./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
--connect
or -c
オプションを付けて起動すると、つないだ状態でプロンプトを出してくれる
アクセスログの有効化設定
[standalone@localhost:9990 /] /subsystem=undertow/server=default-server/host=default-host/setting=access-log:add {"outcome" => "success"}
アクセスログに関する設定確認
[standalone@localhost:9990 /] /subsystem=undertow/server=default-server/host=default-host/setting=access-log:read-resource { "outcome" => "success", "result" => { "directory" => expression "${jboss.server.log.dir}", "pattern" => "common", "prefix" => "access_log", "rotate" => true, "suffix" => ".log", "worker" => "default" } }
設定の再読み込み
[standalone@localhost:9990 /] :reload { "outcome" => "success", "result" => undefined }
サンプルアプリケーション
wildfly-sample-app
というプロジェクトを作成
$ mvn archetype:generate -DgroupId=com.mythosil -DartifactId=wildfly-sample-app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false $ cd wildfly-sample-app $ vim src/main/webapp/index.jsp <html> <head> <title>Hello, Wildfly!</title> </head> <body> <h1>It Works!!</h1> </body> </html> $ mvn package
デプロイ
$ ./jboss-cli.sh -c [standalone@localhost:9990 /] deploy wildfly-sample-app.war [standalone@localhost:9990 /] deployment-info NAME RUNTIME-NAME PERSISTENT ENABLED STATUS wildfly-sample-app.war wildfly-sample-app.war true true OK
http://localhost:8080/wildfly-sample-app/ にアクセスすると「It Works!!」と表示される
アンデプロイ
[standalone@localhost:9990 /] undeploy wildfly-sample-app.war
終了
Ctrl+Cでも終了できるが、せっかくなのでCLIツールを使ってみる
$ ./jboss-cli.sh -c --command=:shutdown {"outcome" => "success"}