spring integration概要

39
Spring Integration 概要 @Kuro

Upload: kuroiwa

Post on 20-May-2015

2.560 views

Category:

Technology


0 download

DESCRIPTION

2013-12-26 勉強会資料

TRANSCRIPT

Page 1: Spring integration概要

Spring Integration 概要!

@Kuro

Page 2: Spring integration概要

本日のアジェンダ1. Spring Integrationとは

2. Spring Integrationの基本構成要素

3. サンプル紹介

4. 他製品との比較

���2

Page 3: Spring integration概要

Spring Integrationとは?• Spring Projectのひとつ

• EIP (Enterprise Integration Patterns) に基づくアプリケーション開発をサポートするフレームワーク

• 多彩なアダプタで外部サービスとの接続もサポート

• Spring XD(ビックデータ解析)のベースにもなっている

���3

Page 4: Spring integration概要

EIPとは?• エンタープライズ統合の方式をパターン化

• 65パターン

• http://www.eaipatterns.com/

���4

Page 5: Spring integration概要

EIPとは?

Message Router

Publish-Subscribe ChannelPolling Consumer���5

Page 6: Spring integration概要

本日のアジェンダ1. Spring Integrationとは

2. Spring Integrationの基本構成要素

3. サンプル紹介

4. 他製品との比較

���6

Page 7: Spring integration概要

Spring Integration の 基本構成要素

• Message • Message Channel • Endpoint

���7

Channel Endpoint Channel

Message

Endpoint Endpoint

Message

Page 8: Spring integration概要

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

Page 9: Spring integration概要

Message Channel• Messageの伝送路を示す

• 2種類の配信モード(P2P, Pub/Sub)

• P2Pモードは1つの受信者に届く

• Pub/Subは登録している受信者すべてに届く

���9

Page 10: Spring integration概要

• 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

Page 11: Spring integration概要

Message Channel• PollableChannel

• QueueChannel:FIFOのキュー。

• PriorityChannel:PRIORITYヘッダの値で並び替え。

• RendezvousChannel:容量0のキュー。

• SubscribableChannel

• PublishSubscribeChannel:すべてのSubscriberに配布。

• DirectChannel:1つのSubscriberのみに配布。channelのデフォルト

• ExecuterChannel:配布先が別スレッドで動作。���11

Page 12: Spring integration概要

Endpoint• Service Activator

• Channel Adapter

• Message Bridge

• Gateway

• Resequencer

• Transformer

• Filter

• Router

• Splitter

• Aggregator

���12

Page 13: Spring integration概要

本日のアジェンダ1. Spring Integrationとは

2. Spring Integrationの基本構成要素

3. サンプル紹介

4. 他製品との比較

���13

Page 14: Spring integration概要

Hello World

String

Hello World Service

Service Activator String

output = “Hello, ” + input;

Hello, KuroKuro

Channel Endpoint POJO凡例: ���14

Page 15: Spring integration概要

下準備 <!-- Spring Integration --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>2.2.6.RELEASE</version> </dependency>

pom.xml

jarファイル

���15

Page 16: Spring integration概要

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

Page 17: Spring integration概要

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

Page 18: Spring integration概要

サービス呼出方法をかえる

• 同期Gateway

• 非同期Gateway

• File Adapter(Channel Adapter)

���18

Page 19: Spring integration概要

Hello World:同期Gateway

String

Hello World Service

Service Activator

StringHello, Kuro

Kuro

Gateway

Channel Endpoint POJO凡例: ���19

Page 20: Spring integration概要

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

Page 21: Spring integration概要

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

Page 22: Spring integration概要

Hello World:非同期Gateway

String

Hello World Service

Service Activator

StringHello, Kuro

Kuro

Gateway

Channel Endpoint POJO凡例: ���22

Page 23: Spring integration概要

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

Page 24: Spring integration概要

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

Page 25: Spring integration概要

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

Page 26: Spring integration概要

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

Page 27: Spring integration概要

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

Page 28: Spring integration概要

File以外のAdapter• AMQP

• Feed

• File

• FTP/FTPS

• GemFile

• HTTP

• TCP/UDP

• JDBC

• JPA

• JMS

• Mail

• MongoDB

• Redis

• Resource

• RMI

• SFTP

• Stream

• Syslog

• Tail

• Twitter

• Web Service

• XML

• XMPP

• AWS*

• Kafka*

• MQTT*

• Print*

• SMB*

• SMPP*

• Splunk*

• Voldemort*

• XQuery**extensions (https://github.com/spring-projects/spring-integration-extensions)���28

Page 29: Spring integration概要

• STSを使うとフローを可視化できる。 • 使い勝手はあまりよくない?

STS(Spring Tool Suite)

���29

Page 30: Spring integration概要

その他のEndpoint• Spritter

• Router

• Aggregator

���30

Page 31: Spring integration概要

メッセージの永続化• キューにMessageStoreを設定することで可能 • サポートしているMessageStore

• JDBC • Redis • MongoDB • Gemfire

• ただし、キューの永続化にRDBMSは非推奨

���31

<int:channel id="outputChannel"> <int:queue capacity="10" message-store="refMessagestore"/> </int:channel>

Page 32: Spring integration概要

本日のアジェンダ1. Spring Integrationとは

2. Spring Integrationの基本構成要素

3. サンプル紹介

4. 他製品との比較

���32

Page 33: Spring integration概要

Spring Integrationの特徴• 既存のSpringアプリケーションとの相性がよい。

• POJOを基本としているためコンポーネントの試験がしやすい。

• 軽量(JUnitやWebアプリから起動可能)。

• インストール不要(It’s a framework, not an application)。

• OSSなのでソースコードを読んで拡張可能!

• XML地獄…。

���33

Page 34: Spring integration概要

他製品との比較• Apache Camel

• Mule ESB

���34

Page 35: Spring integration概要

Mule ESB• エディタが非常にリッチ。

• 要インストール(but easy)。

• 実績多し。

• 商用版のみの機能もあり。

���35

Page 36: Spring integration概要

Apache Camel

public void configure() { from(“file:src/data?noop=true") .choice() .when(xpath(“/person/city=&#39;London&#39;")) .to("file:target/messages/uk") .otherwise() .to("file:target/messages/others");}

• Spring Integrationと内容はほぼ一緒。 • Springとも連携できる。 • コンポーネントの種類は多い(AWSやFacebookやgmail)。 • フロー記述のDSLはSpring Integrationより明快。 • 日本Apache Camelユーザ会が存在(国内実績もあり)。

JACUG応援キャラクターのアイシャちゃん���36

Page 37: Spring integration概要

結局使いどころは?• 既存のSpringアプリケーションにフロー制御を追加する要件が出た場合

• 信頼性(永続化)、運用性(リトライ、リスタート)、拡張性(クラスタリング)については要処理方式検討

• SpringXDにも期待

���37

Page 38: Spring integration概要

まとめ• 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

Page 39: Spring integration概要

参考リンク集• 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