[英]How can I loop in Apache Camel
<setHeader headerName="smsRecivers"><simple>{{reciversList}}</simple></setHeader>
I have a list mobile numbers(in comma separated form reciversList=999999999,88888888,799999999 ) and I have to send each a sms, looping through reciversList list 我有一个手机号码列表(以逗号分隔的形式reciversList = 999999999,88888888,799999999),我必须发送每个短信,循环遍历reciversList列表
some thing like 就像是
<loop on="reciversList">
// so some work
</loop>
I looked in to loop function it have a constant number. 我看了看循环功能,它有一个常数。
If you want to split on a header value rather than the body of a message, you can use the Camel Splitter EIP and write your own method to handle the split. 如果要拆分标题值而不是消息正文,则可以使用Camel Splitter EIP并编写自己的方法来处理拆分。
Your route will look something like this: 您的路线如下所示:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start"/>
<split>
<method ref="splitterBean" method="split"/>
Process each SMS here
</split>
</route>
</camelContext>
And you can then use the MySplitterBean
example in the Apache Splitter page, and write a method like so: 然后,您可以在Apache Splitter页面中使用MySplitterBean
示例,并编写如下方法:
public List<Message> split(@Header(value = "smsReceivers") String header, @Body String body) {
List<Message> answer = new ArrayList<Message>();
// Perform header null checking here
header = header.substring(header.indexOf("=")+1); // Remove var name
String[] parts = header.split(",");
for (String part : parts) {
DefaultMessage message = new DefaultMessage();
message.setHeader("smsReceiver", part);
message.setBody(body);
answer.add(message);
}
return answer;
}
Within the loop, you can then simply access the SMS number through the "smsReceiver" header. 然后,您可以在循环中通过“ smsReceiver”标头简单地访问SMS号码。
You can use loop : http://camel.apache.org/loop.html 您可以使用loop : http : //camel.apache.org/loop.html
<route>
<setHeader headerName="smsRecivers">
<simple>{{reciversList}}</simple>
</setHeader>
<loop>
<simple>${in.header.smsRecivers.size}</simple>
.....
</loop>
</route>
Inside loop body you can get list's item by index, using excange property CamelLoopIndex or you can use custom increment index ( which can calculate in other header ). 在循环体内,您可以使用excange属性CamelLoopIndex按索引获取列表的项目,也可以使用自定义增量索引(可以在其他标头中计算)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.