Camel小例子

最近在看一些Camel的东西,在研究它的时候可以感觉到网上的例子很少。下面我总结了一下官方的几个小例子提出来大家一起讨论讨论。
POJO Messaging Example
说明:这个例子展示了如果你不想学习camel的高级DSL语言,也可以完成同样的功能。因为camel提供了一系列的注解让我们轻松的produce,consume或是route 消息toendpoints。

例子讲解:
通过下面这幅图来说明整个例子的流程:


第一步:SendFileRecordsToQueueBean从./src/data directory目录下取得新文件,目录下有三个文件因此将产生三个消息。如下面的代码所示,the @Consume annotation注解将把任何从./src/data directory目录下新来的文件发送到onFileSendToQueue method。
public class SendFileRecordsToQueueBean {
@Produce(uri = "activemq:personnel.records")
ProducerTemplate producer;

@Consume(uri = "file:src/data?noop=true")
public void onFileSendToQueue(String body) {
producer.sendBody(body);
}
}

第二步:the SendFileRecordsToQueueBean文件形式的内容以字符串的形式发送的personnel.records JMS queue的队列里面去。(这些工作是通过后台的嵌入模式的activemq服务器实例完成的)。字符串到jms message的转换是自动完成的。The @Produce annotation注解用于访问activemq endpoint。

第三步:the DistributeRecordsBean(如下代码所示)从the personnel.records queue取得消息。the @Consume annotation注解再一次被应用,被用于从activemq
Endpoint取得消息。
public class DistributeRecordsBean {
@Consume(uri = "activemq:personnel.records")
@RecipientList
public String[] route(@XPath("/person/city/text()") String city) {
if (city.equals("London")) {
return new String[] {"file:target/messages/emea/hr_pickup",
"file:target/messages/emea/finance_pickup"};
} else {
return new String[] {"file:target/messages/amer/hr_pickup",
"file:target/messages/amer/finance_pickup"};
}
}
}
我们在上面的代码中可以看到@RecipientList annotation这个注解。这个注解使被注解的方法称为一个Recipient List EIPc(即类似一个企业信息集成门户),在这个门户里返回一系列值给the recipients(返回的值可以是String[], List<String>, URI[], etc)。这个注解对于创建自定以的动态接收列表是很有用的。在这个例子的第4步中,我们选择city这个属性(通过@XPath)并且提供一系列的接收者。
例如folk来自London,他们的files将被发送到emea region地区。(file:target/messages/emea/...).其他的被放到the AMER region (file:target/messages/amer/...).
If you have messages that are not XML, don't fret! Camel has ways to get information out of arbitrary message payloads. For instance, you can try using the @Bean annotation to peek at the message using your own Java bean.
@Consume(uri = "activemq:personnel.records")
@RecipientList
public String[] route(@Bean("cityExtractorBean") String city) {
if (city.equals("London")) {

Check out Parameter Binding Annotations for more information on this.
最后:After running the example, browse to the target/messages directory to see where the messages were saved.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值