Red Hat build of Apache Camel for Quarkus のスタートガイド
Red Hat build of Apache Camel for Quarkus のスタートガイド
概要
はじめに
Red Hat build of Apache Camel ドキュメントに関するフィードバック
エラーを報告したり、ドキュメントの改善を提案したりするには、Red Hat Jira アカウントにログインし、課題を送信してください。Red Hat Jira アカウントをお持ちでない場合は、アカウントを作成するように求められます。
手順
- チケットを作成するには、次のリンクで チケットを作成します。
- Summary に課題の簡単な説明を入力します。
- Description に課題や機能拡張の詳細な説明を入力します。問題があるドキュメントのセクションへの URL も記載してください。
- Submit をクリックすると、課題が作成され、適切なドキュメントチームに転送されます。
第1章 Red Hat build of Apache Camel for Quarkus の概要
Red Hat build of Apache Camel for Quarkus は、Apache Camel とその膨大なコンポーネントライブラリーの統合機能を Quarkus ランタイムに提供します。
Red Hat build of Apache Camel for Quarkus を使用する利点は次のとおりです。
- パフォーマンス上の利点、開発者満足 (Developer Joy)、および Quarkus が提供するコンテナーファーストの理念を活用できます。
- 多くの Camel コンポーネント用の Quarkus エクステンションを利用できます。
- Camel で行われた多くのパフォーマンスの改善を利用して、メモリーフットプリントを削減し、リフレクションへの依存を減らし、起動時間を短縮できます。
第2章 統合されたオープンソース機能にアクセス
Red Hat build of Apache Camel は、さまざまな環境で認定およびサポートされており、オープンソースインテグレーションプロジェクトにおける最適なの部分を強力なエンタープライズ対応ツールキットに組み合わせて、現代のビジネスにおけるクラウドネイティブインテグレーションを簡素化および加速するように設計されています。
このようなインテグレーションは次のとおりです。
- エンタープライズ 統合パターンを実装し、数百の事前ビルドされたコンポーネントおよびコネクターを提供する Apache Camel 統合フレームワーク。
- Kaoto Apache Camel のビジュアルデザイナー。
- インテグレーションのトラブルシューティングおよびリモート管理を行う Hawtio モジュール Web コンソール。
- Simple Object Access Protocol (SOAP) Web サービスを開発および使用するための Apache CXF。
- 反復統合用の Camel CLI。
- コードのサポートおよびデバッグ用の VS Code 開発ツール。
- ライセンスライブラリーを必要とする 追加の Camel コンポーネント。
- Backstage の Camel ゴールデンパステンプレート。
- Prometheus および OpenTelemetry による監視およびトレース。
- Narayana トランザクションマネージャー。
- Quarkus および Spring Boot ランタイム。
- 同時セキュリティー更新 のある Quarkus Platform のメンバー です。
第3章 ツール
3.1. 言語
Red Hat build of Apache Camel for Quarkus では、以下の言語を使用して Camel ルートを定義できます。
- Java DSL
- YAML
- XML IO
3.2. Hawtio Diagnostic Console
Hawtio Diagnostic Console は、React や PatternFly などの最新の Web テクノロジーで構築された Red Hat build of Apache Camel 用のプラグ可能な Web 診断コンソールです。Hawtio は、有効なプラグインに応じて、デプロイされた 1 つ以上の HawtIO 対応コンテナーの詳細を確認および管理するための中央インターフェイスを提供します。HawtIO およびシステムリソースの監視、更新の実行、およびサービスの起動と停止を行うことができます。
詳細は、HawtIO Diagnostic Console ドキュメントの Setting up Quarkus applications for HawtIO Online with Jolokia セクションを参照してください。
3.3. Kaoto
Kaoto (Kamel Orchestration Tool)は、インテグレーションを作成および編集できる Apache Camel をベースとしたコード統合デザイナーです。Kaoto は拡張可能で柔軟性があり、さまざまなユースケースに適応できます。
詳細は、Kaoto ドキュメント の Camel ルートの作成 セクションを参照してください。
3.4. Camel CLI
Camel CLI は、Camel ルートの作成と実行に使用できる JBang をベースとする Camel アプリケーションです。
詳細は、Red Hat build of Apache Camel for Quarkus Tooling Guide の Creating and running Camel routes セクションを参照してください。
3.5. IDE プラグイン
プラグインは、言語サポート、コード/設定補完、プロジェクト作成ウィザードなどでインストールできます。プラグインは、それぞれの IDE マーケットプレイスで利用できます。
これらのプラグインのすべてが Red Hat によって提供されているわけではなく、一部は現在サポートされていませんが、ここでは検討のためのオプションとして提供されています。
VS Code:
Eclipse:
- Eclipse plugin (現在はサポートされていません)
Jetbrains:
- IntelliJ plugin (現在はサポートされていません)
プラグインのドキュメントを参照して、お好みの IDE プロジェクトの作成方法を見つけてください。
3.6. Camel コンテンツアシスト
以下のプラグインは、Camel ルートおよび application.properties
の編集時にコンテンツアシストをサポートします。
Eclipse:
JetBrains:
- Apache Camel IDEA プラグイン (常に最新の状態であるとは限りません)
VS Code:
- VS Code Language support for Camel
- Java、YAML、または XML で記述された Camel 統合をローカルでデバッグするための Apache Camel 用デバッグアダプター
- YAML
- XML
その他:
- Language Server Protocol をサポートする他の IDE を使用している場合は、Camel Language Server を手動でインストールおよび設定できます。
Red Hat build of Apache Camel でのツールの詳細は、Tooling Guide を参照してください。
開発サポートの範囲の詳細は、Red Hat サポートポータル の 開発サポートの対象範囲 を参照してください(ログインが必要)。
第4章 Red Hat build of Apache Camel for Quarkus のスタートガイド
このガイドでは、Red Hat build of Apache Camel for Quarkus、プロジェクトを作成するさまざまな方法、および Red Hat build of Apache Camel for Quarkus を使用してアプリケーションの構築を開始する方法を紹介します。
4.1. Red Hat build of Apache Camel for Quarkus の概要
Red Hat build of Apache Camel for Quarkus は、Apache Camel とその膨大なコンポーネントライブラリーの統合機能を Quarkus ランタイムに提供します。
Red Hat build of Apache Camel for Quarkus を使用する利点は次のとおりです。
- パフォーマンス上の利点、開発者満足 (Developer Joy)、および Quarkus が提供するコンテナーファーストの理念を活用できます。
- 多くの Camel コンポーネント用の Quarkus エクステンションを利用できます。
- Camel で行われた多くのパフォーマンスの改善を利用して、メモリーフットプリントを削減し、リフレクションへの依存を減らし、起動時間を短縮できます。
4.2. Red Hat build of Apache Camel for Quarkus を使用した最初のプロジェクトのビルド
4.2.1. 概要
code.quarkus.redhat.com を使用して、Quarkus Maven プロジェクトを生成できます。このプロジェクトは、アプリケーションで使用するエクステンションを自動的に追加および設定します。
このセクションでは、Red Hat build of Apache Camel for Quarkus を使用して Quarkus Maven プロジェクトを作成するプロセスを説明します。このプロセスには以下が含まれます。
- code.quarkus.redhat.com を使用したスケルトンアプリケーションの作成
- 簡単な Camel ルートの追加
- アプリケーションコードの探索
- 開発モードでのアプリケーションのコンパイル
- アプリケーションのテスト
4.2.2. code.quarkus.redhat.com を使用したスケルトンアプリケーションの生成
code.quarkus.redhat.com で、プロジェクトのブートストラップと生成を行うことができます。
Red Hat build of Apache Camel for Quarkus エクステンションは、'Integration' という見出しの下にあります。
追加のエクステンションが必要な場合は、'search' フィールドを使用して見つけてください。
使用するコンポーネントエクステンションを選択し、'Generate your application' をクリックして基本的なスケルトンプロジェクトをダウンロードします。
プロジェクトを GitHub に直接プッシュすることもできます。
code.quarkus.redhat.com
を使用して Quarkus Maven プロジェクトを生成する方法の詳細は、Red Hat build of Quarkus のスタートガイド の code.quarkus.redhat.com を使用した Quarkus Maven プロジェクトの作成 を参照してください。
手順
code.quarkus.redhat.com Web サイトで、次のエクステンションを選択します。
-
camel-quarkus-rest
-
camel-quarkus-jackson
camel-quarkus-direct
注記code.quarkus.redhat.com のアプリケーションをコンパイルしないでください (手順の最終ステップ)。代わりに、以下の 「開発モード」 セクションで説明されているコンパイルコマンドを使用してください。
-
直前の手順で生成されたプロジェクトファイルの展開先ディレクトリーに移動します。
cd <directory_name>
$ cd <directory_name>
Copy to Clipboard Copied!
4.2.3. アプリケーションコードの探索
このアプリケーションには、<dependencyManagement>
でインポートされた com.redhat.quarkus.platform:quarkus-camel-bom
内で管理されるコンパイル依存関係が 2 つあります。
pom.xml
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id> <quarkus.platform.group-id>com.redhat.quarkus.platform</quarkus.platform.group-id> <quarkus.platform.version> <!-- The latest 3.20.x version from https://maven.repository.redhat.com/ga/com/redhat/quarkus/platform/quarkus-bom --> </quarkus.platform.version> ... <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>${quarkus.platform.artifact-id}</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>${quarkus.platform.group-id}</groupId> <artifactId>quarkus-camel-bom</artifactId> <version>${quarkus.platform.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>com.redhat.quarkus.platform</quarkus.platform.group-id>
<quarkus.platform.version>
<!-- The latest 3.20.x version from https://maven.repository.redhat.com/ga/com/redhat/quarkus/platform/quarkus-bom -->
</quarkus.platform.version>
...
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>quarkus-camel-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
BOM 依存関係管理の詳細は、Red Hat build of Apache Camel for Quarkus を使用したアプリケーションの開発を参照してください。
アプリケーションは、src/main/resources/application.properties
内で定義されたプロパティーで設定できます。たとえば、camel.context.name
を設定できます。
4.2.4. 簡単な Camel ルートの追加
この例では、camel-quarkus-examples リポジトリーの簡単な例を使用します。これは、2 つの単純なクラス Fruit.java
、Legume.java
、およびルート定義 Routes.java
で構成されています。
手順
-
src/main/java/org/acme/
サブフォルダーにFruit.java
という名前のファイルを作成します。 以下のコードスニペットのようにクラスを追加します。
Fruit.java
package org.acme.rest.json; import java.util.Objects; import io.quarkus.runtime.annotations.RegisterForReflection; /** * A REST entity representing a fruit. */ @RegisterForReflection // Lets Quarkus register this class for reflection during the native build public class Fruit { private String name; private String description; public Fruit() { } public Fruit(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public boolean equals(Object obj) { if (!(obj instanceof Fruit)) { return false; } Fruit other = (Fruit) obj; return Objects.equals(other.name, this.name); } @Override public int hashCode() { return Objects.hash(this.name); } }
package org.acme.rest.json; import java.util.Objects; import io.quarkus.runtime.annotations.RegisterForReflection; /** * A REST entity representing a fruit. */ @RegisterForReflection // Lets Quarkus register this class for reflection during the native build public class Fruit { private String name; private String description; public Fruit() { } public Fruit(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public boolean equals(Object obj) { if (!(obj instanceof Fruit)) { return false; } Fruit other = (Fruit) obj; return Objects.equals(other.name, this.name); } @Override public int hashCode() { return Objects.hash(this.name); } }
Copy to Clipboard Copied! -
src/main/java/org/acme/
サブフォルダーにLegume.java
という名前のファイルを作成します。 以下のコードスニペットのようにクラスを追加します。
Legume.java
package org.acme.rest.json; import java.util.Objects; import io.quarkus.runtime.annotations.RegisterForReflection; /** * A REST entity representing a legume. */ @RegisterForReflection // Lets Quarkus register this class for reflection during the native build public class Legume { private String name; private String description; public Legume() { } public Legume(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public boolean equals(Object obj) { if (!(obj instanceof Legume)) { return false; } Legume other = (Legume) obj; return Objects.equals(other.name, this.name); } @Override public int hashCode() { return Objects.hash(this.name); } }
package org.acme.rest.json; import java.util.Objects; import io.quarkus.runtime.annotations.RegisterForReflection; /** * A REST entity representing a legume. */ @RegisterForReflection // Lets Quarkus register this class for reflection during the native build public class Legume { private String name; private String description; public Legume() { } public Legume(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public boolean equals(Object obj) { if (!(obj instanceof Legume)) { return false; } Legume other = (Legume) obj; return Objects.equals(other.name, this.name); } @Override public int hashCode() { return Objects.hash(this.name); } }
Copy to Clipboard Copied! -
src/main/java/org/acme/
サブフォルダーにRoutes.java
という名前のファイルを作成します。 以下のコードスニペットのように Camel Rest ルートを追加します。
Routes.java
package org.acme.rest.json; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.rest.RestBindingMode; /** * Camel route definitions. */ public class Routes extends RouteBuilder { private final Set<Fruit> fruits = Collections.synchronizedSet(new LinkedHashSet<>()); private final Set<Legume> legumes = Collections.synchronizedSet(new LinkedHashSet<>()); public Routes() { /* Let's add some initial fruits */ this.fruits.add(new Fruit("Apple", "Winter fruit")); this.fruits.add(new Fruit("Pineapple", "Tropical fruit")); /* Let's add some initial legumes */ this.legumes.add(new Legume("Carrot", "Root vegetable, usually orange")); this.legumes.add(new Legume("Zucchini", "Summer squash")); } @Override public void configure() throws Exception { restConfiguration().bindingMode(RestBindingMode.json); rest("/fruits") .get() .to("direct:getFruits") .post() .type(Fruit.class) .to("direct:addFruit"); rest("/legumes") .get() .to("direct:getLegumes"); from("direct:getFruits") .setBody().constant(fruits); from("direct:addFruit") .process().body(Fruit.class, fruits::add) .setBody().constant(fruits); from("direct:getLegumes") .setBody().constant(legumes); } }
package org.acme.rest.json; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.model.rest.RestBindingMode; /** * Camel route definitions. */ public class Routes extends RouteBuilder { private final Set<Fruit> fruits = Collections.synchronizedSet(new LinkedHashSet<>()); private final Set<Legume> legumes = Collections.synchronizedSet(new LinkedHashSet<>()); public Routes() { /* Let's add some initial fruits */ this.fruits.add(new Fruit("Apple", "Winter fruit")); this.fruits.add(new Fruit("Pineapple", "Tropical fruit")); /* Let's add some initial legumes */ this.legumes.add(new Legume("Carrot", "Root vegetable, usually orange")); this.legumes.add(new Legume("Zucchini", "Summer squash")); } @Override public void configure() throws Exception { restConfiguration().bindingMode(RestBindingMode.json); rest("/fruits") .get() .to("direct:getFruits") .post() .type(Fruit.class) .to("direct:addFruit"); rest("/legumes") .get() .to("direct:getLegumes"); from("direct:getFruits") .setBody().constant(fruits); from("direct:addFruit") .process().body(Fruit.class, fruits::add) .setBody().constant(fruits); from("direct:getLegumes") .setBody().constant(legumes); } }
Copy to Clipboard Copied! この例の詳細は、camel-quarkus-examples リポジトリー を参照してください。
4.2.5. 開発モード
mvn clean compile quarkus:dev
$ mvn clean compile quarkus:dev
このコマンドでは、プロジェクトのコンパイル、アプリケーションの起動、Quarkus ツールでのワークスペースの変更監視などを行います。プロジェクトに加えた変更は、実行中のアプリケーションに自動的に反映されます。
ブラウザーでアプリケーションを確認できます。(たとえば、rest-json
サンプルアプリケーションの場合は、http://localhost:8080/fruits
にアクセスします。)
アプリケーションコードを変更する場合 (例: 'Apple' を 'Orange' に変更)、アプリケーションは自動的に更新されます。適用された変更を確認するには、ブラウザーを更新してください。
開発モードの詳細は、Quarkus のドキュメントの 開発モード のセクションを参照してください。
../Camel-extensions-development/proc-testing-your-first-camel-extensions-for-quarkus-project.adoc
4.2.6. アプリケーションのパッケージ化と実行
4.2.6.1. JVM モード
手順
mvn package
を実行して、ストック JVM で実行するための thinjar
を準備します。mvn clean package ls -lh target/quarkus-app
$ mvn clean package $ ls -lh target/quarkus-app ... -rw-r--r--. 1 user user 238K Oct 11 18:55 quarkus-run.jar ...
Copy to Clipboard Copied! 注記シン形式の
jar
にはアプリケーションコードのみが含まれます。実行するには、target/quarkus-app/lib
の依存関係も必要です。次のように jar を実行します。
java -jar target/quarkus-app/quarkus-run.jar
$ java -jar target/quarkus-app/quarkus-run.jar ... [io.quarkus] (main) Quarkus started in 1.163s. Listening on: http://[::]:8080
Copy to Clipboard Copied!
起動時間は約 1 秒です。
4.2.6.2. ネイティブモード
手順
ネイティブ実行可能ファイルを準備するには、次のようにします。
コマンド
mvn clean package -Pnative
を実行します。mvn clean package -Pnative ls -lh target
$ mvn clean package -Pnative $ ls -lh target ... -rwxr-xr-x. 1 user user 46M Oct 11 18:57 code-with-quarkus-1.0.0-SNAPSHOT-runner ...
Copy to Clipboard Copied! 注記runner
には.jar
拡張子がなく、x
(実行可能) パーミッションが設定されています。これは次のように直接実行できます。./target/*-runner
$ ./target/*-runner ... [io.quarkus] (main) Quarkus started in 0.013s. Listening on: http://[::]:8080 ...
Copy to Clipboard Copied! アプリケーションは 13 ミリ秒で開始しました。
ps -o rss,command -p $(pgrep code-with)
コマンドでメモリー使用量を表示します。ps -o rss,command -p $(pgrep code-with)
$ ps -o rss,command -p $(pgrep code-with) RSS COMMAND 65852 ./target/code-with-quarkus-1.0.0-SNAPSHOT-runner
Copy to Clipboard Copied! アプリケーションは 65 MB のメモリーを使用します。
ネイティブ実行可能ファイルの準備に関する追加情報は、Quarkus アプリケーションのネイティブ実行可能ファイルへのコンパイル ガイドの ネイティブ実行可能ファイルの作成 を参照してください。
Quarkus Native executable guide には、コンテナーイメージを作成する手順 などの詳細情報が記載されています。
第5章 Quarkus アプリケーションのデプロイ
以下のビルドストラテジーのいずれかを使用して、Quarkus アプリケーションを OpenShift にデプロイできます。
- docker ビルド
- S2I バイナリー
- Source S2I
これらの各ビルドストラテジーの詳細は、OpenShift Container Platform ビルドストラテジー および Quarkus アプリケーションの OpenShift Container Platform へのデプロイ ガイドの Red Hat ビルド の Quarkus を参照してください。
OpenShift Docker ビルドストラテジーは、推奨されるストラテジーであり、JVM を対象とした Quarkus アプリケーションおよびネイティブ実行可能ファイルにコンパイルされた Quarkus アプリケーションをサポートします。quarkus.openshift.build-strategy
プロパティーを使用して、デプロイメントストラテジーを設定できます。
第6章 テスト
6.1. Camel Quarkus エクステンションのテスト
テストは、Camel ルートが長期にわたって期待どおりに動作することを確認するための良い方法です。まだの場合は、「Camel Quarkus User guide」の First Steps セクションと Quarkus ドキュメントの Testing your application セクションをご一読ください。
Quarkus のコンテキストでルートをテストする場合、ローカル統合テストを作成することが推奨されます。これには、JVM モードとネイティブモードの両方をカバーできるという利点があります。
JVM モードでは、CamelTestSupport
スタイルのテスト を使用できます。
6.1.1. JVM モードでの実行
JVM モードでは、@QuarkusTest
アノテーションを使用して Quarkus をブートストラップし、@Test
ロジックが実行される 前 に Camel ルートを起動します。
以下に例を示します。
import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; @QuarkusTest class MyTest { @Test public void test() { // Use any suitable code that sends test data to the route and then assert outcomes ... } }
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
@QuarkusTest
class MyTest {
@Test
public void test() {
// Use any suitable code that sends test data to the route and then assert outcomes
...
}
}
サンプル実装は Camel Quarkus ソースにあります。
6.1.2. ネイティブモードでの実行
サポートされているすべてのエクステンションに対して、アプリケーションがネイティブモードで動作することを常にテストしてください。
それぞれの JVM モードクラスからロジックを継承することにより、JVM モード用に定義されたテストロジックを再利用できます。
@QuarkusIntegrationTest
アノテーションを追加して、Quarkus JUnit エクステンションに、テスト対象のアプリケーションをネイティブイメージにコンパイルし、テストを実行する前にイメージを起動するように指示します。
import io.quarkus.test.junit.QuarkusIntegrationTest; @QuarkusIntegrationTest class MyIT extends MyTest { ... }
import io.quarkus.test.junit.QuarkusIntegrationTest;
@QuarkusIntegrationTest
class MyIT extends MyTest {
...
}
サンプル実装は Camel Quarkus ソースにあります。
6.1.3. @QuarkusTest
と @QuarkusIntegrationTest
の違い
ネイティブ実行可能ファイルは、バイトコードではなくネイティブコードであるため、実行に JVM を必要とせず、また JVM で実行することもできません。
従来の JVM を使用して実行するために、テストをネイティブコードにコンパイルしても意味はありません。
そのため、テストとアプリケーション間の通信は、ネットワーク (HTTP/REST、またはアプリケーションが使用するその他のプロトコル)、ファイルシステム (ログファイルなど) の監視、またはその他のプロセス間通信を介して行う必要があります。
6.1.3.1. JVM モードでの @QuarkusTest
JVM モードでは、@QuarkusTest
のアノテーションが付けられたテストは、テスト対象のアプリケーションと同じ JVM で実行されます。
これは、@Inject
を使用して、アプリケーションからテストコードに Bean を追加できることを意味します。
@jakarta.enterprise.inject.Alternative
および @jakarta.annotation.Priority
を使用して、新しい Bean を定義したり、アプリケーションから Bean をオーバーライドしたりすることもできます。
6.1.3.2. ネイティブモードでの @QuarkusIntegrationTest
ネイティブモードでは、@QuarkusIntegrationTest
のアノテーションが付けられたテストは、実行中のネイティブアプリケーションとは別のプロセスでホストされている JVM で実行されます。
この重要な結果として、テストとネイティブアプリケーション間のすべての通信は、次の 1 つ以上の形式を取る必要があります。
- ネットワークの呼び出し。通常は、HTTP またはアプリケーションがサポートするその他のネットワークプロトコルです。
-
ファイルシステムの変更を監視します。(例: Camel
file
エンドポイント経由) - その他の種類のプロセス間通信。
QuarkusIntegrationTest
は、@QuarkusTest
では利用できない追加機能を提供します。
- JVM モードでは、Quarkus ビルドによって生成された実行可能なアプリケーション JAR を起動してテストできます。
- ネイティブモードでは、Quarkus ビルドによって作成されたネイティブアプリケーションを起動してテストできます。
- コンテナーイメージをビルドに追加すると、コンテナーが起動し、それに対してテストが実行されます。
QuarkusIntegrationTest
の詳細は、Quarkus testing guide を参照してください。
6.1.4. 外部サービスによるテスト
6.1.4.1. Testcontainers
アプリケーションは、メッセージングブローカー、データベース、その他のサービスなどの外部リソースにアクセスする必要がある場合があります。
対象のサービスのコンテナーイメージが利用可能な場合は、Testcontainers を使用して、テスト中にサービスを起動および設定できます。
6.1.4.1.1. QuarkusTestResourceLifecycleManager
を使用して設定データを渡す
アプリケーションを正常に動作させるためには、多くの場合、接続設定データ (ホスト、ポート、ユーザー、リモートサービスのパスワード) をアプリケーションの起動前に渡すことが不可欠です。
Quarkus エコシステムでは、QuarkusTestResourceLifecycleManager
がこの目的を果たします。
start()
メソッドで 1 つ以上の Testcontainer を起動し、メソッドから接続設定を Map
の形式で返すことができます。
このマップのエントリーは、モードに応じてさまざまな方法でアプリケーションに渡されます。
-
ネイティブモード: コマンドライン (
-Dkey=value
) - JVM モード: 特別な MicroProfile 設定プロバイダー
コマンドラインおよび MicroProfile 設定は、application.properties
ファイルの設定よりも優先されます。
import java.util.Map; import java.util.HashMap; import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.Wait; public class MyTestResource implements QuarkusTestResourceLifecycleManager { private GenericContainer<?> myContainer; @Override public Map<String, String> start() { // Start the needed container(s) myContainer = new GenericContainer(DockerImageName.parse("my/image:1.0.0")) .withExposedPorts(1234) .waitingFor(Wait.forListeningPort()); myContainer.start(); // Pass the configuration to the application under test // You can also pass camel component property names / values to automatically configure Camel components return new HashMap<>() {{ put("my-container.host", container.getHost()); put("my-container.port", "" + container.getMappedPort(1234)); }}; } @Override public void stop() { // Stop the needed container(s) myContainer.stop(); ... } }
import java.util.Map;
import java.util.HashMap;
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
public class MyTestResource implements QuarkusTestResourceLifecycleManager {
private GenericContainer<?> myContainer;
@Override
public Map<String, String> start() {
// Start the needed container(s)
myContainer = new GenericContainer(DockerImageName.parse("my/image:1.0.0"))
.withExposedPorts(1234)
.waitingFor(Wait.forListeningPort());
myContainer.start();
// Pass the configuration to the application under test
// You can also pass camel component property names / values to automatically configure Camel components
return new HashMap<>() {{
put("my-container.host", container.getHost());
put("my-container.port", "" + container.getMappedPort(1234));
}};
}
@Override
public void stop() {
// Stop the needed container(s)
myContainer.stop();
...
}
}
@QuarkusTestResource
を使用して、テストクラスから定義済みのテストリソースを参照します。
import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; @QuarkusTest @QuarkusTestResource(MyTestResource.class) class MyTest { ... }
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
@QuarkusTestResource(MyTestResource.class)
class MyTest {
...
}
サンプル実装は Camel Quarkus ソースにあります。
6.1.4.2. WireMock
ライブエンドポイントが利用できない、信頼性が低い、コストがかかる場合などには、テストをライブエンドポイントに接続する代わりに、サードパーティーのサービスおよび API との HTTP インタラクションをスタブできます。
WireMock を使用すると、HTTP インタラクションをモックおよび記録できます。これは、さまざまなコンポーネントエクステンション用に Camel Quarkus テストスイート全体で広く使用されています。
6.1.4.2.1. WireMock のセットアップ
手順
WireMock サーバーをセットアップします。
注記テストを行う Camel コンポーネントを常に設定して、すべての HTTP 対話を WireMock プロキシー経由で渡すようにします。これは、API エンドポイント URL を決定するコンポーネントプロパティーを設定することで実現できます。
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import java.util.HashMap; import java.util.Map; import com.github.tomakehurst.wiremock.WireMockServer; import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; public class WireMockTestResource implements QuarkusTestResourceLifecycleManager { private WireMockServer server; @Override public Map<String, String> start() { // Setup & start the server server = new WireMockServer( wireMockConfig().dynamicPort() ); server.start(); // Stub an HTTP endpoint. WireMock also supports a record and playback mode // https://wiremock.org/docs/record-playback/ server.stubFor( get(urlEqualTo("/api/greeting")) .willReturn(aResponse() .withHeader("Content-Type", "application/json") .withBody("{\"message\": \"Hello World\"}"))); // Ensure the camel component API client passes requests through the WireMock proxy Map<String, String> conf = new HashMap<>(); conf.put("camel.component.foo.server-url", server.baseUrl()); return conf; } @Override public void stop() { if (server != null) { server.stop(); } } }
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import java.util.HashMap; import java.util.Map; import com.github.tomakehurst.wiremock.WireMockServer; import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; public class WireMockTestResource implements QuarkusTestResourceLifecycleManager { private WireMockServer server; @Override public Map<String, String> start() { // Setup & start the server server = new WireMockServer( wireMockConfig().dynamicPort() ); server.start(); // Stub an HTTP endpoint. WireMock also supports a record and playback mode // https://wiremock.org/docs/record-playback/ server.stubFor( get(urlEqualTo("/api/greeting")) .willReturn(aResponse() .withHeader("Content-Type", "application/json") .withBody("{\"message\": \"Hello World\"}"))); // Ensure the camel component API client passes requests through the WireMock proxy Map<String, String> conf = new HashMap<>(); conf.put("camel.component.foo.server-url", server.baseUrl()); return conf; } @Override public void stop() { if (server != null) { server.stop(); } } }
Copy to Clipboard Copied! -
テストクラスに
@QuarkusTestResource
アノテーションがあり、適切なテストリソースクラスが値として指定されていることを確認します。WireMock サーバーは、すべてのテストが実行される前に起動され、すべてのテストが終了するとシャットダウンされます。
import io.quarkus.test.common.QuarkusTestResource; import io.quarkus.test.junit.QuarkusTest; @QuarkusTest @QuarkusTestResource(WireMockTestResource.class) class MyTest { ... }
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
@QuarkusTest
@QuarkusTestResource(WireMockTestResource.class)
class MyTest {
...
}
WireMock サーバーは、すべてのテストが実行される前に起動し、すべてのテストが終了するとシャットダウンします。
サンプルの実装は、Camel Quarkus 統合テストソースツリーにあります。
6.1.5. CamelQuarkusTestSupport
での CamelTestSupport
スタイルのテスト
Camel Quarkus 2.13.0 以降、CamelQuarkusTestSupport
をテストに使用できます。これは CamelTestSupport
の後継で、Quarkus では適切に機能しません。
CamelQuarkusTestSupport
は、JVM モードでのみ機能します。ネイティブモードでテストする必要がある場合は、上記の代替テストストラテジーのいずれかを使用します。
6.1.5.1. JVM モードでの CamelQuarkusTestSupport
によるテスト
次の依存関係をモジュール (できれば test
スコープ内) に追加します。
<dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-junit5</artifactId> <scope>test</scope> </dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
次のように、テストで CamelQuarkusTestSupport
を使用できます。
@QuarkusTest @TestProfile(SimpleTest.class) //necessary only if "newly created" context is required for the test (worse performance) public class SimpleTest extends CamelQuarkusTestSupport { ... }
@QuarkusTest
@TestProfile(SimpleTest.class) //necessary only if "newly created" context is required for the test (worse performance)
public class SimpleTest extends CamelQuarkusTestSupport {
...
}
6.1.5.2. テスト用の CamelContext
のカスタマイズ
設定プロファイル、CDI Bean、オブザーバー、mocks などを使用してテスト用に CamelContext
をカスタマイズできます。createCamelContext
メソッドをオーバーライドして、CamelContext
と直接対話することもできます。
createCamelContext
を使用する場合は、新しい CamelContext
をインスタンス化して返さ ないようにしてください。代わりに、super.createCamelContext()
を呼び出して、返された CamelContext
を必要に応じて変更します。このルールに従わないと、例外が発生します。
@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @Override protected CamelContext createCamelContext() throws Exception { // Must call super to get a handle on the application scoped CamelContext CamelContext context = super.createCamelContext(); // Apply customizations context.setTracing(true); // Return the modified CamelContext return context; } }
@QuarkusTest
class SimpleTest extends CamelQuarkusTestSupport {
@Override
protected CamelContext createCamelContext() throws Exception {
// Must call super to get a handle on the application scoped CamelContext
CamelContext context = super.createCamelContext();
// Apply customizations
context.setTracing(true);
// Return the modified CamelContext
return context;
}
}
6.1.5.3. テスト用のルートの設定
アプリケーション内の RouteBuilder
を拡張するすべてのクラスのルートは、CamelContext
に自動的に追加されます。同様に、camel.main.routes-include-pattern
から設定された XML または YAML ルートも読み込まれます。
これは必ずしもテストに適切であるとは限りません。設定プロパティーを使用して、テスト時にどのルートをロードするかを制御します。
-
quarkus.camel.routes-discovery.include-patterns
-
quarkus.camel.routes-discovery.exclude-patterns
-
camel.main.routes-include-pattern
-
camel.main.routes-exclude-pattern
createRouteBuilder
をオーバーライドして、テストクラスごとにテスト固有のルートを定義することもできます。
@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @Test void testGreeting() { MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); mockEndpoint.expectedBodiesReceived("Hello World"); template.sendBody("direct:start", "World"); mockEndpoint.assertIsSatisified(); } @Override protected RoutesBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .transform().simple("Hello ${body}") .to("mock:result"); } }; } }
@QuarkusTest
class SimpleTest extends CamelQuarkusTestSupport {
@Test
void testGreeting() {
MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
mockEndpoint.expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "World");
mockEndpoint.assertIsSatisified();
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.transform().simple("Hello ${body}")
.to("mock:result");
}
};
}
}
6.1.5.4. CamelContext テストライフサイクル
CamelTestSupport
と比較した CamelQuarkusTestSupport
の主な違いの 1 つは、CamelContext
ライフサイクルがどのように管理されるかです。
Camel Quarkus では、ランタイムによって単一の CamelContext
が自動的に作成されます。デフォルトでは、この CamelContext
はすべてのテスト間で共有され、これは、テストスイート全体が実行中は起動状態のままになります。
これにより、テストに予期しない副作用が生じる可能性があります。テスト間で CamelContext
を再起動する必要がある場合は、カスタム テストプロファイル を作成して、テスト対象のアプリケーションを強制的に再起動できます。
たとえば、テストプロファイルを定義するには、次のようにします。
@QuarkusTest class MyTestProfile implements QuarkusTestProfile { ... }
@QuarkusTest
class MyTestProfile implements QuarkusTestProfile {
...
}
次に、@TestProfile
を使用してテストクラスで参照します。
// @TestProfile will trigger the application to be restarted @TestProfile(MyTestProfile.class) @QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { ... }
// @TestProfile will trigger the application to be restarted
@TestProfile(MyTestProfile.class)
@QuarkusTest
class SimpleTest extends CamelQuarkusTestSupport {
...
}
CamelContext
の stop()
メソッドと start()
メソッドを呼び出して手動で再起動できません。これにより、例外が発生します。
6.1.5.5. 例
6.1.5.5.1. シンプルな RouteBuilder
およびテストクラス
シンプルな RouteBuilder
:
public class MyRoutes extends RouteBuilder { @Override public void configure() { from("direct:start") .transform().simple("Hello ${body}") .to("mock:result"); } }
public class MyRoutes extends RouteBuilder {
@Override
public void configure() {
from("direct:start")
.transform().simple("Hello ${body}")
.to("mock:result");
}
}
メッセージペイロードを direct:start
エンドポイントに送信するテストを実行します。
@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @Test void testGreeting() { MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); mockEndpoint.expectedBodiesReceived("Hello World"); template.sendBody("direct:start", "World"); mockEndpoint.assertIsSatisified(); } }
@QuarkusTest
class SimpleTest extends CamelQuarkusTestSupport {
@Test
void testGreeting() {
MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
mockEndpoint.expectedBodiesReceived("Hello World");
template.sendBody("direct:start", "World");
mockEndpoint.assertIsSatisified();
}
}
6.1.5.5.2. AdviceWith
の使用
@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @BeforeEach public void beforeEach() throws Exception { AdviceWith.adviceWith(this.context, "advisedRoute", route -> { route.replaceFromWith("direct:replaced"); }); } @Override protected RoutesBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").routeId("advisedRoute") .transform().simple("Hello ${body}") .to("mock:result"); } }; } @Test void testAdvisedRoute() throws Exception { MockEndpoint mockEndpoint = getMockEndpoint("mock:result"); mockEndpoint.expectedBodiesReceived("Hello World"); template.sendBody("direct:replaced", "World"); mockEndpoint.assertIsSatisfied(); } }
@QuarkusTest
class SimpleTest extends CamelQuarkusTestSupport {
@BeforeEach
public void beforeEach() throws Exception {
AdviceWith.adviceWith(this.context, "advisedRoute", route -> {
route.replaceFromWith("direct:replaced");
});
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").routeId("advisedRoute")
.transform().simple("Hello ${body}")
.to("mock:result");
}
};
}
@Test
void testAdvisedRoute() throws Exception {
MockEndpoint mockEndpoint = getMockEndpoint("mock:result");
mockEndpoint.expectedBodiesReceived("Hello World");
template.sendBody("direct:replaced", "World");
mockEndpoint.assertIsSatisfied();
}
}
6.1.5.5.3. アドバイスの明示的な有効化
アドバイスを明示的に有効にする 場合は、AdviceWith
のセットアップを完了するときに startRouteDefinitions
を呼び出す必要があります。
startRouteDefinitions
を呼び出す必要があるのは、アドバイスされて いない ルートが設定されている場合のみです。
6.1.5.6. 制限
6.1.5.6.1. CamelTestSupport
から継承されたテストライフサイクルメソッド
CamelQuarkusTestSupport
は、CamelTestSupport
から一部のテストライフサイクルメソッドを継承します。ただし、これらは使用すべきではなく、代わりに CamelQuarkusTestSupport
の同等のメソッドに置き換えられます。
CamelTestSupport ライフサイクルメソッド | CamelQuarkusTestSupport 相当 |
---|---|
|
|
|
|
|
|
|
|
6.1.5.6.2. カスタム Camel レジストリーの作成はサポートされていません
createCamelRegistry
の CamelQuarkusTestSupport
実装は UnsupportedOperationException
を出力します。
オブジェクトを Camel レジストリーにバインドまたはバインド解除する必要がある場合は、次のいずれかの方法で実行できます。
名前付き CDI Bean を生成する
public class MyBeanProducers { @Produces @Named("myBean") public MyBean createMyBean() { return new MyBean(); } }
public class MyBeanProducers { @Produces @Named("myBean") public MyBean createMyBean() { return new MyBean(); } }
Copy to Clipboard Copied! -
createCamelContext
をオーバーライドし (上記の例を参照)、camelContext.getRegistry().bind ("foo"、fooBean)
を呼び出す。 @BindToRegistry
アノテーションを使用する。@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @BindToRegistry("myBean") MyBean myBean = new MyBean(); }
@QuarkusTest class SimpleTest extends CamelQuarkusTestSupport { @BindToRegistry("myBean") MyBean myBean = new MyBean(); }
Copy to Clipboard Copied! 注記個々のテストクラスから Camel レジストリーにバインドされた Bean は、テストスイートの実行中は保持されます。テストの期待値によっては、予期しない結果が生じる可能性があります。これを回避するには、テストプロファイルを使用して
CamelContext
を再起動できます。
第7章 Maven のローカルでの設定
一般的な Red Hat build of Apache Camel アプリケーションの開発では、プロジェクトのビルドと管理に Maven を使用します。
7.1. Maven 設定の準備
Maven は、Apache の無料のオープンソースビルドツールです。通常は、Maven を使用して Fuse アプリケーションを構築します。
手順
Maven ダウンロードページ から Maven 3.8.6 以降をダウンロードします。
ヒント正しい Maven および JDK バージョンがインストールされていることを確認するには、コマンドターミナルを開いて次のコマンドを入力します。
mvn --version
mvn --version
Copy to Clipboard Copied! 出力をチェックして、Maven がバージョン 3.8.6 以降であり、OpenJDK 17 を使用していることを確認します。
システムがインターネットに接続していることを確認します。
デフォルトの動作では、プロジェクトのビルド中、Maven は外部リポジトリーを検索し、必要なアーティファクトをダウンロードします。Maven はインターネット上でアクセス可能なリポジトリーを探します。
このデフォルト動作を変更し、Maven によってローカルネットワーク上のリポジトリーのみが検索されるようにすることができます。これは Maven をオフラインモードで実行できることを意味します。オフラインモードでは、Maven によってローカルリポジトリーのアーティファクトが検索されます。「ローカル Maven リポジトリーの使用」 を参照してください。
7.2. Maven への Red Hat リポジトリーの追加
Red Hat Maven リポジトリーにあるアーティファクトにアクセスするには、Red Hat Maven リポジトリーを Maven の settings.xml
ファイルに追加する必要があります。
Maven は、ユーザーのホームディレクトリーの .m2
ディレクトリーで settings.xml
ファイルを探します。ユーザー指定の settings.xml
ファイルがない場合、Maven は M2_HOME/conf/settings.xml
にあるシステムレベルの settings.xml
ファイルを使用します。
前提条件
Red Hat リポジトリーを追加する settings.xml
ファイルがある場所を把握している。
手順
以下の例のように、settings.xml
ファイルに Red Hat リポジトリーの repository
要素を追加します。
camel-jira
コンポーネントを使用している場合は、atlassian リポジトリーも追加します。
<?xml version="1.0"?> <settings> <profiles> <profile> <id>extra-repos</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>redhat-ga-repository</id> <url>https://maven.repository.redhat.com/ga</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>atlassian</id> <url>https://packages.atlassian.com/maven-external/</url> <name>atlassian external repo</name> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>redhat-ga-repository</id> <url>https://maven.repository.redhat.com/ga</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>extra-repos</activeProfile> </activeProfiles> </settings>
<?xml version="1.0"?>
<settings>
<profiles>
<profile>
<id>extra-repos</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>redhat-ga-repository</id>
<url>https://maven.repository.redhat.com/ga</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>atlassian</id>
<url>https://packages.atlassian.com/maven-external/</url>
<name>atlassian external repo</name>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>redhat-ga-repository</id>
<url>https://maven.repository.redhat.com/ga</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>extra-repos</activeProfile>
</activeProfiles>
</settings>
7.3. ローカル Maven リポジトリーの使用
インターネットへ接続せずにコンテナーを実行し、オフライン状態では使用できない依存関係を持つアプリケーションをデプロイする場合は、Maven 依存関係プラグインを使用してアプリケーションの依存関係を Maven オフラインリポジトリーにダウンロードできます。ダウンロード後、このカスタマイズされた Maven オフラインリポジトリーをインターネットに接続していないマシンに提供できます。
手順
pom.xml
ファイルが含まれるプロジェクトディレクトリーで、以下のようなコマンドを実行し、Maven プロジェクトのリポジトリーをダウンロードします。mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.0:go-offline -Dmaven.repo.local=/tmp/my-project
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.0:go-offline -Dmaven.repo.local=/tmp/my-project
Copy to Clipboard Copied! この例では、プロジェクトのビルドに必要な Maven 依存関係とプラグインは
/tmp/my-project
ディレクトリーにダウンロードされます。- このカスタマイズされた Maven オフラインリポジトリーを、インターネットに接続していない内部のマシンに提供します。
7.4. 環境変数またはシステムプロパティーを使用した Maven ミラーの設定
アプリケーションの実行時に、Red Hat Maven リポジトリーにあるアーティファクトにアクセスする必要があります。このリポジトリーは、Maven の settings.xml
ファイルに追加されます。Maven は以下の場所で settings.xml
を探します。
- 指定の URL を検索します。
-
見つからない場合は
${user.home}/.m2/settings.xml
を検索します。 -
見つからない場合は
${maven.home}/conf/settings.xml
を検索します。 -
見つからない場合は
${M2_HOME}/conf/settings.xml
を検索します。 -
どの場所にも見つからない場合は、空の
org.apache.maven.settings.Settings
インスタンスが作成されます。
7.4.1. Maven ミラー
Maven では、一連のリモートリポジトリーを使用して、ローカルリポジトリーで現在利用できないアーティファクトにアクセスします。ほとんどの場合、リポジトリーのリストには Maven Central リポジトリーが含まれますが、Red Hat Fuse では Maven Red Hat リポジトリーも含まれます。リモートリポジトリーへのアクセスが不可能な場合や許可されない場合は、Maven ミラーのメカニズムを使用できます。ミラーは、特定のリポジトリー URL を異なるリポジトリー URL に置き換えるため、リモートアーティファクトの検索時にすべての HTTP トラフィックを単一の URL に転送できます。
7.4.2. Maven ミラーの settings.xml
への追加
Maven ミラーを設定するには、以下のセクションを Maven の settings.xml
に追加します。
<mirror> <id>all</id> <mirrorOf>*</mirrorOf> <url>http://host:port/path</url> </mirror>
<mirror>
<id>all</id>
<mirrorOf>*</mirrorOf>
<url>http://host:port/path</url>
</mirror>
settings.xml
ファイルに上記のセクションがない場合は、ミラーが使用されません。XML 設定を提供せずにグローバルミラーを指定するには、システムプロパティーまたは環境変数を使用します。
7.4.3. 環境変数またはシステムプロパティーを使用した Maven ミラーの設定
環境変数またはシステムプロパティーのいずれかを使用して Maven ミラーを設定するには、以下を追加します。
-
環境変数 MAVEN_MIRROR_URL を
bin/setenv
ファイルに追加します。 -
システムプロパティー mavenMirrorUrl を
etc/system.properties
ファイルに追加します。
7.4.4. Maven オプションを使用した Maven ミラー URL の指定
環境変数またはシステムプロパティーによって指定された Maven ミラー URL ではなく、別の Maven ミラー URL を使用するには、アプリケーションの実行時に以下の Maven オプションを使用します。
-DmavenMirrorUrl=mirrorId::mirrorUrl
たとえば、
-DmavenMirrorUrl=my-mirror::http://mirror.net/repository
となります。-DmavenMirrorUrl=mirrorUrl
たとえば、
-DmavenMirrorUrl=http://mirror.net/repository
となります。この例では、<mirror> の <id> は mirror になります。
7.5. Maven アーティファクトおよびコーディネート
Maven ビルドシステムでは、アーティファクト が基本的なビルディングブロックです。ビルド後のアーティファクトの出力は、通常 JAR や WAR ファイルなどのアーカイブになります。
Maven の主な特徴として、アーティファクトを検索し、検索したアーティファクト間で依存関係を管理できる機能が挙げられます。Maven コーディネート は、特定のアーティファクトの場所を特定する値のセットです。基本的なコーディネートには、以下の形式の 3 つの値があります。
groupId:artifactId:version
Maven は、packaging の値、または packaging 値と classifier 値の両方を使用して基本的なコーディネートを拡張することがあります。Maven コーディネートには以下の形式のいずれかを使用できます。
groupId:artifactId:version groupId:artifactId:packaging:version groupId:artifactId:packaging:classifier:version
groupId:artifactId:version
groupId:artifactId:packaging:version
groupId:artifactId:packaging:classifier:version
値の説明は次のとおりです。
- groupdId
-
アーティファクトの名前の範囲を定義します。通常、パッケージ名のすべてまたは一部をグループ ID として使用します。たとえば、
org.fusesource.example
です。 - artifactId
- グループ名に関連するアーティファクト名を定義します。
- version
-
アーティファクトのバージョンを指定します。バージョン番号には
n.n.n.n
のように最大 4 つの部分を使用でき、最後の部分には数字以外の文字を使用できます。たとえば1.0-SNAPSHOT
の場合は、最後の部分が英数字のサブ文字列である0-SNAPSHOT
になります。 - packaging
-
プロジェクトのビルド時に生成されるパッケージ化されたエンティティーを定義します。OSGi プロジェクトでは、パッケージングは
bundle
になります。デフォルト値はjar
です。 - classifier
- 同じ POM からビルドされた内容が異なるアーティファクトを区別できるようにします。
次に示すように、アーティファクトの POM ファイル内の要素で、アーティファクトのグループ ID、アーティファクト ID、パッケージング、およびバージョンを定義します。
<project ... > ... <groupId>org.fusesource.example</groupId> <artifactId>bundle-demo</artifactId> <packaging>bundle</packaging> <version>1.0-SNAPSHOT</version> ... </project>
<project ... >
...
<groupId>org.fusesource.example</groupId>
<artifactId>bundle-demo</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>
...
</project>
前述のアーティファクトの依存関係を定義するには、以下の dependency
要素を POM ファイルに追加します。
<project ... > ... <dependencies> <dependency> <groupId>org.fusesource.example</groupId> <artifactId>bundle-demo</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> ... </project>
<project ... >
...
<dependencies>
<dependency>
<groupId>org.fusesource.example</groupId>
<artifactId>bundle-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
...
</project>
前述の依存関係に bundle
パッケージを指定する必要はありません。バンドルは特定タイプの JAR ファイルであり、jar
はデフォルトの Maven パッケージタイプであるためです。依存関係でパッケージタイプを明示的に指定する必要がある場合は、type
要素を使用できます。
第8章 サンプルアプリケーション
8.1. Red Hat build of Quarkus の例
Red Hat build of Quarkus examples リポジトリーには、さまざまなユースケースで Camel と統合する方法の例が多数含まれています。ベストプラクティスのアドバイスを提供し、インテグレーションやメッセージングの問題でよく見られるパターンを説明します。
この例は Maven を使用して実行できます。mvn
コマンドを使用すると、Maven は必要な依存関係を中央リポジトリーからローカルリポジトリーにダウンロードしようとします。
8.2. サンプルリポジトリー
例 | 説明 |
---|---|
MQTT プロトコルを使用して Apache Artemis ブローカーからメッセージが消費され、変換され、ElasticSearch にロードされる方法を示します。 | |
Kubernetes リーダーエレクションに Camel のマスターコンポーネントを使用する方法を示します。 | |
カスタム | |
Camel CXF SOAP コンポーネントの使用方法を示します。 | |
2 つのデータベース間で抽出、変換、ロードする方法を示します。 | |
CSV ファイルを消費する方法、データのマーシャリングおよびアンマーシャリングし、FTP 経由で送信する方法を示します。 | |
Quarkus で Camel ヘルスチェックを使用する方法を示します。 | |
platform-http または RESTEasy を使用して HTTP エンドポイントを作成する方法を示します。 | |
複数のトランザクションリソースにわたる JTA トランザクションをサポートする Camel Quarkus アプリケーションを示します。 | |
メッセージが複数回配信された場合でも、メッセージを 1 回だけ消費する方法を示します。 | |
MySQL とシミュレートされた XAResource 間の JTA トランザクションを示します。 | |
Strimzi Operator を使用して Kafka トピックでメッセージを生成および消費する方法を示します。 | |
Camel アプリケーションで使用するためのシンプルな Kamelet を構築する方法を示します。 | |
接続プールと XA トランザクションを備えた AMQ および IBM MQ クライアントを示します。 | |
メトリクス、ヘルスチェック、分散トレーシングのサポートを追加する方法を示します。 | |
Contract First OpenAPI を使用して実行する方法を示します。 | |
Keycloak を使用してプラットフォーム HTTP を保護する方法を示します。 | |
Camel REST DSL と Jackson を使用して REST サービスを作成する方法を示します。 | |
saga および LRA パターンの使用方法を示します。 | |
Camel タイマーコンポーネントを使用して、Hello world メッセージをコンソールに出力します。 | |
CSV ファイルをトークン化するために XML で Camel ルートを定義する方法を示します。 | |
WebSocket サーバーを設定し、接続されたピアと対話する方法を示します。 |