Presentation is loading. Please wait.

Presentation is loading. Please wait.

Apache Camel コンポーネント開発 @ssogabe Data Format Component Apache Camel コンポーネント開発 @ssogabe.

Similar presentations


Presentation on theme: "Apache Camel コンポーネント開発 @ssogabe Data Format Component Apache Camel コンポーネント開発 @ssogabe."— Presentation transcript:

1 Apache Camel コンポーネント開発 @ssogabe
Data Format Component Apache Camel コンポーネント開発 @ssogabe

2 概要 Apache Camelがサポートするデータフォーマットを変換するData Formatコンポーネントの仕組みを理解し、簡単なコンポーネントを作成する。 題材として、Amateras Projectが提供するXLSBeansを使用し、Excelで作成された定型フォーマットからデータを読み取り、JavaBeansに変換する。

3 Data Formatとは EIPにおけるMessage Translator(メッセージ変換)を提供する仕組み
Camelでは、コンポーネントとして、以下のコンポーネントを提供 Bindy JAXB Zip Jackson ...

4 使用例 JAXBコンポーネントを使って、XML⇔JavaBeans
<camelContext id="camel" xmlns="   <dataFormats> <!– JAXBのオプション設定 -->     <jaxb id="myJaxb" prettyPrint="true" />   </dataFormats> <!– JavaBeans ⇒ XML -->   <route>     <from uri="direct:start"/>     <marshal ref="myJaxb"/>     <to uri="direct:marshalled"/>   </route> <!– XML ⇒ JavaBeans -->     <from uri="direct:marshalled"/>     <unmarshal ref="myJaxb"/>     <to uri="mock:result"/>   </route>  </camelContext> <marshal> <jaxb prettyPrint=“true” /> </marshal> でもOK

5 XLSBeansとは Amateras Projectが提供する、アノテーションを付与したJava BeansとExcelをマッピングするライブラリ @Sheet(name = "UserList") public class UserList { @LabelledCell(label = "title", type = LabelledCellType.Right) public String title; @HorizontalRecords(tableLabel = "User List", recordClass = User.class) public List<User> users; } シートの定義 public class User { @Column(columnName = "id") public int id; @Column(columnName = "name") public String name; @Column(columnName = "gender") public String gender; @Column(columnName = "age") public int age; } シートをマッピング UserList userList = (UserList) XMLSBeans().load( new FileInputStream(“user.xlsx”), UserList.class) for (User user : userList.users) { ...... } リストに含まれるユーザの定義

6 仕様 変換対象となるExcelファイルは、Fileコンシューマーなどで取得し、ExchangeのBODYに設定
Spring XMLで、Excelファイルから変換するJavaBeansの完全修飾名(objectType)を指定 XLSX形式のみサポート <camelContext xmlns=" <route> <from uri="direct:unmarshal"/> <unmarshal> <custom ref="xlsBeansDataFormat" /> </unmarshal> <to uri="mock:unmarshal" /> </route> </camelContext> <bean id="xlsBeansDataFormat" class=“jp.co.nttcom.eai.component.xlsbeans.XLSBeansDataFormat" > <property name="objectType" value="jp.co.nttcom.eai.model.UserList" /> </bean>

7 Maven Projectの作成 (1) mvn archetype:generate を実行し、CamelのData Format向けMaven Projectの雛形を生成 mvn eclipse:eclipse を実行し、Eclipseの.classpathなどのファイルを作成 C:\mvn archetype:generate \ -DarchetypeGroupId=org.apache.camel.archetypes \ -DarchetypeArtifactId=camel-archetype-dataformat \ -DarchetypeVersion= \ -DgroupId=osseai \ -DartifactId=xlsbeans \ -Dversion=1.0-SNAPSHOT \ -Dpackage=jp.co.nttcom.eai.component.xlsbeans \ -Dname=XLSBeans \ -Dscheme=xlsbeans

8 Maven Projectの作成 (2) 以下のディレクトリ、ファイルが生成される

9 依存ライブラリの追加 XLSBeansを使用できるようにpom.xmlを修正 <dependencies> (snip)
<dependency> <groupId>jp.sf.amateras.xlsbeans</groupId> <artifactId>xlsbeans</artifactId> <version>1.2.5</version> </dependency> </dependencies> <repositories> <repository> <id>amateras</id> <name>Project Amateras Maven2 Repository</name> <url> </repository> </repositories>

10 ビルドの確認 ビルドできることを確認する C:\xlsbeans\mvn package
[INFO] Scanning for projects... [INFO] [INFO] [INFO] Building Camel XLSBeans Data format 1.0-SNAPSHOT (snip) [INFO] --- maven-jar-plugin:2.3.2:jar xlsbeans --- [INFO] Building jar: c:\home\tmp\xlsbeans\target\xlsbeans-1.0-SNAPSHOT.jar [INFO] BUILD SUCCESS [INFO] Total time: s [INFO] Finished at: Wed Jul 16 10:52:18 JST 2014 [INFO] Final Memory: 7M/17M

11 DataFormatインタフェース Excel⇔JavaBeansを行うメソッドを提供 必須のインタフェース
package org.apache.camel.spi; public interface DataFormat { // JavaBeans の変換 今回は実装しない void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception; // Excel ⇒ JavaBeansの変換を行う // streamは、Excelファイルのストリーム形式 Object unmarshal(Exchange exchange, InputStream stream) throws Exception; }

12 ServiceSupportクラス コンポーネントのライフサイクルを制御するクラス
インスタンスの生成など、初期処理、終了処理が必要な場合は、このクラスを継承する package org.apache.camel.support; public abstract class ServiceSupport implements StatefulService { // 初期処理 protected abstract void doStart() throws Exception; // 終了処理 protected abstract void doStop() throws Exception; }

13 CamelContextAwareインタフェース
Camel本体がCamelContextをインジェクト 今回は使用しない package org.apache.camel; public interface CamelContextAware { // CamelContextの設定 void setCamelContext(CamelContext camelContext); // CamelContextの取得 CamelContext getCamelContext(); }

14 XLSBeansDataFormatの実装(1)
パラメータはフィールドとして定義 objectTypeの確認とXLSBeansのインスタンスを生成 public class XLSBeansDataFormat extends ServiceSupport implements DataFormat { private Class objectType; // setter/getterは必要 private XLSBeans xlsBeans; @Override protected void doStart() throws Exception { // objectTypeは必須 ObjectHelper.notNull(objectType, "objectType"); xlsBeans = new XLSBeans(); } protected void doStop() throws Exception { // do nothing

15 XLSBeansDataFormatの実装(2)
unmarshalのみ実装 XLSBeansのインスタンスは、スレッドで共有しても問題ないので、先に生成しておく @Override public void marshal(Exchange exchange, Object graph, OutputStream stream) throws Exception { throw new UnsupportedOperationException("Not supported yet."); } public Object unmarshal(Exchange exchange, InputStream stream) // XLSX形式のみ対応(固定) return xlsBeans.load(stream, objectType, WorkbookFinder.TYPE_XSSF);

16 テストライブラリの追加 Camelでは、いくつかのテストをサポートするライブラリを提供
Spring XMLで作成したルートをテストするために、pom.xmlに以下を追加 <dependencies> (snip) <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-test-spring</artifactId> <version>2.13.2</version> <scope>test</scope> </dependency> </dependencies>

17 タイトル部分(背景が水色)は変更しないこと
テスト資材の配置 (1) テスト対象のExcelファイルuserlist.xlsxを、src\test\resources\jp\co\nttcom\eai\component\xlsbeansに配置 タイトル部分(背景が水色)は変更しないこと シート名は”UserList”

18 テスト資材の配置 (2) src\test\jp\co\nttcom\eai\modelに配置 Excelシートをマッピングするクラスを作成
package jp.co.nttcom.eai,model; (snip) @Sheet(name = "UserList") public class UserList { @LabelledCell(label = "title", type = LabelledCellType.Right) public String title; @HorizontalRecords(tableLabel = "User List", recordClass = User.class) public List<User> users; } シート名 内容が”title”のセルの右側のセルの値をtitleに設定 “User List”のセルの下にある表の行の内容をUserクラスにマッピングする

19 テスト資材の配置 (3) “User List”テーブルの1行の情報をマッピングするUserクラスを作成 “id”列の値をマッピング
package jp.co.nttcom.eai,model; (snip) public class User { @Column(columnName = "id") public int id; @Column(columnName = "name") public String name; @Column(columnName = "gender") public String gender; @Column(columnName = "age") public int age; } “id”列の値をマッピング

20 テストするルートの作成 テストするルートは、”テストクラス名”-context.xml Excelシートと同じディレクトリに配置する。
テストクラス名がXLSBeansDataFormatTest.javaの場合は、XLSBeansDataFormatTest-context.xml Excelシートと同じディレクトリに配置する。 <?xml version="1.0" encoding="UTF-8" ?> <beans ... > <camelContext xmlns=" <route> <from uri="direct:unmarshal"/> <unmarshal> <custom ref="xlsBeansDataFormat" /> </unmarshal> <to uri="mock:unmarshal" /> </route> </camelContext> <bean id="xlsBeansDataFormat" class=“jp.co.nttcom.eai.component.xlsbeans.XLSBeansDataFormat" > <property name="objectType" value="jp.co.nttcom.eai.model.UserList" /> </bean> </beans>

21 テストクラスの作成(1) 作成したExcelファイル、ルートを使用したテストクラス(XLSBeansDataFormatTest)を作成
package jp.co.nttcom.eai.component.xlsbeans; (snip) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class XLSBeansDataFormatTest { @Produce(uri = "direct:unmarshal") private ProducerTemplate template; @EndpointInject(uri = "mock:unmarshal") private MockEndpoint mock; : テストルートの”<from uri=“direct:unmarshal”/>”に データを送付するためのオブジェクト。 インジェクトされる テストルートの ” <to uri=“mock:unmarshal” />”のモック

22 テストクラスの作成(2) <to uri=“mock:unmarshal” />が受け取るBODYが、UserListのインスタンスであることなどを確認 @Test public void testUnmarshal() throws Exception { // 期待値の設定 // Exchangeを1つ受け取り、BODYがUserListのインスタンスであること mock.expectedMessageCount(1); mock.message(0).body().isInstanceOf(UserList.class); // テスト用のExcelファイルを、BODYとして送信する String fileName = getClass().getResource("userlist.xlsx").getFile(); template.sendBody(new File(fileName)); // 期待値を満たしていることを確認 mock.assertIsSatisfied(); // BODYを取得して、タイトル、Userの数を確認 UserList userList = mock.getReceivedExchanges().get(0).getIn().getBody(UserList.class); assertThat(userList.title, is("ユーザリスト")); assertThat(userList.users, hasSize(5)); }

23 共有できるので、データ変換のオプションとして提供
モデラーでのコンポーネント Spring XMLをもとに、モデラーのコンポーネントをどのように提供するか検討する データ変換として提供 <camelContext xmlns=" <route> <from uri="direct:unmarshal"/> <unmarshal> <custom ref="xlsBeansDataFormat" /> </unmarshal> <to uri="mock:unmarshal" /> </route> </camelContext> <bean id="xlsBeansDataFormat" class=“jp.co.nttcom.eai.component.xlsbeans.XLSBeansDataFormat" > <property name="objectType" value="jp.co.nttcom.eai.model.UserList" /> </bean> 共有できるので、データ変換のオプションとして提供 objectTypeはパラメータ


Download ppt "Apache Camel コンポーネント開発 @ssogabe Data Format Component Apache Camel コンポーネント開発 @ssogabe."

Similar presentations


Ads by Google