はじめに
連載5回目の今回は、JBoss FuseにおいてWebサービスを利用する手順について解説します。
Webサービス編では、以下の内容を理解します。
- JBoss FuseにおけるWebサービスの構成
- CamelルートをWebサービス化する
- WebサービスをCamelルートから呼び出す
- その他
作成するものの構成は図1のようになります。
図1:今回作成するアプリケーション構成
作成したものはJBoss Fuseにデプロイしますので、必ず事前にJBoss Fuseを起動しておいてください。
$ cd <Fuseのインストールディレクトリ>/bin
$ ./fuse
1.JBoss FuseにおけるWebサービスの構成
JBoss FuseのWebサービスは、Apache CXFを用いて実現されています。また、Apache CamelにはCXF用のコンポーネントが用意されており、Camel ルートをWebサービス化したり、Camel ルートからWebサービスを呼び出したりできます(図2)。
図2:FuseのWebサービス構成
図2にもあるように、JBoss FuseではHTTPを利用したSOAPの他、JMSを利用したSOAPやRESTなども利用することができます(JBoss FuseにはSpring Web ServiceコンポーネントというSpringによる実装や、単にHTTPを呼び出すためのコンポーネントなども含まれますが、今回は説明を割愛します)。
2.CamelルートをWebサービス化する
ここでは、次の2つを実装します。
- SOAP/HTTP によるWebサービス化
- RESTful Webサービス化
各手順を確認する前に、事前準備をします。まず、これら2つのCamelルートを作成するためのFuseプロジェクトを作成します。
サーバー用Fuseプロジェクトの設定
設定 | 値 |
Group Id | com.mycompany(デフォルト) |
Artifact Id | camel-archetype-blueprintを選択後、wsserverに変更(図3) |
Version | 1.0.0-SNAPSHOT |
Package | (Group IdとArtifact Idから自動生成) |
図3:Fuseプロジェクトの作成
なお、Artifact IDとして「camel-archetype-blueprint」ではなく、「camel-archetype-cxf-code-first-blueprint」や「camel-archetype-cxf-contract-first-blueprint」を選択すると、SOAPサービスを利用したサンプルを確認することができます。
作成後、不要なクラスやCamelルート、テストコードは削除しておきます(図4の赤線箇所)。
図4:初期状態のFuseプロジェクト
次にFuseプロジェクト内の「pom.xml」に、以下の依存ライブラリ設定を追加します。必ずpom.xml内の ~ 内に記述するようにしてください。
リスト1: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>
SOAP/HTTP によるWebサービス化
【手順1】インターフェース作成
SOAPのインターフェースとなるプログラムを作成します。
リスト2
package com.mycompany.wsserver;
import javax.jws.WebService;
@WebService
public interface OrderEndpoint {
String order(String OrderID);
}
このインターフェースはString「OrderID」を受け取り、Stringデータを返します。インターフェースにはjavax.jws.WebServiceアノテーションを設定しています。
【手順2】Camelルート作成
以下のようなCamelルートを作成します([New]−[Camel XML File])。必ずOSGi Blueprintで作成するようにしてください。
SOAPサーバーのCamelルート設定
設定 | 値 |
RouteContainer | /wsserver/src/main/resources/OSGI-INF/blueprint |
File name | soapServer.xml |
Framework | OSGi Blueprint |
図5:SOAPサーバーのCamelルート
以下のように設定します。
各ノードの設定
位置 | 追加するノード | プロパティ | 設定 |
1 | Endpoint | Uri | cxf:bean:orderEndpoint |
2 | Transform | Expression | OK : ${body} |
| | Language | simple |
設定ができたら保存します。処理の内容は、Webサービスのリクエストを受けたら、メッセージとして「OK:」を付与して返すというものになります。
【手順3】Camelルートへの設定追加
【手順2】でプロセスの大枠を作成したら、追加で設定を入れていきます。Sourceモードでリスト3の強調されている行を追記します。
リスト3
<?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:cxfEndpoint id="orderEndpoint"
address="/order/"
serviceClass="com.mycompany.wsserver.OrderEndpoint"/>
<camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="cxf:bean:orderEndpoint"/>
<transform>
<constant>OK : ${body}</constant>
</transform>
</route>
</camelContext>
</blueprint>
この設定で追加しているのはCXFコンポーネントを利用するためのXMLスキーマの設定と、CXFコンポーネントが参照するWebサービスの設定です。XMLスキーマの設定(xmlns:cxf及び、xsi:schemaLocationに追加している箇所)についてはおまじないなので、この通りに設定してください。
Webサービスの設定では、以下のようなことをしています。
リスト4
<cxf:cxfEndpoint id="orderEndpoint" (1)
address="/order/"
serviceClass="com.mycompany.wsserver.OrderEndpoint"/> (2)
<camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="cxf:bean:orderEndpoint"/>
- uriの「cxf:bean:」の後に指定したIDで「cxf:cxfEndpoint」を参照する。
- 作成済みインターフェースを設定する。
【手順4】バンドルの作成
作成したJavaプログラム、Camelルートをバンドルとしてビルドします。JBoss Developer Studio(以下、JBDS)ではFUSEプロジェクトを右クリックし、[Run As]−[Maven Install]でビルドされます。ビルドされたバンドルは、Mavenリポジトリに格納されます。
【手順5】バンドルのインストール
Mavenリポジトリに格納されたバンドルをFuseサーバーインスタンスにインストールします。
JBossFuse:admin@root> osgi:install -s mvn:com.mycompany/wsserver/1.0.0-SNAPSHOT
Bundle ID: 334
【手順6】確認
SOAPサービスのインストールに成功していると、「http://localhost:8181/cxf」において一覧に登録されています。
図6:SOAPサービスの確認
この一覧に表示されるWSDLの横にあるリンクをクリックすると、作成したWebサービスに対応するWSDLが表示されます(図7)。
図7:作成したサービスに対応したWSDL
Webサービスの動作確認は、SoapUIなどを用いて行います。
図8:SoapUIによる確認
RESTful Webサービス化
CamelルートをRESTful Webサービス化する際には、CXFRSコンポーネントを利用します。手順はSOAPの場合とほぼ同じですので、もう少し中身を実装します。
【手順1】通信で使用するデータフォーマットの設定
RESTful Webサービスの通信で利用するデータフォーマットは、Javaのクラスとして作成します。ここでは、Cutromer.java(リスト5)、CustomerList.java(リスト6)の2つのクラスを作成します。
リスト5:Customer.java
package com.mycompany.wsserver.rest;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Customer {
private String id;
private String firstName;
private String lastName;
private int age;
public Customer(){}
public Customer(String id, String firstName, String lastName, int age) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getId() {
return id;
}
@XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
@XmlElement
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
@XmlElement
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Customer [id=").append(id).append(", firstName=")
.append(firstName).append(", lastName=").append(lastName)
.append(", age=").append(age).append("]");
return builder.toString();
}
}
リスト6:CustomerList.java
package com.mycompany.wsserver.rest;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="CustomerList")
public class CustomerList {
private List<Customer> customers;
public CustomerList(){
setCustomers(new ArrayList<Customer>());
}
public List<Customer> getCustomers() {
return customers;
}
@XmlElement(name="customer")
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
public void addCustomer(Customer customer){
customers.add(customer);
}
}
XML要素にはjavax.xml.bind.annotation.XmlElementアノテーションを付与します。その他、XmlRootElement、XmlAttributeアノテーションも同様です。
【手順2】インターフェース作成
SOAPの場合と同様にインターフェースを作成します。
リスト7:インターフェースの作成(CustomerEndpoint.java)
package com.mycompany.wsserver.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
public interface CustomerEndpoint {
@GET
@Path("/list")
@Produces("application/xml")
public CustomerList listCustomer();
@GET
@Path("/add/{id}/{firstname}/{lastname}/{age}")
@Produces("application/xml")
public CustomerList addCustomer(@PathParam("id") String id, @PathParam("firstname") String firstname, @PathParam("lastname") String lastname, @PathParam("age") int age);
}
javax.ws.rsで提供されている各種アノテーションを付与するところがポイントです。またそれぞれの戻り値として、CustomerListクラスを設定します。
【手順3】データ処理クラス作成
RESTfulインターフェースを介して、Camelルートに入ってきたデータを処理するためのクラスを作成します。上記listCustomerメソッド、addCustomerが呼ばれた際の処理内容を以下のように実装します(リスト8、リスト9)。
リスト8:listCustomerメソッドに対する処理(ListCustomerProcessor.java)
package com.mycompany.wsserver.rest;
import javax.ws.rs.core.Response;
import org.apache.camel.Processor;
import org.apache.camel.Exchange;
public class ListCustomerProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception { CustomerList list = new CustomerList ();
list.addCustomer(new Customer("0001", "一郎", "佐藤", 20));
list.addCustomer(new Customer("0002", "花子", "鈴木", 24));
Response r = Response.status(200).entity(list).build();
exchange.getOut().setBody(r);
}
}
リスト9:addCustomerメソッドに対する処理(AddCustomerProcessor.java)
package com.mycompany.wsserver.rest;
import java.util.List;
import javax.ws.rs.core.Response;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
public class AddCustomerProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
Message message = exchange.getIn();
@SuppressWarnings("unchecked")
List<Object> listBody = (List<Object>) message.getBody();
CustomerList list = new CustomerList();
String id = (String) listBody.get(0);
String firstname = (String) listBody.get(1);
String lastname = (String) listBody.get(2);
int age = (Integer) listBody.get(3);
list.addCustomer(new Customer(id,firstname,lastname,age));
Response r = Response.status(200).entity(list).build();
exchange.getOut().setBody(r);
}
}
Camelルート内に処理を埋め込む際には、org.apache.camel.Processorインターフェースを実装したクラスを作成します。このクラスでは、Camelルート内を流れるメッセージのコンテナであるorg.apache.camel.Exchangeクラスを操作します。ExchangeはJBoss Fuseを理解する上で重要なものなので、別途このページの最後で詳説します。また、RESTのレスポンスはjavax.ws.rs.core.Responseクラスを使用しています。
【手順4】Camelルート作成
ここまでに作成したJavaプログラムを利用する形で、以下のような新しいCamelルートを作成します。
RESTサーバーのCamelルート設定
設定 | 値 |
RouteContainer | /wsserver/src/main/resources/OSGI-INF/blueprint |
File name | restServer.xml |
Framework | OSGi Blueprint |
Designモード
図9:RESTサーバーのCamelルート
リスト10:RESTサーバー(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:cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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:rsServer id="restServer" address="/customer"/>
<bean id="listCustomerProcessor" class="com.mycompany.wsserver.rest.ListCustomerProcessor"/>
<bean id="addCustomerProcessor" class="com.mycompany.wsserver.rest.AddCustomerProcessor"/>
<camelContext trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<route>
<from uri="cxfrs:bean:restServer?resourceClasses=com.mycompany.wsserver.rest.CustomerEndpoint"/>
<choice>
<when>
<simple>${in.header.operationName} == 'listCustomer'</simple>
<process ref="listCustomerProcessor"/>
<log message="${body}"/>
</when>
<when>
<simple>${in.header.operationName} == 'addCustomer'</simple>
<process ref="addCustomerProcessor"/>
<log message="${body}"/>
</when>
</choice>
</route>
</camelContext>
</blueprint>
強調している行が、RESTサービスの設定です。SOAPの場合、参照クラスは別箇所で設定しましたが、RESTの場合は、URIに記述する点に注意してください。Javaによる実装を参照する場合には、を用います。参照先はで外出しにする点をご確認ください。また、条件判定で使用している${in.header.operationName}は、インターフェースCustomerEndpointに設定されているメソッド名です。
【手順5】ビルド、インストール、確認
ビルドや設定の確認方法は、全てSOAPの場合と同様です。JBoss Fuseへのインストールについてはすでにインストール済みなので、アップデートを行います。先ほどのBundle IDを控えていればそれを利用できますが、ない場合は、osgi:listコマンドで確認できます。特に作業をしていなければ、一番下に表示されるOSGiバンドルが該当のものなので、そのBundle IDを用いてosgi:updateを行います。
JBossFuse:admin@root> osgi:list
START LEVEL 100 , List Threshold: 50
ID State Blueprint Spring Level Name
[ 66] [Active ] [Created ] [ ] [ 50] Fabric8 :: Karaf Commands (1.0.0.redhat-379)
・・・ (全てのOSGiバンドルが表示される)
[ 305] [Active ] [ ] [ ] [ 60] wrap_mvn_org.postgresql_postgresql_9.3-1100-jdbc41 (0)
[ 334] [Active ] [Created ] [ ] [ 60] WS Server (1.0.0.SNAPSHOT)
JBossFuse:admin@root> osgi:update 334
JBossFuse:admin@root>
「http://localhost:8181/cxf」では、以下のように確認できます(図14)。
図10:RESTサーバーの確認
WADLも自動的に生成されているのが分かります。動作確認はブラウザを利用して行えます。
図11:List(http://localhost:8181/cxf/customer/list)のレスポンス
図12:Add(http://localhost:8181/cxf/customer/add/10/test/sample/20)処理のレスポンス
Exchangeについて
Exchangeは、Camelルート内を流れるメッセージのコンテナです。下図のような構成となっています。
図13:Exchangeの構成
Exchangeの構成要素一覧
項目 | 概要 |
ExchangeID | Camelが自動採番するID |
Exception | エラー発生時に設定される |
MEP(Message Exchange Pattern) | InOnly(片方向)、InOut(双方向) |
Properties | ルート全体のプロパティ |
In Message | メッセージを保持する |
Out Message | MEPがInOutの時だけ存在する。 応答メッセージを保持する。 |
この中のIn Message、Out MessageのMessage(org.apache.camel.Message)は、次のような構造になっています。
図14:Messageの構成
Messageの構成要素一覧
項目 | 概要 |
Headers | ヘッダ情報。java.util.Mapで実装されている |
Attachments | Webサービスやe-mailなどで使用されるアタッチメント |
Body | メッセージが運ぶデータ本体。データ型はObjectとして定義されている。 |
faultフラグ | outputとfaultを区別するためのフラグ |
Apache Camelは、Camelルートの中でこのExchangeに含まれるMessageを加工していくことで処理を行います。
図15:Camelルート処理イメージ
図11のようなCamelルートがある場合、Exchangeは次のように動きます(一例です)。
図16:Exchangeの推移
各EndpointやProcessorにおいて、Out Messageがnullの場合、In Messageが次の処理にそのまま引き継がれます。一方Out Messageに値が入っている場合、その値がそのまま次の処理のIn Messageとなります。Out Messageに値が入ってくるかどうかは、MEPの設定に依存する場合があります。MEPが「InOnly」の設定になっている場合(多くのEndpointでデフォルト設定)、Camelは処理を呼び出して、そのレスポンスを受け取りません。また、上記ルートでConsumer Endpointが「InOut」の設定になっている場合、Consumer Endpointに返される内容は、Producer EndpointのOut Messageになります。
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)。
図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モード
図18:SOAPクライアントのCamelクライアント
リスト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モード
図19:RESTクライアントのCamelルート
リスト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>
複数のをGUIで設定していく作業は面倒ですが、Sourceモードでコピーペーストして設定することで、作業負荷を減らすことができます。
RESTサービスを呼び出しているのは、Sourceモード(リスト14)の強調箇所です。注意点として、cxfrsのデフォルトのMessage Exchange PatternはInOnlyなので、そのままの設定ではにおいてログ出力されない点が挙げられます。そこでにおいてMEPをInOutに変更し、レスポンスメッセージを取得するようにします。
【手順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 | Facebook連携のためのコンポーネント |
GAE | Google App Engine連携のための各種コンポーネント。 ghttp、gtask、gmail、gauth、gloginなどのコンポーネントが提供されている |
Salesforce | Salesforce連携のためのコンポーネント |
Twitter | Twitter連携のためのコンポーネント |
Fuseには、Red Hatが提供するPaaS基盤であるOpenShift上で動作するカートリッジ、JBoss Fuse for xPaaSが提供されています。Webサービスを組み合わせて新規サービスを作るような場合、このJBoss Fuse for xPaaSを利用することで、クラウド基盤上で完結したシステムを構築することができます。
5.参考URL