最近项目里会用到camel,近期会写一篇文章来总结camel中的一些常用的‘知识点’。
总结一下。
1 from
from("file://C:\Users\xxx\documents\camel?fileName=b.txt").routeId("routeId")
.log(LoggingLevel.DEBUG,"lalala ${id} ${body}")
.inOnly("direct:BBB")
.log(LoggingLevel.DEBUG,"--------"+"${body}"+"processing ${id}");
一直会监听这个路径下的b.txt文件。
2 camel 设置打印log
3 bean processor
类似拦截器,对exchange进行一些处理
如果在bean,processor中有调用exchange.get()方法,读取exchange中的流,之后需要重新set进去才行,否则,之后exchange中的内容为null。
做了一个简单的demo,就了解到这些基本的方法作用,其他的再慢慢探索,还是很枯燥的。
4 onException
捕捉camel route中产生的异常
http://people.apache.org/~dkulp/camel/exception-clause.html
网页中有比较详细的例子可参考。
5 streamCaching
stream缓存,因为stream类型的数据只能读一次。from Camel 2.0 缓存默认不可用。
6 marsh unmarsh
marsh bean转xml
unmarsh xml转bean
7 setExchangePattern(待完成)
可参考 https://examples.javacodegeeks.com/enterprise-java/apache-camel/apache-camel-exchange-example/
http://www.catchmycity.com/tutorial/apache-camel-difference-between-exchangepattern-inout-and-exchangepattern-inonly-in-apache-camel_101
InOnly
InOut request-reply
Not all camel endpoints support INOUT. The JMS endpoint supports it but the file endpoint does not.
Unfortunately this is indeed not well documented.
8 setProperty
setProperty在route中传递, setHeader在exchange中传递。
Similar to message headers, but they last for the duration of the entire exchange.
可参考 http://camel.465427.n5.nabble.com/Camel-Java-DSL-setProperty-behaviour-td5718226.html
9 choice
.choice() .when().simple("${body} == 'test inout'").setBody(simple("one")).endChoice() .when().simple("${body} == 'aaa'").setBody(simple("two")).endChoice() .end()
可参考 https://cleverbuilder.com/articles/camel-choice-when/
10 doTry() doCatch(xx.class)
处理异常,类似java try catch
11 multicast() split()
multicast
The Multicast allows to route the same message to a number of endpoints and process them in a different way. The main difference between the Multicast and Splitter is that Splitter will split the message into several pieces but the Multicast will not modify the request message.
可参考 http://people.apache.org/~dkulp/camel/multicast.html
split
The Splitter from the EIP Pattern allows you split a message into a number of pieces and process them individually
可参考 http://camel.apache.org/splitter.html
12 SEDA indirect vm
可参考 http://camel.apache.org/how-do-the-direct-event-seda-and-vm-endpoints-compare.html
https://cleverbuilder.com/articles/camel-direct-vm-seda/
-
Component Type Within same CamelContext Within same JVM Direct Synchronous Yes No Direct-VM Synchronous Yes Yes SEDA Asynchronous Yes No VM Asynchronous Yes Yes
13 recipientList
使用recipientList可以创建动态的接收者。
example
from("direct:a").recipientList(header("myHeader").tokenize(","));
myHeader = {'direct:a','direct:b'}
14 camel 打印exchange信息到控制台
解决 DEBUG ProducerCache - >>>> Endpoint[direct:start] Exchange[Message: 01234567890123456789... [Body clipped after 20 chars, total length is 1000]]
参考 http://camel.apache.org/how-do-i-set-the-max-chars-when-debug-logging-messages-in-camel.html 设置camelContext信息输出长度。
endpoint.getxxx 参考下图输出body信息。
15 camel - mybatis
参考 https://camel.apache.org/mybatis-example.html
16 wire tap
参考 https://www.javarticles.com/2015/05/apache-camel-wire-tap-examples.html
17 routePolicy
在 RoutePolicySupport 抽象类中存在着这些方法:
实现routePolicy 可在route开始,结束等时候执行。在我理解,routepolicy是一个拦截器,在route执行的各个时间被trigger。
18 Apache Camel的核心概念
参考 http://holbrook.github.io/2014/02/10/apache_camel.html