検索

第17章 Groovy

download PDF

概要

Groovy は、オブジェクトを素早く解析できる Java ベースのスクリプト言語です。Groovy のサポートは camel-groovy モジュールに含まれます。

スクリプトモジュールの追加

ルートで Groovy を使用するには、例17.1「camel-groovy 依存関係の追加」 で示したたように、camel-groovy の依存関係をプロジェクトに追加する必要があります。

例17.1 camel-groovy 依存関係の追加

<!-- Maven POM File -->
<properties>
  <camel-version>2.23.2.fuse-7_13_0-00013-redhat-00001</camel-version>
  ...
</properties>

<dependencies>
  ...
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-groovy</artifactId>
    <version>${camel-version}</version>
  </dependency>
</dependencies>

静的インポート

アプリケーションコードで groovy() static メソッドを使用するには、以下の import ステートメントを Java ソースファイルに追加します。

import static org.apache.camel.builder.script.ScriptBuilder.*;

組み込み属性

表17.1「Groovy 属性」 に、Groovy を使用する際にアクセス可能な組み込み属性の一覧を示します。

表17.1 Groovy 属性
属性タイプ

context

org.apache.camel.CamelContext

Camel コンテキスト

exchange

org.apache.camel.Exchange

現在のエクスチェンジ

request

org.apache.camel.Message

IN メッセージ

response

org.apache.camel.Message

OUT メッセージ

properties

org.apache.camel.builder.script.PropertiesFunction

スクリプト内でプロパティーコンポーネントを簡単に使用できるようにする resolve メソッドを使用した関数。

属性はすべて ENGINE_SCOPE に設定されます。

例17.2「Groovy を使用したルート」 は、Groovy スクリプトを使用する 2 つのルートを示しています。

例17.2 Groovy を使用したルート

<camelContext>
  <route>
    <from uri="direct:items" />
    <filter>
      <language language="groovy">request.lineItems.any { i -> i.value > 100 }</language>
      <to uri="mock:mock1" />
    </filter>
  </route>
  <route>
    <from uri="direct:in"/>
    <setHeader headerName="firstName">
      <language language="groovy">$user.firstName $user.lastName</language>
    </setHeader>
    <to uri="seda:users"/>
  </route>
</camelContext>

プロパティーコンポーネントの使用

プロパティーコンポーネントからプロパティー値にアクセスするには、以下のように組み込み properties 属性で resolve メソッドを呼び出します。

.setHeader("myHeader").groovy("properties.resolve(PropKey)")

PropKey は、解決するプロパティーのキーで、キーの値は String タイプになります。

プロパティーコンポーネントの詳細は、Apache Camel コンポーネントリファレンスガイド の プロパティー を参照してくださいhttps://access.redhat.com/documentation/ja-jp/red_hat_fuse/7.13/html-single/apache_camel_component_reference/index#properties-component

Groovy Shell のカスタマイズ

カスタムの GroovyShell インスタンスを Groovy 式で使用する必要がある場合があります。カスタム GroovyShell を指定するには、org.apache.camel.language.Groovy.GroovyShellFactory SPI インターフェイスの実装を Camel レジストリーに追加します。

たとえば、以下の Bean を Spring コンテキストに追加することで、Apache Camel はデフォルトの GroovyShell インスタンスではなく、カスタムの静的インポートを含む GroovyShell インスタンスを使用します。

public class CustomGroovyShellFactory implements GroovyShellFactory {

  public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("com.example.Utils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    return new GroovyShell(configuration);
  }
 }
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

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

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

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

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

会社概要

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

© 2024 Red Hat, Inc.