Red Hat Training
A Red Hat training course is available for Red Hat Fuse
第11章 Bean
Bean コンポーネント
bean: コンポーネントは Bean を Apache Camel メッセージエクスチェンジにバインドします。
URI 形式
bean:beanID[?options]
beanID には、レジストリーで Bean を検索するために使用される任意の文字列を指定できます。
オプション
名前 | タイプ | デフォルト | 説明 |
---|---|---|---|
メソッド
|
文字列
|
null
|
呼び出される Bean のメソッド名。指定しない場合、Camel はメソッド自体を判断しようとします。あいまいな場合は、例外が発生します。詳細は、Bean Binding を参照してください。Camel 2.8 以降 では、タイプ修飾子を指定して、オーバーロードされたメソッドに使用する正確なメソッドを特定することができます。Camel 2.9 以降では、メソッド構文でパラメーター値を直接指定できます。 |
cache
|
boolean
|
false
|
有効にすると、Apache Camel は最初の レジストリー ルックアップの結果をキャッシュします。レジストリー の Bean がシングルトンスコープとして定義されている場合、キャッシュを有効にできます。 |
multiParameterArray
|
boolean
|
false
|
メッセージボディーから渡されるパラメーターを処理する方法。true の場合、In メッセージボディーはパラメーターの配列である必要があります。
|
bean.xxx
|
null
|
Camel 2.17: クラス名から create bean インスタンスで追加オプションを設定します。たとえば、Bean で
foo オプションを設定するには、bean.foo=123 を使用します。
|
URI にクエリーオプションは
?option=value&option=value&.. の形式で追加できます。
使用
メッセージの消費に使用されるオブジェクトインスタンスは、レジストリー に明示的に登録する必要があり ます。たとえば、Spring を使用している場合は、Spring 設定
spring.xml
で Bean を定義する必要があり、Spring を使用しない場合は Bean を JNDI に配置します。
// lets populate the context with the services we need // note that we could just use a spring.xml file to avoid this step JndiContext context = new JndiContext(); context.bind("bye", new SayService("Good Bye!")); CamelContext camelContext = new DefaultCamelContext(context);
エンドポイントが登録されたら、エクスチェンジの処理に使用するルートを構築できます。
// lets add simple route camelContext.addRoutes(new RouteBuilder() { public void configure() { from("direct:hello").to("bean:bye"); } });
bean: エンドポイントは、ルートへの入力として定義できません。つまり、消費できません。一部のインバウンドメッセージエンドポイントから Bean エンドポイントに出力としてのみルーティングできます。http://camel.apache.org/endpoint.htmlしたがって、direct: または queue: エンドポイントを入力として使用することを検討してください。
ProxyHelper で
createProxy()
メソッドを使用して、BeanExchange を生成して任意のエンドポイントに送信するプロキシーを作成できます。
Endpoint endpoint = camelContext.getEndpoint("direct:hello"); ISay proxy = ProxyHelper.createProxy(endpoint, ISay.class); String rc = proxy.say(); assertEquals("Good Bye!", rc);
Spring DSL を使用した同じルートの場合:
<route> <from uri="direct:hello"> <to uri="bean:bye"/> </route>
エンドポイントとしての Bean
Apache Camel は、エンドポイントとしての Bean の呼び出しもサポートします。ルートは以下のとおりです。
<camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <to uri="myBean"/> <to uri="mock:results"/> </route> </camelContext> <bean id="myBean" class="org.apache.camel.spring.bind.ExampleBean"/>
エクスチェンジが
myBean
にルーティングされると、Apache Camel は Bean バインディングを使用し て Bean を呼び出します。Bean のソースはプレーン POJO です。
public class ExampleBean { public String sayHello(String name) { return "Hello " + name + "!"; } }
Apache Camel は Bean バインディング を使用して
sayHello
メソッドを呼び出します。これには、エクスチェンジの In ボディーを String
型に変換し、メソッドの出力を Exchange Out ボディーに保存します。
Java DSL Bean 構文
Java DSL には、Bean コンポーネントのシンタックス(sugtactic sugar)が同梱されています。Bean を明示的にエンドポイント (つまり
to ("bean:beanName")
) として指定する代わりに、次の構文を使用できます。
// Send message to the bean endpoint // and invoke method resolved using Bean Binding. from("direct:start").beanRef("beanName"); // Send message to the bean endpoint // and invoke given method. from("direct:start").beanRef("beanName", "methodName");
Bean への参照の名前を渡す代わりに (Camel がレジストリーでそれを検索できるようにするため)、Bean 自体を指定できます。
// Send message to the given bean instance. from("direct:start").bean(new ExampleBean()); // Explicit selection of bean method to be invoked. from("direct:start").bean(new ExampleBean(), "methodName"); // Camel will create the instance of bean and cache it for you. from("direct:start").bean(ExampleBean.class);
Bean バインディング
呼び出される Bean メソッド の選択方法(method パラメーターで明示的に指定されていない場合)と、メッセージ からパラメーター値がどのように構築されるかは、Apache Camel のさまざまな Bean 統合メカニズム全体で使用される Bean バインディング メカニズムによってすべて定義されます。
- クラス コンポーネント
Bean 言語
Bean 言語の目的は、Bean で単純なメソッドを使用して式または述語を実装することです。Spring
詳細は Bean 言語 の章を参照してください。
ApplicationContext
などのレジストリーで Bean 名を指定すると、メソッドを呼び出して Expression または Predicate を評価します。ただし、メソッド名を指定しない場合は、以下を使用して選択できます。
- Bean バインディングのルール
- メッセージボディーのタイプ
- Bean メソッドのアノテーション
注記
Camel 2.17 以降では、Bean 言語は静的メソッドを純粋な静的クラスで呼び出すことができます。Bean 言語が OGNL メソッドチェーンからメソッドを呼び出す場合、メソッドは null 値を返します。ただし、を提供する追加のメソッド呼び出しを防ぐことができます。
NullPointerException.