My first ride apache camel

本文介绍如何使用Apache Camel创建FTP路由,并详细解释了如何在FTP端点中捕获错误,如无效登录、服务器下线等。通过设置抛出异常和自定义消费者策略,实现路由在遇到错误时仅消费一次。

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

These days I have create my first route of apache camel for a real enterprise bizz.

Just as what 《Camel in Action》 says: "Camel is an integration framework that aims to make your integration projects productive and fun" , It`s awsome that the Java DSL looks so directly and the Spring DSL is so powerful that you can just config the Spring xml to route for your own without java compiling.

 

The route as follows:

 

<route id="lz" autoStartup="false" trace="true">
	   	   <from uri="ftp://lori@127.0.0.1:21?password=123&amp;filter=#lzFtpFileNameFilter&amp;maximumReconnectAttempts=1&amp;reconnectDelay=3000&amp;disconnect=true&amp;pollStrategy=#lzPollingConsumerPollStrategy&amp;throwExceptionOnConnectFailed=true"/>
	   	   <camel:onCompletion>
	   	       <process ref="stopLZRouteProcessor"/>
	   	   </camel:onCompletion>
	   	   
	   	   <camel:log loggingLevel="INFO" message="Picked up ${file:name}"/>
	   	   <process ref="zipFileSplittingProcessor"/>
	   	   <split>
	   	   	   <simple>${body}</simple>
	   	   	   <unmarshal><csv /></unmarshal>
	   	   	   <bean ref="lzServiceBean" method="csv2MapList"/>
	   	   	   <split>
	   	   	   		<simple>${body}</simple>
	   	   	   		<camel:setHeader headerName="Subject"><camel:constant>Test subject</camel:constant></camel:setHeader>
	   	   	   		<camel:setHeader headerName="From"><camel:constant>Lori</camel:constant></camel:setHeader>
	   	   	   		<camel:setHeader headerName="contentType"><camel:constant>text/html;charset=UTF-8</camel:constant></camel:setHeader>
	   	   	   		<camel:setHeader headerName="to"><camel:simple>${body[dest]}</camel:simple></camel:setHeader>
	   	   	   		<to uri="velocity://template/test.vm?contentCache=true&amp;encoding=utf-8"/>
	   	   	   		<to uri="smtp://mail.elitecrm.com?username=loriling@elitecrm.com&amp;password=0308"/>
	   	   	   </split>
	   	   </split>
	   </route>
 

 

With this route, the bigest problem is that I want to catch errors in the FTP endpoint (invalid login, server down, etc). I have tried errorHandler() but it only catches the errors produced after the message enters the exchange i.e. after from().

 

i try to set the FTP endpoint properties:

throwExceptionOnConnectFailed=true and pollStrategy=#lzPollingConsumerPollStrategy

then the FTPConsumer will  thrown an exception if a successful connection and login could not be establish and using the custom PollingConsumerPollStrategy which coded:

consumer.stop();

 in the rollback method.

Then the problem solved, it will only consume once if there is an exception of which FTP endpoint throws.

 

There are many features and EIPs that I havenot deal with and much more things i need to learn.

Last i extend my heartfelt gratitude to each camel authors and committers for bringing us so pretty project. Thanks again.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值