メッセージメディエーション(Message Mediation)

この例では、Synapseは、通過するメッセージをすべて出力する単純なログを使用します。この単純なシナリオは、ログ出力を行うだけですが、この説明は、メッセージメディエーションの基本となります。このログ出力の機能は、メッセージの変換やコンテントベースのルーティングだけでなく、異なる通信プロトコルのブリッジといった、どのような組み合わせにも置き換えることが可能です。

ダウンロード(Download)

最初に、Synapseをダウンロードします。ブラウザを開き、次のURLにアクセスします。http://synapse.apache.org/download.html リリースの一覧から、最新のバージョンをクリックすると、Synapseのダウンロードページに移動します。それから、'Standard binary distribution'のZIPまたはtar.gzから、使用しているオペレーティングシステムに適したアーカイブをダウンロードします。

インストール(Installation)

Synapseは、ダウンロードしたバイナリを解凍するだけでインストールできます。選択された親ディレクトリの中には、"synapse"とバージョン番号がついたSynapseに必要なファイルをすべて含むディレクトリが作成されます。これからこのディレクトリのことを、とします。

サンプルを実行する。

Synapseは、そのコアとなるいくつかの機能を説明するための一連のサンプルクライアントおよびサービスとともにリリースされています。したがって、あなたは、メッセージメディエーションの知識をえるために3つのプログラムを実行することができます。(?) クライアントから呼び出される最終的なサービスをホストしている宛先のサーバ、そしてそのクライアントの間で、Synapseは仲介をするブリッジとして動作します。

Axis2サーバのサンプルをスタートする。(Starting the sample Axis2 server)

ここでは、サーバとして、Axis2Webサービスエンジンをスタンドアローンで使用します。配布されているSynapseには、すでにAxis2がバンドルされているので、Axis2を新たに入手する必要はありません。しかし、クライアントがリクエストを送信できるように、サンプルのサービスをデプロイする必要があります。/samples/axis2Server/src/SimpleStockQuoteService ディレクトリに移動してください。そして、サンプルのサービスをビルドし、Axis2サーバにデプロイするために、"ant"を実行します。

user@host:/opt/synapse-1.1.1/samples/axis2Server$ cd src/SimpleStockQuoteService/
user@host:/opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService$ ant
Buildfile: build.xml

clean:

init:
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/classes
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Server/repository/services

compile-all:
    [javac] Compiling 9 source files to /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/classes

build-service:
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/SimpleStockQuote
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/SimpleStockQuote/META-INF
     [copy] Copying 1 file to /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/SimpleStockQuote/META-INF
     [copy] Copying 1 file to /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/SimpleStockQuote/META-INF
     [copy] Copying 9 files to /opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService/temp/SimpleStockQuote
      [jar] Building jar: /opt/synapse-1.1.1/samples/axis2Server/repository/services/SimpleStockQuoteService.aar

BUILD SUCCESSFUL
Total time: 2 seconds
user@host:/opt/synapse-1.1.1/samples/axis2Server/src/SimpleStockQuoteService$

/samples/axis2Server ディレクトリに移動し、以下のコマンドでサーバを起動します。これにより、Axis2サーバが、9000番(http)*1のポートで起動します。

コンソールには、以下のメッセージがが表示されます。

user@host:/opt/synapse-1.1.1/samples/axis2Server$ ./axis2server.sh
 Using Bouncy castle JAR for Java 1.5
 Using JAVA_HOME:   /opt/jdk1.5_06
 Using AXIS2 Repository :   /opt/synapse-1.1.1/samples/axis2Server/repository
 Using AXIS2 Configuration :   /opt/synapse-1.1.1/samples/axis2Server/repository/conf/axis2.xml
2007-11-05 14:36:41,462 [-] [main]  INFO SimpleHTTPServer [SimpleAxisServer] Starting
[SimpleAxisServer] Using the Axis2 Repository : /opt/synapse-1.1.1/samples/axis2Server/repository
[SimpleAxisServer] Using the Axis2 Configuration File : /opt/synapse-1.1.1/samples/axis2Server/repository/conf/axis2.xml
2007-11-05 14:36:43,864 [-] [main]  INFO HttpCoreNIOSender HTTPS Sender starting
2007-11-05 14:36:43,891 [-] [main]  INFO HttpCoreNIOSender HTTP Sender starting
2007-11-05 14:36:44,288 [-] [main]  INFO HttpCoreNIOListener HTTPS Listener starting on port : 9002
2007-11-05 14:36:44,298 [-] [main]  INFO HttpCoreNIOListener HTTP Listener starting on port : 9000
2007-11-05 14:36:44,350 [-] [main]  INFO SimpleHTTPServer [SimpleAxisServer] Started
Synapseを起動する。

Synapseを起動するときがきました。このシナリオでは、synapse_sample_0.xml(repository/conf/sample内)に定義されたサンプルコンフィグレーションを使用します。その内容を以下に示します。以下は、すべてのメッセージをログ出力し、通過させるための設定です。

<definitions xmlns="http://ws.apache.org/ns/synapse">
    <log level="full"/> 
    <send/>
</definitions> 

/bin ディレクトリに移動し、以下に示すコマンドをタイプします。すると、Synapseは、8080番(http)と、8443番(https - JDK1.5使用時)のポートで起動します。

Synapseの起動が成功すると、以下に示すメッセージがコンソールに出力されます。

user@host:/opt/synapse-1.1.1/bin$ ./synapse.sh -sample 0
 Using Bouncy castle JAR for Java 1.5
Starting Synapse/Java ...
Using SYNAPSE_HOME:    /opt/synapse-1.1.1
Using JAVA_HOME:       /opt/jdk1.5_06
Using SYNAPSE_XML:     -Dsynapse.xml=/opt/synapse-1.1.1/repository/conf/sample/synapse_sample_0.xml
2007-11-05 14:58:55,029 [-] [main]  INFO ServerManager Using the Axis2 Repository /opt/synapse-1.1.1/repository
2007-11-05 14:58:56,617 [-] [main]  INFO SynapseInitializationModule Initializing Synapse at : Mon Nov 05 14:58:56 LKT 2007
2007-11-05 14:58:56,618 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Loading mediator extensions...
2007-11-05 14:58:56,619 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Initializing the Synapse configuration ...
2007-11-05 14:58:56,624 [127.0.1.1-asankha] [main]  INFO XMLConfigurationBuilder Generating the Synapse configuration model by parsing the XML configuration
2007-11-05 14:58:56,717 [127.0.1.1-asankha] [main]  INFO SynapseConfigurationBuilder Loaded Synapse configuration from : /opt/synapse-1.1/repository/conf/sample/synapse_sample_0.xml
2007-11-05 14:58:56,724 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Deploying the Synapse service..
2007-11-05 14:58:56,740 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Initializing Sandesha 2...
2007-11-05 14:58:56,801 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Deploying Proxy services...
2007-11-05 14:58:56,801 [127.0.1.1-asankha] [main]  INFO SynapseInitializationModule Synapse initialized successfully...!
2007-11-05 14:58:56,886 [127.0.1.1-asankha] [main]  INFO HttpCoreNIOSender HTTPS Sender starting
2007-11-05 14:58:56,887 [127.0.1.1-asankha] [main]  INFO HttpCoreNIOSender HTTP Sender starting
2007-11-05 14:58:57,039 [127.0.1.1-asankha] [main]  INFO HttpCoreNIOListener HTTPS Listener starting on port : 8443
2007-11-05 14:58:57,040 [127.0.1.1-asankha] [main]  INFO ServerManager Starting transport https on port 8443
2007-11-05 14:58:57,041 [127.0.1.1-asankha] [main]  INFO HttpCoreNIOListener HTTP Listener starting on port : 8080
2007-11-05 14:58:57,041 [127.0.1.1-asankha] [main]  INFO ServerManager Starting transport http on port 8080
2007-11-05 14:58:57,085 [127.0.1.1-asankha] [main]  INFO ServerManager Starting transport vfs
2007-11-05 14:58:57,086 [127.0.1.1-asankha] [main]  INFO ServerManager Ready for processing
クライアントを実行する。(Run the client)

最後のステップでは、クライアントを実行します。 /samples/axis2Client ディレクトリに移動し、以下のコマンドを実行します。

user@host:/opt/synapse-1.1.1/samples/axis2Client$ ant stockquote -Daddurl=http://localhost:9000/soap/SimpleStockQuoteService -Dtrpurl=http://localhost:8080 -Dmode=quote -Dsymbol=IBM
Buildfile: build.xml

init:
    [mkdir] Created dir: /opt/synapse-1.1.1/samples/axis2Client/target/classes

compile:
    [javac] Compiling 10 source files to /opt/synapse-1.1.1/samples/axis2Client/target/classes

stockquote:
     [java] Standard :: Stock price = $91.09641757880443

BUILD SUCCESSFUL

ここでは、株価情報の記号であるIBMと、SynapseのトランスポートURL(http://localhost:8080)、実際のサービスを提供するサーバ(http://localhost:9000/soap/SimpleStockQuoteService)のWS-Addressing EPRのリクエストを送信します。
クライアントからローカルホスト上のSynapseインスタンスの8080番ポートに送信された実際のwire level? HTTPメッセージは以下のとおりです。

POST / HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "urn:getQuote"
User-Agent: Axis2
Host: 127.0.0.1
Transfer-Encoding: chunked

218
<?xml version='1.0' encoding='UTF-8'?>
   <soapenv:Envelope xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Header>
         <wsa:To>http://localhost:9000/soap/SimpleStockQuoteService</wsa:To>
         <wsa:MessageID>urn:uuid:D538B21E30B32BB8291177589283717</wsa:MessageID>
         <wsa:Action>urn:getQuote</wsa:Action>
      </soapenv:Header>
      <soapenv:Body>
         <m0:getQuote xmlns:m0="http://services.samples/xsd">
            <m0:request>
               <m0:symbol>IBM</m0:symbol>
            </m0:request>
         </m0:getQuote>
      </soapenv:Body>
   </soapenv:Envelope>0

Synapseを実行しているコンソールを確認します。Synapseを通過したすべてのSOAPメッセージとともに、Synapseが仲介した詳細情報を確認することができます。lib/log4j.propertiesの"log4j.category.org.apache.synapse"を"INFO"から"DEBUG"に変更することで、Synapseをデバッグモードで起動できます。Synapseを再起動して、上述のシナリオを再度繰り返すと以下に示すように、より多くの情報を得ることができます。

2007-11-05 15:03:51,082 [127.0.1.1-asankha] [I/O dispatcher 3]  INFO PipeImpl Using native OS Pipes for event-driven to stream IO bridging
2007-11-05 15:03:51,206 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SynapseMessageReceiver Synapse received a new message for message mediation...
2007-11-05 15:03:51,206 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SynapseMessageReceiver Received To: http://localhost:9000/soap/SimpleStockQuoteService
2007-11-05 15:03:51,206 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SynapseMessageReceiver SOAPAction: urn:getQuote
2007-11-05 15:03:51,206 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SynapseMessageReceiver WSA-Action: urn:getQuote
2007-11-05 15:03:51,206 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG Axis2SynapseEnvironment Injecting MessageContext
2007-11-05 15:03:51,207 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG Axis2SynapseEnvironment Using Main Sequence for injected message
2007-11-05 15:03:51,207 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SequenceMediator Start : Sequence <main>
2007-11-05 15:03:51,207 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SequenceMediator Sequence <SequenceMediator> :: mediate()
2007-11-05 15:03:51,207 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG LogMediator Start : Log mediator
2007-11-05 15:03:51,231 [127.0.1.1-asankha] [HttpServerWorker-1]  INFO LogMediator To: http://localhost:9000/soap/SimpleStockQuoteService, WSAction: urn:getQuote, SOAPAction: urn:getQuote, ReplyTo: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:DB76240DF26CE9AF1D1194253430879, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing"><soapenv:Header><wsa:To>http://localhost:9000/soap/SimpleStockQuoteService</wsa:To><wsa:MessageID>urn:uuid:DB76240DF26CE9AF1D1194253430879</wsa:MessageID><wsa:Action>urn:getQuote</wsa:Action></soapenv:Header><soapenv:Body><m0:getQuote xmlns:m0="http://services.samples/xsd"><m0:request><m0:symbol>IBM</m0:symbol></m0:request></m0:getQuote></soapenv:Body></soapenv:Envelope>
2007-11-05 15:03:51,250 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG LogMediator End : Log mediator
2007-11-05 15:03:51,250 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SendMediator Start : Send mediator
2007-11-05 15:03:51,250 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SendMediator Sending request message using implicit message properties..
Sending To: http://localhost:9000/soap/SimpleStockQuoteService
SOAPAction: urn:getQuote
2007-11-05 15:03:51,254 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG Axis2FlexibleMEPClient Sending [add = false] [sec = false] [rm = false] [ to Address: http://localhost:9000/soap/SimpleStockQuoteService]
2007-11-05 15:03:51,302 [127.0.1.1-asankha] [HttpServerWorker-1]  INFO TimeoutHandler This engine will expire all callbacks after : 86400 seconds, irrespective of the timeout action, after the specified or optional timeout
2007-11-05 15:03:51,356 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SendMediator End : Send mediator
2007-11-05 15:03:51,356 [127.0.1.1-asankha] [HttpServerWorker-1] DEBUG SequenceMediator End : Sequence <main>
2007-11-05 15:03:51,398 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SynapseCallbackReceiver Synapse received an asynchronous response message
2007-11-05 15:03:51,400 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SynapseCallbackReceiver Received To: null
2007-11-05 15:03:51,400 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SynapseCallbackReceiver SOAPAction: null
2007-11-05 15:03:51,400 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SynapseCallbackReceiver WSA-Action: null
2007-11-05 15:03:51,402 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SynapseCallbackReceiver Body :
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:getQuoteResponse xmlns:ns="http://services.samples/xsd"><ns:return type="samples.services.GetQuoteResponse"><ns:change>4.03627430702446</ns:change><ns:earnings>-9.467701672785129</ns:earnings><ns:high>191.83014686803938</ns:high><ns:last>185.42637586281398</ns:last><ns:lastTradeTimestamp>Mon Nov 05 15:03:51 LKT 2007</ns:lastTradeTimestamp><ns:low>193.2690208751758</ns:low><ns:marketCap>-1737393.107878862</ns:marketCap><ns:name>IBM Company</ns:name><ns:open>-183.2632780777984</ns:open><ns:peRatio>-17.430497030284027</ns:peRatio><ns:percentageChange>1.9749680728382655</ns:percentageChange><ns:prevClose>204.37162314344914</ns:prevClose><ns:symbol>IBM</ns:symbol><ns:volume>6695</ns:volume></ns:return></ns:getQuoteResponse></soapenv:Body></soapenv:Envelope>
2007-11-05 15:03:51,404 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG Axis2SynapseEnvironment Injecting MessageContext
2007-11-05 15:03:51,404 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG Axis2SynapseEnvironment Using Main Sequence for injected message
2007-11-05 15:03:51,404 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SequenceMediator Start : Sequence <main>
2007-11-05 15:03:51,404 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SequenceMediator Sequence <SequenceMediator> :: mediate()
2007-11-05 15:03:51,404 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG LogMediator Start : Log mediator
2007-11-05 15:03:51,405 [127.0.1.1-asankha] [HttpClientWorker-1]  INFO LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, MessageID: urn:uuid:A6510AF6BD288D8DFB1194253431259544001-942151716, Direction: response, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns:getQuoteResponse xmlns:ns="http://services.samples/xsd"><ns:return type="samples.services.GetQuoteResponse"><ns:change>4.03627430702446</ns:change><ns:earnings>-9.467701672785129</ns:earnings><ns:high>191.83014686803938</ns:high><ns:last>185.42637586281398</ns:last><ns:lastTradeTimestamp>Mon Nov 05 15:03:51 LKT 2007</ns:lastTradeTimestamp><ns:low>193.2690208751758</ns:low><ns:marketCap>-1737393.107878862</ns:marketCap><ns:name>IBM Company</ns:name><ns:open>-183.2632780777984</ns:open><ns:peRatio>-17.430497030284027</ns:peRatio><ns:percentageChange>1.9749680728382655</ns:percentageChange><ns:prevClose>204.37162314344914</ns:prevClose><ns:symbol>IBM</ns:symbol><ns:volume>6695</ns:volume></ns:return></ns:getQuoteResponse></soapenv:Body></soapenv:Envelope>
2007-11-05 15:03:51,405 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG LogMediator End : Log mediator
2007-11-05 15:03:51,405 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SendMediator Start : Send mediator
2007-11-05 15:03:51,405 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SendMediator Sending response message using implicit message properties..
Sending To: http://www.w3.org/2005/08/addressing/anonymous
SOAPAction: null
2007-11-05 15:03:51,408 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SendMediator End : Send mediator
2007-11-05 15:03:51,408 [127.0.1.1-asankha] [HttpClientWorker-1] DEBUG SequenceMediator End : Sequence <main>

これで、このガイドの最初のパートは終了です。次のシナリオであるプロキシサービスを見てみましょう。

今日はここまで...

*1:httpsは9002