JBoss Fuseを使ってみる その3:Webサービス編
![](https://thinkit.co.jp/sites/default/files/styles/main_image_730x/public/main_images/5926_main_8.png?itok=cU8lAz8-)
3.WebサービスをCamelルートから呼び出す
「2.CamelルートをWebサービス化する」で作成したそれぞれのWebサービスを、異なるCamelルートから呼び出す手順を説明します。以下の2つを実装します。
- SOAP Webサービスを呼び出す
- RESTful Webサービスを呼び出す
ここでもまず、これら2つのCamelルートのためのFuseプロジェクトを作成します。
クライアントFuseプロジェクトの設定
設定 | 値 |
---|---|
Group Id | com.mycompany(デフォルト) |
Artifact Id | camel-archetype-blueprintを選択後、wsclientに変更 |
Version | 1.0.0-SNAPSHOT |
Package | (Group IdとArtifact Idから自動生成) |
作成後、不要なクラスやCamelルート、テストコードは削除しておきます。
次にFuseプロジェクト内の「pom.xml」に以下の依存ライブラリ設定を追加します。必ずpom.xml内の
リスト11:pom.xmlに記述する依存ライブラリ
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>2.12.0.redhat-610379</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.0.redhat-610379</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.0.redhat-610379</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.7.0.redhat-610379</version> </dependency>
SOAP Webサービスを呼び出す
【手順1】WSDLからJavaを生成する
SOAP Webサービスを利用する際には、WSDLからJavaを生成します。まず、事前にWSDLファイルをダウンロードし、wsclientプロジェクト内にインポートしておきます。ダウンロードは、URL「http://localhost:8181/cxf/order/?wsdl」から行います。WSDLからJavaプログラムを生成するツールとして、「wsdl2java」を使用します。pom.xmlの
リスト12:pom.xmlに追加する内容
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>2.7.0.redhat-610379</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>wsdl2java</goal> </goals> <configuration> <sourceRoot> ${project.basedir}/generated </sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${project.basedir}/src/main/resources/wsdl/OrderEndpointService.wsdl</wsdl> <extraargs> <extraarg>-fe</extraarg> <extraarg>jaxws21</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> </execution> </executions> </plugin>
この設定により、「src/main/resources/wsdl/OrderEndpointService.wsdl」から生成されるJavaプログラムが「${project.basedir}/generated」に生成されます。
生成はmvn generate-sourcesで行います。JBDSではFUSEプロジェクトを右クリックし、[Run As]−[Maven generate-sources]で生成されます。以のようなクラスが生成されていることを確認します(図17)。
![生成されるJavaコード](/sites/default/files/592617.png)
図17:生成されるJavaコード
生成されているクラスはそれぞれ以下のようなものになります。
生成されるJavaコード一覧
クラス | 概要 |
---|---|
ObjectFactory.java | JavaとXMLの相互変換を行うためのクラス |
Order.java | SOAPリクエストで使用されるXML型を表現するクラス |
OrderEndpoint.java | Webサービスのリクエスト・レスポンスのインターフェース |
OrderEndpointService.java | Webサービスクライアント。javax.xml.ws.Serviceの拡張クラス |
OrderResponse.java | SOAPレスポンスで使用されるXML型を表現するクラス |
package-info.java | パッケージ情報 |
【手順2】Camelルートの中でSOAP Webサービスを呼び出す
Camelルートとして次のような内容を作成します。
SOAPクライアントのCamelルート設定
設定 | 値 |
---|---|
RouteContainer | /wsclient/src/main/resources/OSGI-INF/blueprint |
File name | soapClient.xml |
Framework | OSGi Blueprint |
Designモード
リスト13:SOAPクライアントのCamelクライアント(Sourceモード)
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <cxf:cxfEndpoint id="soapClient" address="http://localhost:8181/cxf/order/" serviceClass="com.mycompany.wsserver.OrderEndpoint"/> <camelContext trace="false" id="blueprintContext" xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="timer:foo?period=30000"/> <transform> <simple>sample</simple> </transform> <to uri="cxf:bean:soapClient"/> <log message="メッセージ : ${body} "/> </route> </camelContext> </blueprint>
SOAPサービスを呼び出している箇所は、リスト13の強調されている行です。参照などについてはサーバーサイドと同様の実装になっています。
【手順3】ビルド、インストール、確認
ビルド、インストール、設定の確認方法はこれまでと同様です。インストールの際に設定するArtifact IDが異なる点にご注意ください。
JBossFuse:admin@root> osgi:install -s mvn:com.mycompany/wsclient/1.0.0-SNAPSHOT Bundle ID: 335
SOAP Webサービスを30秒ごとに呼んでいることを確認します。
JBossFuse:admin@root> log:display -n 2 2015-03-19 19:32:01,483 | INFO | ault-workqueue-1 | route22 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | メッセージ : OK : sample 2015-03-19 19:32:31,455 | INFO | ault-workqueue-2 | route22 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | メッセージ : OK : sample
RESTful Webサービスを呼び出す
【手順1】Camelルートの作成
まず、以下のようなCamelルートを作成します。
RESTクライアントのCamelルート設定
設定 | 値 |
---|---|
RouteContainer | /wsclient/src/main/resources/OSGI-INF/blueprint |
File name | restClient.xml |
Framework | OSGi Blueprint |
Designモード
リスト14:RESTクライアントのCamelルート(Sourceモード)
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <cxf:rsClient id="restClient" address="http://localhost:8181"/> <camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="timer:bar?period=40000"/> <setHeader headerName="Exchange.HTTP_PATH"> <simple>/cxf/customer/add/0010/テスト/サンプル/20</simple> </setHeader> <setHeader headerName="Exchange.HTTP_METHOD"> <simple>GET</simple> </setHeader> <setExchangePattern pattern="InOut"/> <to uri="cxfrs:bean:restClient"/> <log message="REST : レスポンスメッセージ : ${body}"/> </route> </camelContext> </blueprint>
複数の
RESTサービスを呼び出しているのは、Sourceモード(リスト14)の強調箇所です。注意点として、cxfrsのデフォルトのMessage Exchange PatternはInOnlyなので、そのままの設定では
【手順2】ビルド、インストール、確認
ビルド、インストール、設定の確認方法はこれまでと同様です。RESTサービスを40秒ごとに呼んでいることを確認します。
JBossFuse:admin@root> osgi:update 335 JBossFuse:admin@root> log:display -n 4 2015-03-19 20:09:59,869 | INFO | 68 - timer://bar | route23 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | REST : レスポンスメッセージ : <?xml version="1.0" encoding="UTF-8" standalone="yes"?><CustomerList><customer id="0010"><age>20</age><firstName>テスト</firstName><lastName>サンプル</lastName></customer></CustomerList> 2015-03-19 20:10:19,925 | INFO | ault-workqueue-3 | route24 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | メッセージ : OK : sample 2015-03-19 20:10:39,867 | INFO | p1425932069-1484 | route20 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | 2015-03-19 20:10:39,870 | INFO | 68 - timer://bar | route23 | ? ? | 142 - org.apache.camel.camel-core - 2.12.0.redhat-610379 | REST : レスポンスメッセージ : <?xml version="1.0" encoding="UTF-8" standalone="yes"?><CustomerList><customer id="0010"><age>20</age><firstName>テスト</firstName><lastName>サンプル</lastName></customer></CustomerList>
4.その他
インターネットを介して利用するようなサービスの中で、主なものは、独自のコンポーネントとして実装されています。
Webサービス関連のコンポーネント
コンポーネント | 概要 |
---|---|
AWS | Amazon Web Services連携のための各種コンポーネント。 AWS-CW、AWS-DDB、AWS-S3、AWS-SDB、AWS-SES、AWS-SNS、AWS-SQSなどの各コンポーネントが提供されている |
Facebook連携のためのコンポーネント | |
GAE | Google App Engine連携のための各種コンポーネント。 ghttp、gtask、gmail、gauth、gloginなどのコンポーネントが提供されている |
Salesforce | Salesforce連携のためのコンポーネント |
Twitter連携のためのコンポーネント |
Fuseには、Red Hatが提供するPaaS基盤であるOpenShift上で動作するカートリッジ、JBoss Fuse for xPaaSが提供されています。Webサービスを組み合わせて新規サービスを作るような場合、このJBoss Fuse for xPaaSを利用することで、クラウド基盤上で完結したシステムを構築することができます。
5.参考URL
- Webサービス開発の全般的な情報
https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.1/html/Apache_Camel_Development_Guide/CamelCxf.html - Camel CXFコンポーネント
https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.1/html/Apache_Camel_Component_Reference/IDU-CXF.html - Camel CXFRSコンポーネント
https://access.redhat.com/documentation/en-US/Red_Hat_JBoss_Fuse/6.1/html/Apache_Camel_Component_Reference/IDU-CXFRS.html - JBoss Fuse for xPaaS
https://developers.openshift.com/en/xpaas-fuse.html