そごうソフトウェア研究所

SOA、開発プロセス、ITアーキテクチャなどについて書いています。Twitterやってます@rsogo

Mule ESB 3.7のDatabase Connectorを試す2(INSERT)

先日、Muleを使ってOracle Databaseにアクセスするための設定の記事を書きました。

begirama.hatenablog.com

今回は、JSONで受け取って、Oracle DBにInsertするまでを書きます。

まず、メインのmule-config.xml

gist.github.com

前回のSELECTだけやるフローを改良しています。

まず、HTTPメソッドに応じて動かすフローを分けています。

GETメソッドで動かすフロー

  <flow name="GetFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="Recieve HTTP request" allowedMethods="GET"/>

中身は前回のフローとほぼ同様で、データベースに対してSELECT文を発行して、結果をJSONにして返します。

レスポンスのサンプルはこんな感じ。

[
  {
    "VALUE": "foo",
    "NAME": "SATO"
  },
  {
    "VALUE": "bar",
    "NAME": "SUZUKI"
  }
]

POSTメソッドで動かすフロー

  <flow name="PostFlow" doc:name="PostFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="Recieve HTTP request" allowedMethods="POST"/>

中身の処理は

デバッグ用にTransformerの前後でPayloadの中身をログにダンプ

<logger doc:name="Log the payload" level="INFO" message="About to echo #[message.payload]"/>

変換前後のログの出力結果はこんな感じ。

INFO  2015-08-06 08:51:47,892 [[database_update].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: About to echo org.glassfish.grizzly.utils.BufferInputStream@386d562
INFO  2015-08-06 08:51:47,973 [[database_update].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: About to echo [{VALUE=foo, NAME=SATO}, {VALUE=bar, NAME=SUZUKI}]

JSONをObjectに変換

この時の変換先のクラスはjava.util.ArrayListということを指定しています。 今回は、この後でループ処理をやるためにArrayListに変換していますが、<byte-array-to-string-transformer doc:name="Convert Byte Array to String"/>とかで、一つのStringオブジェクトに変換してもOK。

<json:json-to-object-transformer returnClass="java.util.ArrayList" doc:name="JSON to Object"/>

INSERTの発行

リクエストに含まれる件数分ループで回しつつ、Insert文を発行。

<foreach collection="#[message.payload]" doc:name="For Each">
    <db:insert config-ref="Oracle_Configuration" doc:name="Database">
        <db:parameterized-query><![CDATA[INSERT INTO TEST(NAME, VALUE)
              VALUES (#[payload.NAME], #[payload.VALUE])]]></db:parameterized-query>
    </db:insert>
</foreach>

マニュアル

あんまり読んで無いけど、JDBC周りのリファレンス。

JDBC Transport Reference - Current Mule Documentation

HTTPリスナでリクエストを受け付けるところはこのマニュアルを読んでおけばOK。

HTTP Listener Connector - Current Mule Documentation

Payloadからの値の取り方について

あと、これも参考にした。

JSON Payload ingested into MySQL database using Mule ESB - Stack Overflow

Mule ESB 3.7 メッセージ変換

使えるメッセージ変換の機能は下記のマニュアルを参照。

  • 用意されている変換機能

Objectと、JSONXMLの変換なんかは自動でやってくれる。 いざとなったらJavaやGroovyも呼び出せる。

Transformers - Current Mule Documentation

MEL (Mule Expression Language)

MELというXMLベースの独自言語が用意されていて、メッセージの要素にアクセスしたり、値を評価しての条件分岐とか書ける。

概要はこっちのマニュアルを読んで、

Mule Expression Language MEL - Current Mule Documentation

どんな機能が用意されているからは、こっちのリファレンスを見ればOK。 Mule Expression Language Reference - Current Mule Documentation

後はサンプルを見て、感じをつかむのが早い。

デプロイすると、No suitable driver foundが原因のjava.sql.SQLExceptionが発生する。 Muleを再起動すると解消する。DB接続が絡んでいない場合は、起動中のホットデプロイができているので、DB接続がらみで何かあるのかも知れない。要調査。

org.mule.module.db.internal.domain.connection.ConnectionCreationException: java.sql.SQLException: Cannot get connection for URL jdbc:oracle:thin:CMN/CMDB01@localhost:1521:aaa : No suitable driver found for jdbc:oracle:thin:aaa/aaa@localhost:1521:aaa (java.sql.SQLException). Message payload is of type: LinkedHashMap

Mule ESB 3.7のDatabase Connectorを試す1(設定とSELECT)

デプロイ

mule-config.xmlをappsの下に作ったdatabaseフォルダに置きます。

$ pwd
/Users/rsogo/work/mule-standalone-3.7.0/apps/database
$ ls
mule-config.xml

gist.github.com

デプロイ時ログ

おぉ、成功。

*******************************************************************************************************
*            - - + APPLICATION + - -            *       - - + DOMAIN + - -       * - - + STATUS + - - *
*******************************************************************************************************
* default                                       * default                        * DEPLOYED           *
* database                                      * default                        * DEPLOYED           *
*******************************************************************************************************

JDBC Driverの配置

  • Database Driverになにもやってない状態でhttp://localhost:8081/にアクセスすると次のようなエラーがでた。
org.mule.module.db.internal.domain.connection.ConnectionCreationException: java.sql.SQLException: Error trying to load driver: oracle.jdbc.Driver : oracle.jdbc.Driver (java.sql.SQLException). Message payload is of type: NullPayload

OracleからJDBC Driverをダウンロードする。

Oracle Database 12c Release 1 JDBC Driver Downloads

自分の環境はJDK1.7を使っているので、ojdbc7.jar

{MULE_HOME}/lib/user/配下に置く。

$ ls lib/user/
README.txt          mule-tests-functional-3.7.0.jar xmlunit-1.5.jar
junit-4.11.jar          ojdbc7.jar

StackOverflowかどこかに{MULE_HOME}/lib/mule/に置けって言っている人がいるけど、それだと動かなかった。

  • DriverはOKで、コネクションが確立できない場合のログ。
org.mule.module.db.internal.domain.connection.ConnectionCreationException: java.sql.SQLException: Cannot get connection for URL jdbc:oracle:thin:{User}/{Password}@localhost:{SID} : No suitable driver found for jdbc:oracle:thin:{User}/{Password}@localhost:{SID} (java.sql.SQLException). Message payload is of type: NullPayload

実行

Muleを実行しているマシンのhttp://locahost:8081/にアクセスしてみる。 上手く行っていると、データベースに検索した結果がJSONに変換されたレスポンスが見られる。

続きを次のエントリで書いています。 begirama.hatenablog.com

Mule ESB 3.7起動

以前、OSX上でMule 3.6を動かしました。

Mule ESB 3.6起動 - そごうソフトウェア研究所

Mule 3.7がリリースされているので、そちらを試してみたいと思います。

Mule 3.7のダウンロード

以前のエントリでは、Enterprise版のダウンロードページにしか行けずに、そちらで検証していました。 Enterprise版ではトライアル期間が終わると次のようなエラーが出て、起動できなくなります。

ERROR 2015-08-04 16:22:59,527 [main] com.mulesource.licm.impl.TrueLicenseHelper: Couldn't validate license key!
Invalid license

Community Editionのダウンロードはこちらで行けそう。

developer.mulesoft.com

Muleの起動

ダウンロードしたファイルを解凍。

$ unzip mule-standalone-3.7.0.zip

実行ファイルがあるところまで移動して、

$ cd mule-standalone-3.7.0/bin/
$ ls
additional.groovy   launcher.bat        mule
launcher        launcher.conf       mule.bat

muleコマンドを実行

$ ./mule 

起動ログ

$ ./mule 
MULE_HOME is set to /Users/rsogo/work/mule-standalone-3.7.0
Running in console (foreground) mode by default, use Ctrl-C to exit...
MULE_HOME is set to /Users/rsogo/work/mule-standalone-3.7.0
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Running Mule...
--> Wrapper Started as Console
Launching a JVM...
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Starting the Mule Container...
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
  Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.


WARNING - Unable to load the Wrapper's native library because none of the
          following files:
            libwrapper-macosx-x86-64.dylib
            libwrapper-macosx-universal-64.dylib
            libwrapper.dylib
          could be located on the following java.library.path:
            /Users/rsogo/work/mule-standalone-3.7.0/bin/%LD_LIBRARY_PATH%
            /Users/rsogo/work/mule-standalone-3.7.0/lib/boot
          Please see the documentation for the wrapper.java.library.path
          configuration property.
          System signals will not be handled correctly.

INFO  2015-08-04 16:46:04,937 [WrapperListener_start_runner] org.mule.module.launcher.MuleContainer: 
**********************************************************************
* Mule ESB and Integration Platform                                  *
* Version: 3.7.0 Build: 725cbc8a                                     *
* MuleSoft, Inc.                                                     *
* For more information go to http://www.mulesoft.org                 *
*                                                                    *
* Server started: 15/08/04 16:46                                     *
* JDK: 1.7.0_71 (mixed mode)                                         *
* OS: Mac OS X (10.10.4, x86_64)                                     *
* Host: Ryohei-no-MacBook-Pro.local (192.168.11.5)                   *
**********************************************************************
INFO  2015-08-04 16:46:04,941 [WrapperListener_start_runner] org.mule.module.launcher.coreextension.DefaultMuleCoreExtensionManager: Initializing core extensions
INFO  2015-08-04 16:46:04,941 [WrapperListener_start_runner] org.mule.module.launcher.coreextension.DefaultMuleCoreExtensionManager: Starting core extensions
INFO  2015-08-04 16:46:04,955 [WrapperListener_start_runner] org.mule.module.launcher.DefaultArchiveDeployer: ================== New Exploded Artifact: default
INFO  2015-08-04 16:46:04,963 [WrapperListener_start_runner] org.mule.module.launcher.MuleSharedDomainClassLoader: Using domain dir /Users/rsogo/work/mule-standalone-3.7.0/domains/default for domain default
INFO  2015-08-04 16:46:05,013 [WrapperListener_start_runner] org.mule.module.launcher.MuleDeploymentService: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started domain 'default'                                 +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2015-08-04 16:46:05,020 [WrapperListener_start_runner] org.mule.module.launcher.DefaultArchiveDeployer: ================== New Exploded Artifact: default
INFO  2015-08-04 16:46:05,044 [WrapperListener_start_runner] org.mule.module.launcher.application.DefaultMuleApplication: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ New app 'default'                                        +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2015-08-04 16:46:09,459 [WrapperListener_start_runner] org.mule.module.launcher.MuleDeploymentService: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Started app 'default'                                    +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2015-08-04 16:46:09,462 [WrapperListener_start_runner] org.mule.module.launcher.DeploymentDirectoryWatcher: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Mule is up and kicking (every 5000ms)                    +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2015-08-04 16:46:09,533 [WrapperListener_start_runner] org.mule.module.launcher.StartupSummaryDeploymentListener: 
**********************************************************************
*              - - + DOMAIN + - -               * - - + STATUS + - - *
**********************************************************************
* default                                       * DEPLOYED           *
**********************************************************************

*******************************************************************************************************
*            - - + APPLICATION + - -            *       - - + DOMAIN + - -       * - - + STATUS + - - *
*******************************************************************************************************
* default                                       * default                        * DEPLOYED           *
*******************************************************************************************************

Jenkins導入メモ

やったこと。

Jenkinsのダウンロード

本家にMac用のモジュールがあったので取得

プラグインを入れる

gitプラグインが入ってなかったので入れないといろいろできない。

その前に、既存のプラグインをすべて更新。

ついでに入れたプラグイン達。

Android Studioへの自作ライブラリプロジェクトの取込

まだそれほどAndroid Studioを触ったわけではないですが、社内のAndroidプロジェクトを順次、ADTからAndroid Studioに移行しています。

今回は社内のAndroidライブラリのプロジェクトを他のプロジェクトから使うまでの手順です。もちろんライブラリ側のプロジェクトをjar化して、使用するのもありますが、プロジェクトを参照する形で今回はやっています。

新規プロジェクトの作成

ライブラリを使う側のプロジェクトを新規作成します。

モジュールのインポート

メニューの「New>Module」を選択します。 f:id:begirama:20150727160851p:plain 「Import Gradle Project」を選択します。

setting.gradleファイルに以下の記述が追加されます。

include ':app', ':AppPotSDK'

ここではappが利用側プロジェクト、AppPotSDKがライブラリプロジェクトです。

build.gradleへの依存性の設定

利用側プロジェクトのbuild.gradleファイルに下記の記述を追記します。AppPotSDKは先ほどのImportしたライブラリプロジェクトです。 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile project(":AppPotSDK") }

以上

Mule ESB 3.7 Database Connectorの情報

マニュアルに寄ると、もともと有ったJDBC connectorは、Mule 3.5からDatabase connectorに置き換えられました。

Note: The Database connector replaces the JDBC connector. As of Mule 3.5.0, the JDBC connector is deprecated.

しかも、Database connectorはMule Community Editionでも使えます。これは嬉しい。

The Database connector is available with both Mule Community and Mule Enterprise runtimes.

使える操作はマニュアルから引用すると下記の通り。

  • Select
  • Insert
  • Update
  • Delete
  • Stored Procedure
  • Bulk Execute
  • DDL operations such as CREATE, ALTER, etc.

参照情報

マニュアル

Database Connector Reference - Current Mule Documentation

2つのサンプルが紹介されています。

Database Connector Examples - Current Mule Documentation

1つはHTTP Connectorでリクエストを受け付け、Databaseにクエリーを発行し、JSONで返します。でもこの形は推奨していないみたい。なんでだろう。負荷?

This example is meant to illustrate the concept of a simple SELECT operation, but we do not recommend exposing database functionality directly as an API.

2つめはDatabaseからFileへの連携。

サンプルファイルの有りか