SlideShare une entreprise Scribd logo
1  sur  27
Spring Integration
超入門
まずはどんなものか触ってみよう!
@yasutkga
はじめに
なぜ、このスライドを作ったか
 「なんだか難しそうだ」という根拠のない感覚を打ち消すため
 Spring Integration を導入する場合に「学習コストが~」と導入を拒否される現実を
緩和するため
対象読者
 Spring Integration を全く知らない人
 Spring Integration に興味はあっても、調査が後回しになっている人
注意
 間違いや認識の違いがありましたら、指摘頂ければ幸いです
Agenda
 Spring Integration とは
 簡単なアプリケーションの作成
 STS3 の integration-graph を利用
 Spring Integration の Java DSL を利用
Main Projects Description
Spring Boot Takes an opinionated view of building Spring applications and gets you up and running as
quickly as possible.
Spring Framework Provides core support for dependency injection, transaction management, web apps, data
access, messaging, and more.
Spring Cloud Provides a set of tools for common patterns in distributed systems. Useful for building and
deploying microservices.
Spring Cloud Data Flow Provides an orchestration service for composable data microservice applications on
modern runtimes.
Spring Data Provides a consistent approach to data access – relational, non-relational, map-reduce,
and beyond.
Spring Integration Supports the well-known Enterprise Integration Patterns through lightweight messaging
and declarative adapters.
Spring Batch Simplifies and optimizes the work of processing high-volume batch operations.
Spring Security Protects your application with comprehensive and extensible authentication and
authorization support.
Spring Projects
https://spring.io/projects
Spring Integration
https://spring.io/projects/spring-integration
 Enterprise Integration Pattern の実装
 Message
 Endpoint
 Channel
 Aggregator
 Filter
 Transformer
 REST/HTTP, FTP/SFTP, TCP/UDP, JMS, Email etc.
Spring Integration : https://spring.io/projects/spring-integration
Enterprise Integration Pattern : https://www.enterpriseintegrationpatterns.com/
どういうときに使うの?
 同期・非同期メッセージング、リソース間を接続したいとき
 サポートされている Endpoint が多いので作らなくて良い
 とはいえ、要件に適合しているかは要チェック
 Pipe and Filters Pattern によりソフトウェアの構造をシンプルにしたいとき
 接続プロトコルの変更などに対応しやすい
 処理を変更・追加しやすい
 業務特化の Adapter を部品化して再利用しやすい
Integration Endpoints : https://docs.spring.io/spring-integration/docs/current/reference/html/index.html
Pattern-Oriented Software Architecture : https://en.wikipedia.org/wiki/Pattern-Oriented_Software_Architecture
前提環境
 Windows
 Pleiades 2020-12 Java Full Edition
 追加プラグイン:Spring Tools 3 Add-On for Spring Tools 4.3.9.16.RELEASE
簡単な
アプリケーション
の作成
 作成するアプリケーション
 Feed(Atom)を読み込み、指定したディレクトリに書き出していく
 5秒間隔にアクセスして更新する
 Feedの内容は、titleとlinkを抜き出して保存する
 内容は以下のURLそのまま(素晴らしい例題です)
https://spring.pleiades.io/projects/spring-integration
Atomとは、ウェブサイトの更新情報の配信等に利用されている技術
RFC4297 - The Atom Syndication Format(https://tools.ietf.org/html/rfc4287)
RFC5023 – The Atom Publishing Protocol (https://tools.ietf.org/html/rfc5023)
ニュース
サイト
Feed情報
Spring Boot
Application
With
integration
Feed情報
出力ファイル
title@link
5秒間隔
手順は3つのみ
1. プロジェクトの作成
2. お絵描きと設定
3. 数行の実装
 [ファイル] → [新規] → [その他] → [Spring Boot] → [Spring スターター・プロジェクト]
 Spring スターター・プロジェクト
 名前:integration-demo
 グループ:com.example.integ
 成果物:integration-demo
 パッケージ:com.example.integ.demo
 Spring スターター・プロジェクト依存関係
 Spring Boot バージョン:2.4.5
 Spring Integration を選択
プロジェクトの作成
動作確認済み Spring Boot バージョン
2.4.5, 2.3.10.RELEASE, 2.2.13.RELEASE, 2.2.4.RELEASE
お絵描きと設定 1/4
 pom.xmlの依存関係にfeedとfileを設定
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
お絵描きと設定 2/4
 設定ファイルの作成
 [ファイル] → [新規] → [その他] → [Spring] → [Spring Bean 構成ファイル]
 src/main/resources/integration.xml を作成
 Integration.xml ファイルを開く
 [名前空間] を3つ選択して保存
 Int
 Int-feed
 Int-file
お絵描きと設定 3/4
 [integration-graph] で絵を描く(integration.xmlファイルを開く)1/2
 コンポーネントを配置
 feed から “inbound-channel-adapter”
 ファイル から “outbound-channel-adapter”
 変換 から “transformer”
 コンポーネント間をつなぐ
 “接続” を選択してコンポーネントを繋げる(channelは自動的に配置される)
feed transformer file
 [integration-graph] で絵を描く(integration.xmlファイルを開く)2/2
 コンポーネント毎の設定(コンポーネントをダブルクリックして、プロパティウィンドウを開く)
1. feed - inbound-channel-adapter
 id:news
 url:https://www.j-cast.com/atom.xml
2. ファイル – outbound-channel-adapter
 id:file
 charset:UTF-8
 directory:C:temp
 filename-generator-expression:’jcast-news’
 mode:APPEND
3. 変換 – transformer
 expression:payload.title + ‘@’ + payload.link + ‘#{systemProperties[‘line.separator’]}’
お絵描きと設定 4/4
1.feed 2.ファイル
3.変換
数行の実装
 Integration.xml に poller 設定追加
 IntegrationDemoApplication.java の書き換え
<int-feed:inbound-channel-adapter
id="news" channel="channel1" url="https://www.j-cast.com/atom.xml">
<int:poller fixed-rate="5000" />
</int-feed:inbound-channel-adapter>
@SpringBootApplication
@ImportResource("/integration.xml")
public class IntegrationDemoApplication {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context =
new SpringApplication(IntegrationDemoApplication.class).run(args);
System.out.println(“Hit enter to terminate.”); // main を終了しないようにする
System.in.read();
context.close();
}
}
完成したので実行する
 実行
 IntegrationDemoApplication.java を選択して右クリック
 [実行] → [Spring Boot アプリケーション]
 “C:tempjcast-news” ファイルが出力され、5秒間隔で Feed 情報の取得が繰り返される
※コンソールウィンドウ内でリターンをするとアプリケーションが終了する
XML定義は、もう少し簡素に書ける
 Channelを経由しなくてもシンプルに書けるが、integration-graphでは書けない
<int-feed:inbound-channel-adapter
id="news" channel="channel1" url="https://www.j-cast.com/atom.xml">
<int:poller fixed-rate="5000" />
</int-feed:inbound-channel-adapter>
<int-file:outbound-channel-adapter channel="channel2" id="file" charset="UTF-8“
directory="C:temp" mode="APPEND“ filename-generator-expression="'jcast-news'">
</int-file:outbound-channel-adapter>
<int:transformer input-channel="channel1"
output-channel="channel2"
expression="payload.title + '@' + payload.link + '#{systemProperties['line.separator']}'">
</int:transformer>
<int:channel id="channel1"></int:channel>
<int:channel id="channel2"></int:channel>
<int-feed:inbound-channel-adapter id="news" url="https://www.j-cast.com/atom.xml">
<int:poller fixed-rate="5000" />
</int-feed:inbound-channel-adapter>
<int:transformer input-channel="news"
expression="payload.title + '@' + payload.link + '#{systemProperties['line.separator']}'"
output-channel="file" />
<int-file:outbound-channel-adapter id="file" mode="APPEND" charset="UTF-8"
directory="C:temp" filename-generator-expression="'jcast-news'" />
XMLタグ数:6
XMLタグ数:4
Spring Integration の Java DSL を使って
みよう
 先ほどと同じアプリケーションを、作成する
手順は3つのみ
 プロジェクトの作成
 pom.xmlの設定
 Configuration, Application の実装
 [ファイル] → [新規] → [その他] → [Spring Boot] → [Spring スターター・プロジェクト]
 Spring スターター・プロジェクト
 名前:integration-demo2
 グループ:com.example.integ
 成果物:integration-demo2
 パッケージ:com.example.integ.demo2
 Spring スターター・プロジェクト依存関係
 Spring Boot バージョン:2.4.5
 Spring Integration を選択
プロジェクトの作成
pom.xml の設定
 pom.xmlの依存関係にfeedとfileを設定
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-feed</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
Configuration, Application の実装
 IntegrationDemo2Application.java の書き換え
@SpringBootApplication
public class IntegrationDemo2Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context =
new SpringApplication(IntegrationDemo2Application.class).run(args);
System.out.println(“Hit enter to terminate.”); // main を終了しないようにする
System.in.read();
context.close();
}
}
<point>
XMLとは異なり、”@ImportResource” が必要ない
Configuration, Application の実装
Part.1
 IntegrationDemo2Configuration.java の作成
@Configuration
@EnableIntegration
public class IntegrationDemo2Configuration {
public MessageSource<?> feedMessageSource() throws Exception {
URL url = new URL("https://www.j-cast.com/atom.xml");
String metaKey = "news";
FeedEntryMessageSource source = new FeedEntryMessageSource(url, metaKey);
return source;
}
public GenericTransformer<?, ?> spelTransformer() {
String exp = "payload.title + '@' + payload.link + '" + System.lineSeparator() + "'";
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(exp);
return new ExpressionEvaluatingTransformer(expression);
}
@Bean
public IntegrationFlow demo2Flow() throws Exception {
return IntegrationFlows.from(feedMessageSource(), c -> c.poller(Pollers.fixedRate(5_000)))
.transform(spelTransformer())
.handle(Files.outboundAdapter(new File("C:temp")).fileNameExpression("'jcast-news2'")
.charset(StandardCharsets.UTF_8.name()).fileExistsMode(FileExistsMode.APPEND))
.get();
}
}
<point>
• IntegrationFlows#from から始まる
• filter, handle, route, split, transform など Javadoc を良く読む
• 利用している Spring ライブラリの dsl パッケージのソースを
ちゃんと見ないと作れない
Ex. org.springframework.integration.file.dsl.Files
分かりやすく XML 定義になるべく
忠実に再現したソース
完成したので実行する
 実行
 IntegrationDemo2Application.java を選択して右クリック
 [実行] → [Spring Boot アプリケーション]
 “C:tempjcast-news2” ファイルが出力され、5秒間隔で Feed 情報の取得が繰り返される
※コンソールウィンドウ内でリターンをするとアプリケーションが終了する
Configuration, Application の実装
Part.2
 IntegrationDemo2Configuration.java の変更
@Configuration
@EnableIntegration
public class IntegrationDemo2Configuration {
@Bean
public IntegrationFlow demo3Flow() throws Exception {
return IntegrationFlows
.from(new FeedEntryMessageSource(new URL("https://www.j-cast.com/atom.xml"), "news"),
c -> c.poller(Pollers.fixedRate(5_000)))
.<SyndEntry, String>transform(m-> new String(m.getTitle() + "@" + m.getLink() + System.lineSeparator()))
.handle(Files.outboundAdapter(new File("C:temp")).fileNameExpression("'jcast-news2'")
.charset(StandardCharsets.UTF_8.name()).fileExistsMode(FileExistsMode.APPEND))
.get();
}
}
<point>
• 簡単な処理は”より簡単に!”、複雑な処理は”より難しく…”
• 標準出力に出しながら動作確認していくために以下を使うと便利
“.handle(System.out::print)”
• 記述方法に慣れるには以下をよく読む
https://spring.pleiades.io/spring-integration/reference/html/dsl.html
Do it simply !!
小ネタ
 Eclipse で pom.xml がエラーになったら、Eclipse を再起動しよう
 今までエラーが無かった pom.xml がエラーと言われることがある。
 典型的なエラーは、”The element type "groupId" must be terminated by the
matching end-tag "</groupId>”
 SpringBootApplication の DEBUG ログを出力する
 /src/main/resources/application.properties
 logging.level.root=DEBUG
 /src/main/resources/application.yml
 logging:
level:
root: DEBUG
小ネタ
 SpringBootApplication のバナーを消す
 /src/main/resources/application.properties
 spring.main.banner-mode=off
 /src/main/resources/application.yml
 spring:
main:
banner-mode: “off”
 Eclipse から SpringBootApplication を起動すると DEBUG ログに
javax.management.InstanceNotFoundException が発生している原因
 Eclipse の実行構成で”JMXを使用可能にする”にチェックがついているため、リモー
トでJMXを確認する必要が無ければチェックを外す。チェックを外しても、ローカル
からJXM確認することは可能

Contenu connexe

Tendances

Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)
Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)
Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)NTT DATA Technology & Innovation
 
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗く
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗くSpring Initializrをハックする-カスタマイズを通してその内部実装を覗く
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗くapkiban
 
Azure API Management 俺的マニュアル
Azure API Management 俺的マニュアルAzure API Management 俺的マニュアル
Azure API Management 俺的マニュアル貴志 上坂
 
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢apkiban
 
大規模・長期保守を見据えたエンタープライズ システム開発へのSpring Frameworkの適用
大規模・長期保守を見据えたエンタープライズシステム開発へのSpring Frameworkの適用大規模・長期保守を見据えたエンタープライズシステム開発へのSpring Frameworkの適用
大規模・長期保守を見据えたエンタープライズ システム開発へのSpring Frameworkの適用apkiban
 
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)Trainocate Japan, Ltd.
 
開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)mosa siru
 
What's new in Spring Boot 2.6 ?
What's new in Spring Boot 2.6 ?What's new in Spring Boot 2.6 ?
What's new in Spring Boot 2.6 ?土岐 孝平
 
SQL大量発行処理をいかにして高速化するか
SQL大量発行処理をいかにして高速化するかSQL大量発行処理をいかにして高速化するか
SQL大量発行処理をいかにして高速化するかShogo Wakayama
 
Spring Boot × Vue.jsでSPAを作る
Spring Boot × Vue.jsでSPAを作るSpring Boot × Vue.jsでSPAを作る
Spring Boot × Vue.jsでSPAを作るGo Miyasaka
 
怖くないSpring Bootのオートコンフィグレーション
怖くないSpring Bootのオートコンフィグレーション怖くないSpring Bootのオートコンフィグレーション
怖くないSpring Bootのオートコンフィグレーション土岐 孝平
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -onozaty
 
今こそ知りたいSpring Web(Spring Fest 2020講演資料)
今こそ知りたいSpring Web(Spring Fest 2020講演資料)今こそ知りたいSpring Web(Spring Fest 2020講演資料)
今こそ知りたいSpring Web(Spring Fest 2020講演資料)NTT DATA Technology & Innovation
 
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティスAmazon Web Services Japan
 
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とは
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とはがんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とは
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とはJun-ichi Sakamoto
 
オープンソースのAPIゲートウェイ Kong ご紹介
オープンソースのAPIゲートウェイ Kong ご紹介 オープンソースのAPIゲートウェイ Kong ご紹介
オープンソースのAPIゲートウェイ Kong ご紹介 briscola-tokyo
 
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)NTT DATA Technology & Innovation
 
さくっと理解するSpring bootの仕組み
さくっと理解するSpring bootの仕組みさくっと理解するSpring bootの仕組み
さくっと理解するSpring bootの仕組みTakeshi Ogawa
 
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...NTT DATA Technology & Innovation
 
Java EE から Quarkus による開発への移行について
Java EE から Quarkus による開発への移行についてJava EE から Quarkus による開発への移行について
Java EE から Quarkus による開発への移行についてShigeru Tatsuta
 

Tendances (20)

Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)
Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)
Kubernetes 疲れに Azure Container Apps はいかがでしょうか?(江東区合同ライトニングトーク 発表資料)
 
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗く
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗くSpring Initializrをハックする-カスタマイズを通してその内部実装を覗く
Spring Initializrをハックする-カスタマイズを通してその内部実装を覗く
 
Azure API Management 俺的マニュアル
Azure API Management 俺的マニュアルAzure API Management 俺的マニュアル
Azure API Management 俺的マニュアル
 
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢
GraalVM の概要と、Native Image 化によるSpring Boot 爆速化の夢
 
大規模・長期保守を見据えたエンタープライズ システム開発へのSpring Frameworkの適用
大規模・長期保守を見据えたエンタープライズシステム開発へのSpring Frameworkの適用大規模・長期保守を見据えたエンタープライズシステム開発へのSpring Frameworkの適用
大規模・長期保守を見据えたエンタープライズ システム開発へのSpring Frameworkの適用
 
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)
AWSとオンプレミスを繋ぐときに知っておきたいルーティングの基礎知識(CCSI監修!)
 
開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)開発速度が速い #とは(LayerX社内資料)
開発速度が速い #とは(LayerX社内資料)
 
What's new in Spring Boot 2.6 ?
What's new in Spring Boot 2.6 ?What's new in Spring Boot 2.6 ?
What's new in Spring Boot 2.6 ?
 
SQL大量発行処理をいかにして高速化するか
SQL大量発行処理をいかにして高速化するかSQL大量発行処理をいかにして高速化するか
SQL大量発行処理をいかにして高速化するか
 
Spring Boot × Vue.jsでSPAを作る
Spring Boot × Vue.jsでSPAを作るSpring Boot × Vue.jsでSPAを作る
Spring Boot × Vue.jsでSPAを作る
 
怖くないSpring Bootのオートコンフィグレーション
怖くないSpring Bootのオートコンフィグレーション怖くないSpring Bootのオートコンフィグレーション
怖くないSpring Bootのオートコンフィグレーション
 
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
今からでも遅くないDBマイグレーション - Flyway と SchemaSpy の紹介 -
 
今こそ知りたいSpring Web(Spring Fest 2020講演資料)
今こそ知りたいSpring Web(Spring Fest 2020講演資料)今こそ知りたいSpring Web(Spring Fest 2020講演資料)
今こそ知りたいSpring Web(Spring Fest 2020講演資料)
 
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
[Aurora事例祭り]Amazon Aurora を使いこなすためのベストプラクティス
 
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とは
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とはがんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とは
がんばらなくても C# で Single Page Web アプリケーションが書けてしまう「Blazor」とは
 
オープンソースのAPIゲートウェイ Kong ご紹介
オープンソースのAPIゲートウェイ Kong ご紹介 オープンソースのAPIゲートウェイ Kong ご紹介
オープンソースのAPIゲートウェイ Kong ご紹介
 
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
今こそ知りたいSpring Batch(Spring Fest 2020講演資料)
 
さくっと理解するSpring bootの仕組み
さくっと理解するSpring bootの仕組みさくっと理解するSpring bootの仕組み
さくっと理解するSpring bootの仕組み
 
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...
NTTデータ流Infrastructure as Code~ 大規模プロジェクトを通して考え抜いた基盤自動化の新たな姿~(NTTデータ テクノロジーカンフ...
 
Java EE から Quarkus による開発への移行について
Java EE から Quarkus による開発への移行についてJava EE から Quarkus による開発への移行について
Java EE から Quarkus による開発への移行について
 

Similaire à Spring Integration 超入門

Visual StudioやAzureからAzure DevOpsを使う
Visual StudioやAzureからAzure DevOpsを使うVisual StudioやAzureからAzure DevOpsを使う
Visual StudioやAzureからAzure DevOpsを使うTakeshi Fukuhara
 
Entity Framework 6.1.3 + Windows フォーム サンプル アプリケーション構築 手順書
Entity Framework 6.1.3 + Windows フォームサンプル アプリケーション構築手順書Entity Framework 6.1.3 + Windows フォームサンプル アプリケーション構築手順書
Entity Framework 6.1.3 + Windows フォーム サンプル アプリケーション構築 手順書Masaki Takeda
 
Logic Apps/Flow Update Summary
Logic Apps/Flow Update SummaryLogic Apps/Flow Update Summary
Logic Apps/Flow Update SummaryTomoyuki Obi
 
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)NTT DATA Technology & Innovation
 
SpringBootの研修本で学んだこと
SpringBootの研修本で学んだことSpringBootの研修本で学んだこと
SpringBootの研修本で学んだことiPride Co., Ltd.
 
TestFlight自動化でらくらくチームテスト
TestFlight自動化でらくらくチームテストTestFlight自動化でらくらくチームテスト
TestFlight自動化でらくらくチームテストYoichiro Sakurai
 
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用de:code 2017
 
Android勉強会 1
Android勉強会 1Android勉強会 1
Android勉強会 1shotaueda3
 
.NET の過去、現在、そして未来
.NET の過去、現在、そして未来.NET の過去、現在、そして未来
.NET の過去、現在、そして未来Akira Inoue
 
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)Takeshi Fukuhara
 
Interactive connection2
Interactive connection2Interactive connection2
Interactive connection2Takao Tetsuro
 
Apiドキュメンテーションツールを使いこなす【api blueprint編】
Apiドキュメンテーションツールを使いこなす【api blueprint編】Apiドキュメンテーションツールを使いこなす【api blueprint編】
Apiドキュメンテーションツールを使いこなす【api blueprint編】dcubeio
 
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成Hiroshi Yamaguchi
 
Gitlab ci & ecsへのデプロイ
Gitlab ci & ecsへのデプロイGitlab ci & ecsへのデプロイ
Gitlab ci & ecsへのデプロイiwata jaws-ug
 
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみよう
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみようNTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみよう
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみようMidori Oge
 
Google App Engine Java 入門
Google App Engine Java 入門Google App Engine Java 入門
Google App Engine Java 入門tantack
 
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...Shotaro Suzuki
 
Logic Apps と Api Apps の話
Logic Apps と Api Apps の話Logic Apps と Api Apps の話
Logic Apps と Api Apps の話Sunao Tomita
 
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)wind06106
 

Similaire à Spring Integration 超入門 (20)

Visual StudioやAzureからAzure DevOpsを使う
Visual StudioやAzureからAzure DevOpsを使うVisual StudioやAzureからAzure DevOpsを使う
Visual StudioやAzureからAzure DevOpsを使う
 
Windows Azure PHP Tips
Windows Azure PHP Tips Windows Azure PHP Tips
Windows Azure PHP Tips
 
Entity Framework 6.1.3 + Windows フォーム サンプル アプリケーション構築 手順書
Entity Framework 6.1.3 + Windows フォームサンプル アプリケーション構築手順書Entity Framework 6.1.3 + Windows フォームサンプル アプリケーション構築手順書
Entity Framework 6.1.3 + Windows フォーム サンプル アプリケーション構築 手順書
 
Logic Apps/Flow Update Summary
Logic Apps/Flow Update SummaryLogic Apps/Flow Update Summary
Logic Apps/Flow Update Summary
 
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
OpenLineage による Airflow のデータ来歴の収集と可視化(Airflow Meetup Tokyo #3 発表資料)
 
SpringBootの研修本で学んだこと
SpringBootの研修本で学んだことSpringBootの研修本で学んだこと
SpringBootの研修本で学んだこと
 
TestFlight自動化でらくらくチームテスト
TestFlight自動化でらくらくチームテストTestFlight自動化でらくらくチームテスト
TestFlight自動化でらくらくチームテスト
 
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用
[TL04] .NET 15 周年の今こそ考えるクラウドネイティブ アプリケーションと .NET の活用
 
Android勉強会 1
Android勉強会 1Android勉強会 1
Android勉強会 1
 
.NET の過去、現在、そして未来
.NET の過去、現在、そして未来.NET の過去、現在、そして未来
.NET の過去、現在、そして未来
 
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)
Part 3: サーバーレスとシステム間連携基盤 (製造リファレンス・アーキテクチャ勉強会)
 
Interactive connection2
Interactive connection2Interactive connection2
Interactive connection2
 
Apiドキュメンテーションツールを使いこなす【api blueprint編】
Apiドキュメンテーションツールを使いこなす【api blueprint編】Apiドキュメンテーションツールを使いこなす【api blueprint編】
Apiドキュメンテーションツールを使いこなす【api blueprint編】
 
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成
VisualSFMとMeshLabとCloudCompareによるドローン撮影画像を用いたデジタル地図作成
 
Gitlab ci & ecsへのデプロイ
Gitlab ci & ecsへのデプロイGitlab ci & ecsへのデプロイ
Gitlab ci & ecsへのデプロイ
 
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみよう
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみようNTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみよう
NTTコミュニケーションズ Cloudn勉強会資料 SDKでAPIをたたいてみよう
 
Google App Engine Java 入門
Google App Engine Java 入門Google App Engine Java 入門
Google App Engine Java 入門
 
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...
Developing .NET 6 Blazor WebAssemby apps with Radzen Blazor component library...
 
Logic Apps と Api Apps の話
Logic Apps と Api Apps の話Logic Apps と Api Apps の話
Logic Apps と Api Apps の話
 
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)
Cloud OS Tech Day 2014:Windows Azure Pack プライベートクラウドとセルフポータル(仮)
 

Spring Integration 超入門

  • 2. はじめに なぜ、このスライドを作ったか  「なんだか難しそうだ」という根拠のない感覚を打ち消すため  Spring Integration を導入する場合に「学習コストが~」と導入を拒否される現実を 緩和するため 対象読者  Spring Integration を全く知らない人  Spring Integration に興味はあっても、調査が後回しになっている人 注意  間違いや認識の違いがありましたら、指摘頂ければ幸いです
  • 3. Agenda  Spring Integration とは  簡単なアプリケーションの作成  STS3 の integration-graph を利用  Spring Integration の Java DSL を利用
  • 4. Main Projects Description Spring Boot Takes an opinionated view of building Spring applications and gets you up and running as quickly as possible. Spring Framework Provides core support for dependency injection, transaction management, web apps, data access, messaging, and more. Spring Cloud Provides a set of tools for common patterns in distributed systems. Useful for building and deploying microservices. Spring Cloud Data Flow Provides an orchestration service for composable data microservice applications on modern runtimes. Spring Data Provides a consistent approach to data access – relational, non-relational, map-reduce, and beyond. Spring Integration Supports the well-known Enterprise Integration Patterns through lightweight messaging and declarative adapters. Spring Batch Simplifies and optimizes the work of processing high-volume batch operations. Spring Security Protects your application with comprehensive and extensible authentication and authorization support. Spring Projects https://spring.io/projects
  • 5. Spring Integration https://spring.io/projects/spring-integration  Enterprise Integration Pattern の実装  Message  Endpoint  Channel  Aggregator  Filter  Transformer  REST/HTTP, FTP/SFTP, TCP/UDP, JMS, Email etc. Spring Integration : https://spring.io/projects/spring-integration Enterprise Integration Pattern : https://www.enterpriseintegrationpatterns.com/
  • 6. どういうときに使うの?  同期・非同期メッセージング、リソース間を接続したいとき  サポートされている Endpoint が多いので作らなくて良い  とはいえ、要件に適合しているかは要チェック  Pipe and Filters Pattern によりソフトウェアの構造をシンプルにしたいとき  接続プロトコルの変更などに対応しやすい  処理を変更・追加しやすい  業務特化の Adapter を部品化して再利用しやすい Integration Endpoints : https://docs.spring.io/spring-integration/docs/current/reference/html/index.html Pattern-Oriented Software Architecture : https://en.wikipedia.org/wiki/Pattern-Oriented_Software_Architecture
  • 7. 前提環境  Windows  Pleiades 2020-12 Java Full Edition  追加プラグイン:Spring Tools 3 Add-On for Spring Tools 4.3.9.16.RELEASE
  • 8. 簡単な アプリケーション の作成  作成するアプリケーション  Feed(Atom)を読み込み、指定したディレクトリに書き出していく  5秒間隔にアクセスして更新する  Feedの内容は、titleとlinkを抜き出して保存する  内容は以下のURLそのまま(素晴らしい例題です) https://spring.pleiades.io/projects/spring-integration Atomとは、ウェブサイトの更新情報の配信等に利用されている技術 RFC4297 - The Atom Syndication Format(https://tools.ietf.org/html/rfc4287) RFC5023 – The Atom Publishing Protocol (https://tools.ietf.org/html/rfc5023) ニュース サイト Feed情報 Spring Boot Application With integration Feed情報 出力ファイル title@link 5秒間隔
  • 10.  [ファイル] → [新規] → [その他] → [Spring Boot] → [Spring スターター・プロジェクト]  Spring スターター・プロジェクト  名前:integration-demo  グループ:com.example.integ  成果物:integration-demo  パッケージ:com.example.integ.demo  Spring スターター・プロジェクト依存関係  Spring Boot バージョン:2.4.5  Spring Integration を選択 プロジェクトの作成 動作確認済み Spring Boot バージョン 2.4.5, 2.3.10.RELEASE, 2.2.13.RELEASE, 2.2.4.RELEASE
  • 12. お絵描きと設定 2/4  設定ファイルの作成  [ファイル] → [新規] → [その他] → [Spring] → [Spring Bean 構成ファイル]  src/main/resources/integration.xml を作成  Integration.xml ファイルを開く  [名前空間] を3つ選択して保存  Int  Int-feed  Int-file
  • 13. お絵描きと設定 3/4  [integration-graph] で絵を描く(integration.xmlファイルを開く)1/2  コンポーネントを配置  feed から “inbound-channel-adapter”  ファイル から “outbound-channel-adapter”  変換 から “transformer”  コンポーネント間をつなぐ  “接続” を選択してコンポーネントを繋げる(channelは自動的に配置される) feed transformer file
  • 14.  [integration-graph] で絵を描く(integration.xmlファイルを開く)2/2  コンポーネント毎の設定(コンポーネントをダブルクリックして、プロパティウィンドウを開く) 1. feed - inbound-channel-adapter  id:news  url:https://www.j-cast.com/atom.xml 2. ファイル – outbound-channel-adapter  id:file  charset:UTF-8  directory:C:temp  filename-generator-expression:’jcast-news’  mode:APPEND 3. 変換 – transformer  expression:payload.title + ‘@’ + payload.link + ‘#{systemProperties[‘line.separator’]}’ お絵描きと設定 4/4 1.feed 2.ファイル 3.変換
  • 15. 数行の実装  Integration.xml に poller 設定追加  IntegrationDemoApplication.java の書き換え <int-feed:inbound-channel-adapter id="news" channel="channel1" url="https://www.j-cast.com/atom.xml"> <int:poller fixed-rate="5000" /> </int-feed:inbound-channel-adapter> @SpringBootApplication @ImportResource("/integration.xml") public class IntegrationDemoApplication { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = new SpringApplication(IntegrationDemoApplication.class).run(args); System.out.println(“Hit enter to terminate.”); // main を終了しないようにする System.in.read(); context.close(); } }
  • 16. 完成したので実行する  実行  IntegrationDemoApplication.java を選択して右クリック  [実行] → [Spring Boot アプリケーション]  “C:tempjcast-news” ファイルが出力され、5秒間隔で Feed 情報の取得が繰り返される ※コンソールウィンドウ内でリターンをするとアプリケーションが終了する
  • 17. XML定義は、もう少し簡素に書ける  Channelを経由しなくてもシンプルに書けるが、integration-graphでは書けない <int-feed:inbound-channel-adapter id="news" channel="channel1" url="https://www.j-cast.com/atom.xml"> <int:poller fixed-rate="5000" /> </int-feed:inbound-channel-adapter> <int-file:outbound-channel-adapter channel="channel2" id="file" charset="UTF-8“ directory="C:temp" mode="APPEND“ filename-generator-expression="'jcast-news'"> </int-file:outbound-channel-adapter> <int:transformer input-channel="channel1" output-channel="channel2" expression="payload.title + '@' + payload.link + '#{systemProperties['line.separator']}'"> </int:transformer> <int:channel id="channel1"></int:channel> <int:channel id="channel2"></int:channel> <int-feed:inbound-channel-adapter id="news" url="https://www.j-cast.com/atom.xml"> <int:poller fixed-rate="5000" /> </int-feed:inbound-channel-adapter> <int:transformer input-channel="news" expression="payload.title + '@' + payload.link + '#{systemProperties['line.separator']}'" output-channel="file" /> <int-file:outbound-channel-adapter id="file" mode="APPEND" charset="UTF-8" directory="C:temp" filename-generator-expression="'jcast-news'" /> XMLタグ数:6 XMLタグ数:4
  • 18. Spring Integration の Java DSL を使って みよう  先ほどと同じアプリケーションを、作成する
  • 20.  [ファイル] → [新規] → [その他] → [Spring Boot] → [Spring スターター・プロジェクト]  Spring スターター・プロジェクト  名前:integration-demo2  グループ:com.example.integ  成果物:integration-demo2  パッケージ:com.example.integ.demo2  Spring スターター・プロジェクト依存関係  Spring Boot バージョン:2.4.5  Spring Integration を選択 プロジェクトの作成
  • 22. Configuration, Application の実装  IntegrationDemo2Application.java の書き換え @SpringBootApplication public class IntegrationDemo2Application { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = new SpringApplication(IntegrationDemo2Application.class).run(args); System.out.println(“Hit enter to terminate.”); // main を終了しないようにする System.in.read(); context.close(); } } <point> XMLとは異なり、”@ImportResource” が必要ない
  • 23. Configuration, Application の実装 Part.1  IntegrationDemo2Configuration.java の作成 @Configuration @EnableIntegration public class IntegrationDemo2Configuration { public MessageSource<?> feedMessageSource() throws Exception { URL url = new URL("https://www.j-cast.com/atom.xml"); String metaKey = "news"; FeedEntryMessageSource source = new FeedEntryMessageSource(url, metaKey); return source; } public GenericTransformer<?, ?> spelTransformer() { String exp = "payload.title + '@' + payload.link + '" + System.lineSeparator() + "'"; ExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression(exp); return new ExpressionEvaluatingTransformer(expression); } @Bean public IntegrationFlow demo2Flow() throws Exception { return IntegrationFlows.from(feedMessageSource(), c -> c.poller(Pollers.fixedRate(5_000))) .transform(spelTransformer()) .handle(Files.outboundAdapter(new File("C:temp")).fileNameExpression("'jcast-news2'") .charset(StandardCharsets.UTF_8.name()).fileExistsMode(FileExistsMode.APPEND)) .get(); } } <point> • IntegrationFlows#from から始まる • filter, handle, route, split, transform など Javadoc を良く読む • 利用している Spring ライブラリの dsl パッケージのソースを ちゃんと見ないと作れない Ex. org.springframework.integration.file.dsl.Files 分かりやすく XML 定義になるべく 忠実に再現したソース
  • 24. 完成したので実行する  実行  IntegrationDemo2Application.java を選択して右クリック  [実行] → [Spring Boot アプリケーション]  “C:tempjcast-news2” ファイルが出力され、5秒間隔で Feed 情報の取得が繰り返される ※コンソールウィンドウ内でリターンをするとアプリケーションが終了する
  • 25. Configuration, Application の実装 Part.2  IntegrationDemo2Configuration.java の変更 @Configuration @EnableIntegration public class IntegrationDemo2Configuration { @Bean public IntegrationFlow demo3Flow() throws Exception { return IntegrationFlows .from(new FeedEntryMessageSource(new URL("https://www.j-cast.com/atom.xml"), "news"), c -> c.poller(Pollers.fixedRate(5_000))) .<SyndEntry, String>transform(m-> new String(m.getTitle() + "@" + m.getLink() + System.lineSeparator())) .handle(Files.outboundAdapter(new File("C:temp")).fileNameExpression("'jcast-news2'") .charset(StandardCharsets.UTF_8.name()).fileExistsMode(FileExistsMode.APPEND)) .get(); } } <point> • 簡単な処理は”より簡単に!”、複雑な処理は”より難しく…” • 標準出力に出しながら動作確認していくために以下を使うと便利 “.handle(System.out::print)” • 記述方法に慣れるには以下をよく読む https://spring.pleiades.io/spring-integration/reference/html/dsl.html Do it simply !!
  • 26. 小ネタ  Eclipse で pom.xml がエラーになったら、Eclipse を再起動しよう  今までエラーが無かった pom.xml がエラーと言われることがある。  典型的なエラーは、”The element type "groupId" must be terminated by the matching end-tag "</groupId>”  SpringBootApplication の DEBUG ログを出力する  /src/main/resources/application.properties  logging.level.root=DEBUG  /src/main/resources/application.yml  logging: level: root: DEBUG
  • 27. 小ネタ  SpringBootApplication のバナーを消す  /src/main/resources/application.properties  spring.main.banner-mode=off  /src/main/resources/application.yml  spring: main: banner-mode: “off”  Eclipse から SpringBootApplication を起動すると DEBUG ログに javax.management.InstanceNotFoundException が発生している原因  Eclipse の実行構成で”JMXを使用可能にする”にチェックがついているため、リモー トでJMXを確認する必要が無ければチェックを外す。チェックを外しても、ローカル からJXM確認することは可能