3. l コンテナとは、Spring-Boot, CDI, OSGiなどを指す
l コンテナ⾃⾝の内部サービスの初期化の後、CamelContextを初期化す
る
l コンテナによってCamelContext⽣成する⽅法が異なる
l Spring-boot XMLの場合、<camelContext xmlns="…"/>
起動フェーズ1: コンテナがCamelContextのインスタ
ンスを⽣成
20203
9. 1. CamelContext
l startRoute()
l startAllRoutes()
l stopRoute()
2. ControlBus EIP
l EIPである
l Route内で実⾏可能
3. RoutePolicy
l RoutePolicySupportを拡張
l CamelからCallback
4. JMX
l リモートから制御可能
l ManagedRoute Mean
l Start()
l Stop()
l 詳細は16章で紹介
Routeの起動・停⽌をプログラムで制御する⽅法
20209
JVM
Runtime
(Karaf/SpringBoot/WildFly/Tomcat)
CamelContext
Route1
Route2
JMX
RoutePolicy
ControlBus EIP
CamelContext
10. 例:「onCompletion + CamelContext.stop()」を使って、1つファイル
を処理後に、Route(⾃⾝)を停⽌する
Routeの起動・停⽌をプログラムで制御する⽅法
⽅法1: CamelContext
202010
camelinaction2/chapter15/startup/src/test/resources/META-INF/spring/SpringManualRouteWithOnCompletionTest.xml
<route id="manual" autoStartup="false">
<from uri="file://target/inventory/manual?maxMessagesPerPoll=1"/>
<!-- on completion which will stop the route using the processor -->
<onCompletion>
<process ref="stopRouteProcessor"/>
</onCompletion>
<log message="Doing manual update with file ${file:name}"/>
<split>
<!-- split the body using a tokenizer -->
<tokenize token="n"/>
<convertBodyTo type="camelinaction.inventory.UpdateInventoryInput"/>
<to uri="direct:update"/>
</split>
</route>
Routeの⾃動起動をオフにする
ファイル1回のみ処理にする
Route処理完了時に、Processorを実⾏して、Route
を停⽌する
※ Processor内でCamelContext.stopRoute()
11. 例: CamelContext.stopRoute()を使うProcessorの実装
Routeの起動・停⽌をプログラムで制御する⽅法
⽅法1: CamelContext (続き)
202011
camelinaction2/chapter15/startup/src/test/java/camelinaction/StopRouteProcessor.java
/**
* @param name route to stop
*/
public StopRouteProcessor(String name) {
this.name = name;
}
public void process(Exchange exchange) throws Exception {
// force stopping this route while we are routing an Exchange
// requires two steps:
// 1) unregister from the inflight registry
// 2) stop the route
LOG.info("Stopping route: " + name);
exchange.getContext().getInflightRepository().remove(exchange, name);
exchange.getContext().stopRoute(name);
}
InflightRepositoryからExchangeを削除
して置かないと、現在ExchangeがInflight
中のWARNがログに出る
Processor初期化時に、停⽌するRoute名をメン
バー変数にセットするため
指定したRouteを停⽌する
12. 例:「ControlBus EIP」 を使って、1つファイルを処理後に、Route(⾃
⾝)を停⽌する
Routeの起動・停⽌をプログラムで制御する⽅法
⽅法2: ControlBus EIP
202012
camelinaction2/chapter15/controlbus/src/test/java/camelinaction/ControlBusRoute.java
// ensure we only pickup one file at any given time
from("file://target/inventory/manual?maxMessagesPerPoll=1")
// use noAutoStartup to indicate this route should
// NOT be started when Camel starts
.routeId("manual").noAutoStartup()
.log("Doing manual update with file ${file:name}")
.split(body().tokenize("n"))
.convertBodyTo(UpdateInventoryInput.class)
.to("direct:update")
.end()
// use end() to denote the end of the splitter sub-route
.to("controlbus:route?routeId=manual&action=stop&async=true");
}
Routeの⾃動起動をオフにする
ファイル1回のみ処理にする
Route処理完了時に、ControlBus EIPを実⾏して、
Routeを停⽌する
14. 例:「RoutePolicy」を使って、Route[foo]とRoute[bar]を交互に起動する
(foo -> bar -> foo -> bar -> …)
Routeの起動・停⽌をプログラムで制御する⽅法
⽅法3: RoutePolicy(続き)
202014
camelinaction2/chapter15/routepolicy/src/test/resources/camel-fliproute.xml
<bean id="flipPolicy" class="camelinaction.FlipRoutePolicy">
<constructor-arg index="0" value="foo"/>
<constructor-arg index="1" value="bar"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<!-- the foo route uses the flipPolicy -->
<route id="foo" routePolicyRef="flipPolicy">
<from uri="timer:foo?delay=500"/>
<setBody><constant>Foo message</constant></setBody>
<to uri="log:foo"/>
<to uri="mock:foo"/>
</route>
<!-- the bar route uses the flipPolicy and is NOT started on startup -->
<route id="bar" routePolicyRef="flipPolicy" autoStartup="false">
<from uri="timer:bar?delay=500"/>
<setBody><constant>Bar message</constant></setBody>
<to uri="log:bar"/>
<to uri="mock:bar"/>
</route>
</camelContext>
カスタムRoutePolicyを適⽤する
カスタムRoutePolicyを
初期化する
Routeの⾃動起動をオフにして、
カスタムRoutePolicyを適⽤する
15. 例: カスタムRoutePolicyの実装例
Routeの起動・停⽌をプログラムで制御する⽅法
⽅法3: RoutePolicy (続き)
202015
camelinaction2/chapter15/routepolicy/src/test/java/camelinaction/FlipRoutePolicy.java
public class FlipRoutePolicy extends RoutePolicySupport {
public FlipRoutePolicy(String name1, String name2) {
this.name1 = name1;
this.name2 = name2;
}
@Override
public void onExchangeDone(Route route, Exchange exchange) {
String stop = route.getId().equals(name1) ? name1 : name2;
String start = route.getId().equals(name1) ? name2 : name1;
CamelContext context = exchange.getContext();
try {
context.getInflightRepository().remove(exchange);
context.stopRoute(stop);
context.startRoute(start);
} catch (Exception e) {
// let the exception handle handle it, which is often just to log it
getExceptionHandler().handleException("Error flipping routes", e);
}
}
}
カスタムRoutePolicyの初期化時
に、制御するRoute名をメンバー変
数にセットするため
org.apache.camel.impl.RoutePolicySupport
を拡張する
Exchange処理完了時
のCallbackを実装する
交互に起動/停⽌を実現
するため、Start/Stopす
るRoute名を⼊れ替え
InflightRepositoryからExchangeを
削して、Route1を停⽌し、Route2を
起動する
16. l Routeが停⽌する際に、処理中Exchangeがない場合、即時に停⽌処理が
⾏われます。
l Routeが停⽌する際に処理中Exchangeがあった場合は、処理中のデータ
が無くなるまで停⽌処理を待機します(Graceful Shutdown)
l デフォルトで300秒経過しても処理中のデータがあって停⽌が完了しない
場合は、Routeが強制的に停⽌されます。
GracefulShutdownを制御する
202016
SpringCamelContext - Apache Camel 2.20.1 (CamelContext: camel-1) is shutting down
DefaultShutdownStrategy - Starting to graceful shutdown 3 routes (timeout 10 seconds)
DefaultShutdownStrategy - Route: webservice shutdown complete, was consuming from:
cxf://bean:inventoryEndpoint
DefaultShutdownStrategy - Route: file shutdown complete, was consuming from:
file://target/inventory/updates
DefaultShutdownStrategy - Route: update shutdown complete, was consuming from:
direct://update
DefaultShutdownStrategy - Graceful shutdown of 3 routes completed in 0 seconds
SpringCamelContext - Apache Camel 2.20.1 (CamelContext: camel-1) uptime 0.684 seconds
SpringCamelContext - Apache Camel 2.20.1 (CamelContext: camel-1) is shutdown in 0.025
seconds
DefaultShutdownStrategy - Waiting as there are still 3 inflight and pending exchanges to
complete, timeout in 60 seconds
Inflights per route: [file = 2, update = 1]
ログ
ログ
17. l GracefulShutdownの待機時間のデフォルト値が300秒
l Junitから実⾏の場合、CamelTestSupportによって10秒にセット
l 待機時間を変更するには、
GracefulShutdownの待機時間
202017
context.getShutdownStrategy().setTimeout(20);
Java DSL
<bean id="shutdown"
class="org.apache.camel.impl.DefaultShutdownStrategy">
<property name="timeout" value="20"/>
</bean>
XML DSL
※ 他のオプションは、https://camel.apache.org/manual/latest/graceful-shutdown.html を参照
18. l Camel on Spring-boot
l Camel on Quarkus
l Embedding Camel in a Java application
l Running Camel in a web environment such as Apache Tomcat
l Running Camel inside WildFly
l Running Camel in an OSGi container such as Apache Karaf
l Running Camel in a container that supports CDI, such as
Apache Karaf or WildFly
Deploying Camel
202018
20. Camel on Quarkus
202020
l Quarkusとは
l Java仮想マシン (JVM) およびネイティブコンパイルの
ために作成されたフルスタックのJavaフレームワーク
l Java標準フレームワーク以外に、 Eclipse MicroProfile
と CDI (コンテキストと依存性注⼊) を備え
l Apache Kafka、RESTEasy (JAX-RS)、Hibernate ORM
(JPA)、Spring、Infinispan、Camel およびその他多数
のライブラリと組み合わせ可能
l Camel on Quarkus
l 300+ コンポーネントをQuarkus extensions
の形で提供
l Quarkusで実装した MicroProfileと連携
Microprofile
Quarkus
OpenJDK or GraalVM