1.2. Spring Boot


Spring Boot は Camel を自動的に設定します。Camel コンテキストのオピニオン自動設定は、Spring コンテキストで使用できる Camel ルートを自動検出し、プロデューサーテンプレート、コンシューマーテンプレート、タイプコンバーターなどの主な Camel ユーティリティーを Bean として登録します。

Maven ユーザーは、このコンポーネントを使用するために pom.xml に次の依存関係を追加する必要があります。

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot</artifactId>
    <version>3.20.1.redhat-00064</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

camel-spring-boot jar には spring.factories ファイルが付属しているため、その依存関係をクラスパスに追加するとすぐに、Spring Boot が自動的に Camel を自動設定します。

1.2.1. Camel Spring Boot スターター

Apache Camel には、スターターを使用して Spring Boot Starter アプリケーションを開発できる、Spring Boot スターターモジュールが含まれています。ソースコードにも サンプルアプリケーション があります。

スターターを使用するには、Spring Boot pom.xml ファイルに以下を追加します。

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-spring-boot-bom</artifactId>
    <version>3.20.1.redhat-00064</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

次に、次のような Camel ルートでクラスを追加するだけです。

package com.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("timer:foo").to("log:bar");
    }
}
Copy to Clipboard Toggle word wrap

その後、これらのルートは自動的に開始されます。

application.properties または application.yml ファイルで Camel アプリケーションをカスタマイズできます。

1.2.2. Spring Boot 自動設定

Spring Boot で spring -boot を使用する場合は、自動設定をサポートするために、次の Maven 依存関係を必ず使用してください。

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-spring-boot-starter</artifactId>
  <version>3.20.1.redhat-00064</version> <!-- use the same version as your Camel core version -->
</dependency>
Copy to Clipboard Toggle word wrap

1.2.3. 自動設定された Camel コンテキスト

Camel 自動設定によって提供される最も重要な機能は、CamelContext インスタンスです。Camel 自動設定は、SpringCamelContext を作成し、そのコンテキストの適切な初期化とシャットダウンを処理します。作成された Camel コンテキストは、Spring アプリケーションコンテキストにも (camelContext Bean 名で) 登録されるため、他の Spring Bean と同じようにアクセスできます。

@Configuration
public class MyAppConfig {

  @Autowired
  CamelContext camelContext;

  @Bean
  MyService myService() {
    return new DefaultMyService(camelContext);
  }

}
Copy to Clipboard Toggle word wrap

1.2.4. Camel ルートの自動検出

Camel auto-configuration は、Spring コンテキストからすべての RouteBuilder インスタンスを収集し、提供された CamelContext に自動的に注入します。これは、Spring Boot スターターを使用して新しい Camel ルートを作成するのは、@Component アノテーション付きクラスをクラスパスに追加するのと同じくらい簡単であることを意味します。

@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("jms:invoices").to("file:/invoices");
  }

}
Copy to Clipboard Toggle word wrap

または、新しいルート RouteBuilder Bean を @Configuration クラスで作成します。

@Configuration
public class MyRouterConfiguration {

  @Bean
  RoutesBuilder myRouter() {
    return new RouteBuilder() {

      @Override
      public void configure() throws Exception {
        from("jms:invoices").to("file:/invoices");
      }

    };
  }

}
Copy to Clipboard Toggle word wrap

1.2.5. Camel プロパティー

Spring Boot の自動設定は、プロパティーのプレースホルダー、OS 環境変数、Camel プロパティーがサポートされるシステムプロパティーなどの Spring Boot 外部設定 に自動的に接続します。これは基本的に、application.properties ファイルで定義されたすべてのプロパティーを意味します。

route.from = jms:invoices
Copy to Clipboard Toggle word wrap

または、システムプロパティーを介して設定します。

java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
Copy to Clipboard Toggle word wrap

Camel ルートのプレースホルダーとして使用できます。

@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("{{route.from}}").to("{{route.to}}");
  }

}
Copy to Clipboard Toggle word wrap

1.2.6. カスタムの Camel コンテキスト設定

Camel 自動設定によって作成された CamelContext Bean でいくつかの操作を実行する場合は、Spring コンテキストに CamelContextConfiguration インスタンスを登録します。

@Configuration
public class MyAppConfig {

  @Bean
  CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
      @Override
      void beforeApplicationStart(CamelContext context) {
        // your custom configuration goes here
      }
    };
  }

}
Copy to Clipboard Toggle word wrap

メソッド beforeApplicationStart は Spring コンテキストが開始される直前に呼び出されるため、このコールバックに渡される CamelContext インスタンスは完全に自動設定されます。CamelContextConfiguration の複数のインスタンスを Spring コンテキストに追加すると、各インスタンスが実行されます。

1.2.7. 自動設定されたコンシューマーおよびプロデューサーのテンプレート

Camel auto configuration によって、事前設定された ConsumerTemplate および ProducerTemplate インスタンスが提供されます。それらを Spring-managed の Bean に簡単に注入できます。

@Component
public class InvoiceProcessor {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private ConsumerTemplate consumerTemplate;

  public void processNextInvoice() {
    Invoice invoice = consumerTemplate.receiveBody("jms:invoices", Invoice.class);
    ...
    producerTemplate.sendBody("netty-http:http://invoicing.com/received/" + invoice.id());
  }

}
Copy to Clipboard Toggle word wrap

デフォルトでは、コンシューマーテンプレートとプロデューサーテンプレートのエンドポイントキャッシュサイズは 1000 に設定されています。これらの値は、次の Spring プロパティーを変更することで変更できます。

camel.springboot.consumer-template-cache-size = 100
camel.springboot.producer-template-cache-size = 200
Copy to Clipboard Toggle word wrap

1.2.8. 自動設定された TypeConverter

Camel auto configuration では、Spring コンテキストの typeConverter という名前の TypeConverter インスタンスが登録されます。

@Component
public class InvoiceProcessor {

  @Autowired
  private TypeConverter typeConverter;

  public long parseInvoiceValue(Invoice invoice) {
    String invoiceValue = invoice.grossValue();
    return typeConverter.convertTo(Long.class, invoiceValue);
  }

}
Copy to Clipboard Toggle word wrap

1.2.8.1. Spring タイプコンバージョン API ブリッジ

Spring には、強力な 型変換 API が付属しています。Spring API は Camel の 型コンバーター API と似ています。両方の API が非常に似ているため、Camel Spring Boot は、Spring 変換 API に委譲するブリッジコンバーター (SpringTypeConverter) を自動的に登録します。これは、すぐに使用できる Camel が Spring コンバーターを Camel コンバーターのように扱うことを意味します。このアプローチでは、Camel TypeConverter API 経由でアクセスする Camel コンバーターと Spring コンバーターの両方を使用できます。

@Component
public class InvoiceProcessor {

  @Autowired
  private TypeConverter typeConverter;

  public UUID parseInvoiceId(Invoice invoice) {
    // Using Spring's StringToUUIDConverter
    UUID id = invoice.typeConverter.convertTo(UUID.class, invoice.getId());
  }

}
Copy to Clipboard Toggle word wrap

Camel Spring Boot は、アプリケーションコンテキストで利用可能な Spring の ConversionService インスタンスに変換を委ねます。ConversionService インスタンスが利用できない場合は、Camel Spring Boot 自動設定によってインスタンスが作成されます。

1.2.9. アプリケーションの存続

この機能が有効になっている Camel アプリケーションは、JVM の終了を防止してアプリケーションを存続させることのみを目的として、起動時に新しいスレッドを起動します。つまり、Spring Boot で Camel アプリケーションを開始した後、アプリケーションは Ctrl+C シグナルを待機し、すぐには終了しません。

コントローラースレッドは、camel.springboot.main-run-controllertrue に設定してアクティブ化できます。

camel.springboot.main-run-controller = true
Copy to Clipboard Toggle word wrap

Web モジュールを使用するアプリケーション (例えば org.springframework.boot:spring-boot-web-starter モジュールをインポートするアプリケーション) は、他の非デーモンスレッドの存在によってアプリケーションが維持されるため、通常この機能を使用する必要はありません。

1.2.10. XML ルートの追加

デフォルトでは、Camel XML ルートをディレクトリー camel の下のクラスパスに配置できます。これは、camel-spring-boot が自動検出して含めます。ディレクトリー名を設定するか、設定オプションを使用してこれをオフにすることができます。

# turn off
camel.springboot.routes-include-pattern = false
Copy to Clipboard Toggle word wrap
# scan only in the com/foo/routes classpath
camel.springboot.routes-include-pattern = classpath:com/foo/routes/*.xml
Copy to Clipboard Toggle word wrap

XML ファイルは、次のような Camel XML ルート (<CamelContext>ではない) である必要があります。

<routes xmlns="http://camel.apache.org/schema/spring">
    <route id="test">
        <from uri="timer://trigger"/>
        <transform>
            <simple>ref:myBean</simple>
        </transform>
        <to uri="log:out"/>
    </route>
</routes>
Copy to Clipboard Toggle word wrap

1.2.11. JUnit 5 の方法をテストする

テストのために、Maven ユーザーは次の依存関係を pom.xml に追加する必要があります。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.7.12</version> <!-- Use the same version as your Spring Boot version -->
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-test-spring-junit5</artifactId>
    <version>3.20.1.redhat-00031</version> <!-- use the same version as your Camel core version -->
    <scope>test</scope>
</dependency>
Copy to Clipboard Toggle word wrap

Camel Spring Boot アプリケーションをテストするには、テストクラスに @CamelSpringBootTest のアノテーションを付けます。これにより、Camel の Spring Test サポートがアプリケーションにもたらされるため、Spring Boot テスト規則 を使用してテストを作成できます。

CamelContext または ProducerTemplate を取得するには、@Autowired を使用して、通常の Spring の方法でそれらをクラスに注入できます。

camel-test-spring-junit5 を使用して、テストを宣言的に設定することもできます。この例では、@MockEndpoints アノテーションを使用して、エンドポイントを自動モック化します。

@CamelSpringBootTest
@SpringBootApplication
@MockEndpoints("direct:end")
public class MyApplicationTest {

    @Autowired
    private ProducerTemplate template;

    @EndpointInject("mock:direct:end")
    private MockEndpoint mock;

    @Test
    public void testReceive() throws Exception {
        mock.expectedBodiesReceived("Hello");
        template.sendBody("direct:start", "Hello");
        mock.assertIsSatisfied();
    }

}
Copy to Clipboard Toggle word wrap
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat