私は1つの難易度はあなたが言及コンポーネント(HTTP、FTP、ファイル、JMS)のために、あなたがプロデューサまたはコンシューマのいずれかを使用することをお勧めします、ということになると信じて:
- FTP 、File:間違いなくファイルを読む消費者。
- 間違いなく、プロデューサーがサーバーに要求を送信します(サーバーの返信は新しいメッセージ本文で行われます)
- JMS:キュー(消費者)から読み込みたいReplyToヘッダー付きのキューにメッセージを送信してから、応答(プロデューサ)を待ちます。
プロデューサー:
あなたが2.16+キャメルを使用している場合は、新しい"dynamic to"構文を使用することができます。エンドポイントuriがsimple式(またはoptionnaly、別のタイプの式)を使用して動的に評価できる点を除いて、基本的には "to"と同じです。また、のenrich
フレーバーを使用できます。また、Camel 2.16以降の動的なウイスもサポートしています。
旧バージョンのCamelを使用している場合、または複数のエンドポイント(1つではない)に動的にルーティングする必要がある場合は、recipient listパターンを使用できます。
ここに例があります。エンドポイントを呼び出すことによってメッセージ本文を変換します。そのエンドポイントのURIはTargetUri
というヘッダーにあり、メッセージごとに動的に評価されます。
// An instance of this class is registered as 'testbean' in the registry. Instead of
// sending to this bean, I could send to a FTP or HTTP endpoint, or whatever.
public class TestBean {
public String toUpperCase(final String str) {
return str.toUpperCase();
}
}
// This route sends a message to our example route for testing purpose. Of course, we
// could send any message as long as the 'TargetUri' header contains a valid endpoint uri
from("file:inbox?move=done&moveFailed=failed")
.setHeader("TargetUri").constant("bean:testbean?method=toUpperCase")
.setBody().constant("foo")
.to("direct:test");
// 1. The toD example :
from("direct:test")
.toD("${header.TargetUri}")
.to("log:myRoute");
// 2. The recipient list example :
from("direct:test")
.recipientList(header("TargetUri"))
.to("log:myRoute");
// 3. The enrich example :
from("direct:test")
.enrich().simple("${header.TargetUri}") // add an AggregationStrategy if necessary
.to("log:myRoute");
消費者:
キャメル2.16+で、あなたはcontent-enricher patternのpollEnrich
風味を使用することができます。
旧バージョンのCamelでは、プロセッサーにConsumerTemplate
を使用できます。
// 4. The pollEnrich example (assuming the TargetUri header contains, e.g., a file
// or ftp uri) :
from("direct:test")
.pollEnrich().simple("${header.TargetUri}") // add an AggregationStrategy if necessary
.to("log:myRoute");
// 5. The ConsumerTemplate example (same assumption as above)
from("direct:test")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String uri = exchange.getIn().getHeader("TargetUri", String.class);
ConsumerTemplate consumer = exchange.getContext().createConsumerTemplate();
final Object data = consumer.receiveBody(uri);
exchange.getIn().setBody(data);
}
})
.to("log:myRoute");
プロデューサまたはコンシューマ?
悲しいことに、私はどちらも本当に優雅な解決策を考えることはできません - 私はあなたがuriと既知のコンポーネントに基づいて2つの支店にルーティングする必要があると思う...ここでは、キャメル2.16+)と、それは非常にきれいではありません。
// This example only handles http and ftp endpoints properly
from("direct:test")
.choice()
.when(header("TargetUri").startsWith("http"))
.enrich().simple("${header.TargetUri}")
.endChoice()
.when(header("TargetUri").startsWith("ftp"))
.pollEnrich().simple("${header.TargetUri}")
.endChoice()
.end()
.to("log:myRoute");
あなたが最も可能性の高い新しい[「への動的な」]を見てしたい(http://camel.apache.org/message-endpoint.html )構文(Camel 2.16以降を使用している場合)または[受信者リスト](http://camel.apache.org/recipient-list.html)のパターンで指定します。どちらも、ウリの動的評価を可能にします。 –