Red Hat Training
A Red Hat training course is available for Red Hat Fuse
250.27. カスタム関数の使用
Camel 2.14.1 から利用可能
Properties コンポーネントを使用すると、プロパティープレースホルダーの解析中に使用できるサードパーティーの機能をプラグインできます。これらの関数は、データベースの検索、カスタム計算の実行、または何ではないかなど、プレースホルダーを解決するためのカスタムロジックを実行できます。関数の名前はプレースホルダーで使用される接頭辞になります。これは、以下のコード例で最も適しています。
<bean id="beerFunction" class="MyBeerFunction"/> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <propertyPlaceholder id="properties"> <propertiesFunction ref="beerFunction"/> </propertyPlaceholder> <route> <from uri="direct:start"/> <to uri="{`{beer:FOO}`}"/> <to uri="{`{beer:BAR}`}"/> </route> </camelContext>
camel 2.19.0 からは、場所属性(propertyPlaceholder タグ)がより必須ではありません。
ここでは、Bean ID を使用するよう < ;propertyPlaceholder&
gt; を定義した Camel XML ルートがあります(例: beerFunction
)。アクセラー関数は「beer 」
を名前として使用するため、プレースホルダー構文は beer:value
で始まるビール機能をトリガーできます。
関数の実装は、以下のように 2 つのメソッドのみになります。
public static final class MyBeerFunction implements PropertiesFunction { @Override public String getName() { return "beer"; } @Override public String apply(String remainder) { return "mock:" + remainder.toLowerCase(); } }
この関数は org.apache.camel.component.properties.PropertiesFunction
インターフェースを実装する必要があります。getName
メソッドは関数の名前です(例: beer)。apply
メソッドは、カスタムロジックを実装する場所です。サンプルコードはユニットテストからのものであるため、モックエンドポイントを参照する値のみを返します。
Java コードからカスタム関数を登録するには、以下のようにします。
PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class); pc.addFunction(new MyBeerFunction());