camel

本文深入探讨了Apache Camel的实战应用与核心概念,包括路由配置、异常处理、消息处理策略、数据转换及整合模式等关键内容。通过具体示例,解析了如from、onException、split、multicast等组件的功能与使用场景,同时提供了对Camel-mybatis、wiretap等特性的介绍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目里会用到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/

  • ComponentTypeWithin same CamelContextWithin same JVM
    DirectSynchronousYesNo
    Direct-VMSynchronousYesYes
    SEDAAsynchronousYesNo
    VMAsynchronousYesYes

 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

https://stackoverflow.com/questions/41482674/what-is-the-difference-bewteen-wiretap-and-multicast-in-apache-camel

 

 

17 routePolicy

在 RoutePolicySupport 抽象类中存在着这些方法:

实现routePolicy 可在route开始,结束等时候执行。在我理解,routepolicy是一个拦截器,在route执行的各个时间被trigger。

 18 Apache Camel的核心概念

参考 http://holbrook.github.io/2014/02/10/apache_camel.html

 

 

 

 





 

转载于:https://www.cnblogs.com/Jenny22/p/9927668.html

中文名: Camel 实战 原名: Camel in Action 作者: Claus Ibsen Jonathan Anstey 资源格式: PDF 版本: 英文文字版/更新源代码 出版社: Manning书号: 9781935182368发行时间: 2010年12月 地区: 美国 语言: 英文 简介: 内容介绍: Apache Camel is a Java framework that lets you implement the standard enterprise integration patterns in a few lines of code. With a concise but sophisticated DSL you snap integration logic into your app, Lego-style, using Java, XML, or Scala. Camel supports over 80 common transports such as HTTP, REST, JMS, and Web Services. Camel in Action is a Camel tutorial full of small examples showing how to work with the integration patterns. It starts with core concepts like sending, receiving, routing, and transforming data. It then shows you the entire lifecycle and goes in depth on how to test, deal with errors, scale, deploy, and even monitor your app—details you can find only in the Camel code itself. Written by the developers of Camel, this book distills their experience and practical insights so that you can tackle integration tasks like a pro. 目录: Part 1 First steps 1 Meeting Camel 2 Routing with Camel Part 2 Core Camel 3 Transforming data with Camel 4 Using beans with Camel 5 Error handling 6 Testing with Camel 7 Understanding components 8 Enterprise integration patterns Part 3 Out in the wild 9 Using transactions 10 Concurrency and scalability 11 Developing Camel projects 12 Management and monitoring 13 Running and deploying Camel 14 Bean routing and remoting appendix A Simple, the expression language appendix B Expressions and predicates appendix C The producer and consumer templates appendix D The Camel community appendix E Akka and Camel
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值