我相信一个困难是,对于您提到的组件(HTTP、FTP、文件、JMS),您可能希望使用生产者或消费者:
- FTP、文件:绝对是读取文件的消费者。
- HTTP(或HTTP4):肯定是生产者,向服务器发送请求(服务器的回复将由新的消息体)
- JMS:取决于您是要从队列中读取数据(消费者),还是向带有ReplyTo 标头的队列发送消息,然后等待应答(生产者)。
制片人:
如果您使用的是 Camel 2.16+,则可以使用新的 "dynamic to" 语法。它与常规的“to”基本相同,除了端点 uri 可以使用 simple 表达式(或者,可选地,另一种类型的表达式)动态评估。或者,您可以使用 content-enricher pattern 的 enrich 风格,它还支持从 Camel 2.16 开始的动态 uri。
如果您使用的是旧版本的 Camel,或者如果您需要动态路由到多个端点(不仅仅是一个),您可以使用 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");
消费者:
使用 Camel 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 和已知组件路由到两个分支......这是我可能会做的事情(使用 Camel 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");