Post on 20-May-2015
description
Spring Integration 概要!
@Kuro
本日のアジェンダ1. Spring Integrationとは
2. Spring Integrationの基本構成要素
3. サンプル紹介
4. 他製品との比較
���2
Spring Integrationとは?• Spring Projectのひとつ
• EIP (Enterprise Integration Patterns) に基づくアプリケーション開発をサポートするフレームワーク
• 多彩なアダプタで外部サービスとの接続もサポート
• Spring XD(ビックデータ解析)のベースにもなっている
���3
EIPとは?
Message Router
Publish-Subscribe ChannelPolling Consumer���5
本日のアジェンダ1. Spring Integrationとは
2. Spring Integrationの基本構成要素
3. サンプル紹介
4. 他製品との比較
���6
Spring Integration の 基本構成要素
• Message • Message Channel • Endpoint
���7
Channel Endpoint Channel
Message
Endpoint Endpoint
Message
Message• Spring Integration内でやり取りするデータを示すオブジェクト
• HeaderとPayloadで構成
• HeaderはMap<String, Object>、Payloadは任意のオブジェクト
• Headerにはid, timestamp, priorityなどの予め定義されている項目もある
public interface Message<T> { MessageHeaders getHeaders(); T getPayload();}!public final class MessageHeaders implements Map<String, Object>, Serializable {…省略…}
���8
Message Channel• Messageの伝送路を示す
• 2種類の配信モード(P2P, Pub/Sub)
• P2Pモードは1つの受信者に届く
• Pub/Subは登録している受信者すべてに届く
���9
• MessageChannel
!
• PollableChannel
!
• SubscribableChannel
Message Channel
public interface MessageChannel { boolean send(Message<?> message); boolean send(Message<?> message, long timeout);}
public interface PollableChannel extends MessageChannel { Message<?> receive(); Message<?> receive(long timeout);}
public interface SubscribableChannel extends MessageChannel { boolean subscribe(MessageHandler handler); boolean unsubscribe(MessageHandler handler);}
能動的に受信する。
登録したHandler に配信される。
送信のみ定義。
���10
Message Channel• PollableChannel
• QueueChannel:FIFOのキュー。
• PriorityChannel:PRIORITYヘッダの値で並び替え。
• RendezvousChannel:容量0のキュー。
• SubscribableChannel
• PublishSubscribeChannel:すべてのSubscriberに配布。
• DirectChannel:1つのSubscriberのみに配布。channelのデフォルト
• ExecuterChannel:配布先が別スレッドで動作。���11
Endpoint• Service Activator
• Channel Adapter
• Message Bridge
• Gateway
• Resequencer
• Transformer
• Filter
• Router
• Splitter
• Aggregator
���12
本日のアジェンダ1. Spring Integrationとは
2. Spring Integrationの基本構成要素
3. サンプル紹介
4. 他製品との比較
���13
Hello World
String
Hello World Service
Service Activator String
output = “Hello, ” + input;
Hello, KuroKuro
Channel Endpoint POJO凡例: ���14
下準備 <!-- Spring Integration --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>2.2.6.RELEASE</version> </dependency>
pom.xml
jarファイル
���15
Hello World:実装 <int:channel id="inputChannel"/> <int:channel id="outputChannel"> <int:queue capacity="10"/> </int:channel> ! <!-- inputChannel => HelloService#sayHello => outputChannel --> <int:service-activator input-channel="inputChannel" output-channel="outputChannel" ref="helloService" method="sayHello"/> ! <beans:bean id="helloService" class=“net.spring.HelloService"/>
public class HelloService { ! public String sayHello(String name) { return “Hello, " + name; } }
bean定義ファイル
HelloService.java
1. 入出力Channelを定義。
2. Service Activatorを定義。
3. Serviceはbeanとして定義。
���16
Hello World:テスト @Test public void helloWorldTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorld.xml");! // 送信メッセージ Message<String> message = MessageBuilder.<String> withPayload("Kuro").build();! // 送信 MessageChannel inputChannel = applicationContext.getBean(“inputChannel", MessageChannel.class); inputChannel.send(message);! // 受信 PollableChannel outputChannel = applicationContext.getBean("outputChannel", PollableChannel.class); Message<String> output = (Message<String>) outputChannel.receive();! // 確認 logger.debug("output: " + output.getPayload()); assertThat(output.getPayload(), is("Hello, Kuro")); }
���17
サービス呼出方法をかえる
• 同期Gateway
• 非同期Gateway
• File Adapter(Channel Adapter)
���18
Hello World:同期Gateway
String
Hello World Service
Service Activator
StringHello, Kuro
Kuro
Gateway
Channel Endpoint POJO凡例: ���19
Hello World:同期Gateway
<!-- HelloWorldService 同期Gateway --> <int:gateway id="HelloWorldServiceGateway" default-request-channel="inputChannel" default-reply-channel="outputChannel" service-interface=“net.spring.gateway.HelloWorldServiceGateway" />
public interface HelloWorldServiceGateway { public String sayHello(String name);}
1. interfaseを定義。
2. request-channelとreply-channelに 入力と出力Channelを設定。
bean定義ファイル
HelloWorldServiceGateway.java
���20
Hello World:同期Gateway
@Test public void helloWorldServiceGatewayTest() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "/META-INF/spring/integration/helloWorld.xml");! HelloWorldServiceGateway gateway = applicationContext .getBean("HelloWorldServiceGateway", HelloWorldServiceGateway.class); String message = gateway.sayHello("Kuro");! assertThat(message, is("Hello, Kuro")); }
HelloWorldTest.java
呼出がシンプルになる。
���21
Hello World:非同期Gateway
String
Hello World Service
Service Activator
StringHello, Kuro
Kuro
Gateway
Channel Endpoint POJO凡例: ���22
Hello World:非同期Gateway
<!-- HelloWorldService 非同期Gateway --> <int:gateway id="HelloWorldServiceGatewayAsync" default-request-channel="inputChannel" default-reply-channel="outputChannel" service-interface="net.spring.gateway.HelloWorldServiceGatewayAsync" />
public interface HelloWorldServiceGatewayAsync { public Future<String> sayHello(String name); }
HelloWorldServiceGatewayAsync.java
bean定義ファイル
1. interfaseを定義。返り値の方はFuture。
2. request-channelとreply-channelに 入力と出力Channelを設定。
���23
Hello World:非同期Gateway
@Test public void helloWorldServiceGatewayAsyncTest() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorld.xml");! HelloWorldServiceGatewayAsync gateway = applicationContext .getBean("HelloWorldServiceGatewayAsync", HelloWorldServiceGatewayAsync.class); Future<String> future = gateway.sayHello("Kuro");! String message = null; while (true) { if (future.isDone()) { message = future.get(); break; } } assertThat(message, is("Hello, Kuro")); }
非同期で結果を取得。
HelloWorldTest.java
���24
Hello World:File Adapter
String
Hello World Service
Service Activator String
Hello, Kuro
Channel Endpoint POJO凡例:
File Adapter TransformerFile
Kuro
hello-world-kuro.txt
���25
Hello World:File Adapter <!-- HelloWorldScervice FileAdapter --> <int-file:inbound-channel-adapter id="fileAdapter" directory="file:${java.io.tmpdir}" channel="filesInChannel" filename-pattern="hello-world-*.txt"> <int:poller fixed-rate="1000" /> </int-file:inbound-channel-adapter>! <int-file:file-to-string-transformer delete-files="true" charset="UTF-8" input-channel="filesInChannel" output-channel="inputChannel" />
bean定義ファイル
���26
Hello World:File Adapter @Test public void helloWorldServiceFileAdapterTest() throws Exception { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorld.xml");! // ポーリングされるディレクトリにファイルを出力する。 String tmpdirPath = System.getProperty("java.io.tmpdir"); String inputFilePath = tmpdirPath + "hello-world-kuro.txt"; File nameFile = new File(inputFilePath); OutputStream outputStream = new FileOutputStream(nameFile); outputStream.write("Kuro".getBytes()); outputStream.close(); // 受信 PollableChannel outputChannel = applicationContext .getBean("outputChannel", PollableChannel.class); Message<String> output = (Message<String>) outputChannel.receive(); assertThat(output.getPayload(), is("Hello, Kuro")); }
HelloWorldTest.java
���27
File以外のAdapter• AMQP
• Feed
• File
• FTP/FTPS
• GemFile
• HTTP
• TCP/UDP
• JDBC
• JPA
• JMS
• MongoDB
• Redis
• Resource
• RMI
• SFTP
• Stream
• Syslog
• Tail
• Web Service
• XML
• XMPP
• AWS*
• Kafka*
• MQTT*
• Print*
• SMB*
• SMPP*
• Splunk*
• Voldemort*
• XQuery**extensions (https://github.com/spring-projects/spring-integration-extensions)���28
• STSを使うとフローを可視化できる。 • 使い勝手はあまりよくない?
STS(Spring Tool Suite)
���29
その他のEndpoint• Spritter
• Router
• Aggregator
���30
メッセージの永続化• キューにMessageStoreを設定することで可能 • サポートしているMessageStore
• JDBC • Redis • MongoDB • Gemfire
• ただし、キューの永続化にRDBMSは非推奨
���31
<int:channel id="outputChannel"> <int:queue capacity="10" message-store="refMessagestore"/> </int:channel>
本日のアジェンダ1. Spring Integrationとは
2. Spring Integrationの基本構成要素
3. サンプル紹介
4. 他製品との比較
���32
Spring Integrationの特徴• 既存のSpringアプリケーションとの相性がよい。
• POJOを基本としているためコンポーネントの試験がしやすい。
• 軽量(JUnitやWebアプリから起動可能)。
• インストール不要(It’s a framework, not an application)。
• OSSなのでソースコードを読んで拡張可能!
• XML地獄…。
���33
他製品との比較• Apache Camel
• Mule ESB
���34
Mule ESB• エディタが非常にリッチ。
• 要インストール(but easy)。
• 実績多し。
• 商用版のみの機能もあり。
���35
Apache Camel
public void configure() { from(“file:src/data?noop=true") .choice() .when(xpath(“/person/city='London'")) .to("file:target/messages/uk") .otherwise() .to("file:target/messages/others");}
• Spring Integrationと内容はほぼ一緒。 • Springとも連携できる。 • コンポーネントの種類は多い(AWSやFacebookやgmail)。 • フロー記述のDSLはSpring Integrationより明快。 • 日本Apache Camelユーザ会が存在(国内実績もあり)。
JACUG応援キャラクターのアイシャちゃん���36
結局使いどころは?• 既存のSpringアプリケーションにフロー制御を追加する要件が出た場合
• 信頼性(永続化)、運用性(リトライ、リスタート)、拡張性(クラスタリング)については要処理方式検討
• SpringXDにも期待
���37
まとめ• Spring Integration
• EIPの参照実装 • Spring Integrationの基本
• Message • MessageChannel • Endpoint
• Message • Header • Payload
• MessageChannel • P2P • Pub/Sub
• Endpoint • ServiceActivator
• 呼出 • Gateway • Channel Adapter
• 他製品 • Mule ESB • Apache Camel
���38
参考リンク集• Spring Integrationhttp://projects.spring.io/spring-integration/
• Spring Integration Sampleshttps://github.com/spring-projects/spring-integration-samples
• Spring Integration Extensionshttps://github.com/spring-projects/spring-integration-extensions
• Spring Integration and EIP Introductionhttp://www.slideshare.net/iweinfuld/spring-integration-and-eip-introduction
• Light-weight, Open-source Integration: Apache Camel vs. Spring Integrationhttp://java.dzone.com/articles/light-weight-open-source
• Which Integration Framework to use ‒ Spring Integration, Mule ESB or Apache Camel?http://www.kai-waehner.de/blog/2012/01/10/spoilt-for-choice-which-integration-framework-to-use-spring-integration-mule-esb-or-apache-camel/
• Apache Camel http://camel.apache.org/
• 日本Apache Camelユーザー会http://sourceforge.jp/projects/cameluserjp/wiki/FrontPage
• Mule ESBhttp://www.mulesoft.org/ ���39