如何使Camel在分割器组件之后不回滚更改

时间:2012-09-26 05:42:14

标签: java file apache-camel splitter

我需要按行解析文件。每行使用分离器组件分开处理。处理完所有行后,我需要将文件复制到done_folder。如果正确处理所有行,则一切正常。但是如果有不正确的行,那么我会收到有关回滚的以下警告,并且文件不会复制到done_folder 警告:

WARN (Camel (com.company.realcardparser) thread #0 - file://project/src/test/resources/working_folder) [GenericFileOnCompletion] Rollback file strategy: org.apache.camel.component.file.strategy.GenericFileDeleteProcessStrategy@41a7d9e7 for file: GenericFile[237file09062012-qa.csv]

我的骆驼配置:

 <camelContext id="com.company.realcardparser" xmlns="http://camel.apache.org/schema/spring" trace="true">
        <routeContextRef ref="idtProxyRoute"/>
        <endpoint id="fileParserInputEndPoint" uri="file:${idt.proxy.real.card.parser.folder.test.input}?delete=true&amp;readLock=${idt.proxy.real.card.parser.readLock}&amp;readLockCheckInterval=${idt.proxy.real.card.parser.readLockCheckInterval}&amp;readLockTimeout=${idt.proxy.real.card.parser.readLockTimeout}&amp;delay=${idt.proxy.real.card.parser.delay}"/>
        <endpoint id="fileParserOutputEndPoint" uri="file:${idt.proxy.real.card.parser.folder.test.output}"/>
        <endpoint id="fileParserOutputFailedEndPoint" uri="file:${idt.proxy.real.card.parser.folder.test.output.failed}"/>
    </camelContext>
    <bean id="idtTxRequired" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
        <property name="transactionManager" ref="transactionManager"/>
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
    </bean>

    <routeContext id="idtProxyRoute" xmlns="http://camel.apache.org/schema/spring">
        <route id="idtRealCardParserRoute">
            <from ref="fileParserInputEndPoint"/>
            <transacted ref="idtTxRequired"/>
            <split>
                <method bean="realCardParser" method="handle"/>
                <to uri="bean:realCardFinalizer"/>
            </split>
            <to ref="fileParserOutputEndPoint"/>
        </route>
    </routeContext>

如何让骆驼忽略异常?我尝试用try / catch块包围分割器,但它没有帮助。

2 个答案:

答案 0 :(得分:3)

克劳斯易卜生的回答指引我走上了正确的道路。但是,我花了一点时间才弄清楚如何去做。

onException(Exception.class)
        .process(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            // place to add logic to handle exception
            Throwable caught = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, 
                    Throwable.class);
            logger.error("FATAL ERROR - ", caught);
        }
    })
    .handled(true); // if I don't give handled(true), it will keep reprocessing the file again and again.


    from("file:" + pathToFile + "?noop=true")
    // rest of the route

http://camel.apache.org/exception-clause.html - 解释更多错误处理方法。

答案 1 :(得分:2)

您需要将try .. catch块放在拆分器中。我假设异常发生在分割器内部执行的逻辑中。

另一种方法是使用onException并处理异常。