使用Apache Camel事务时如何回滚文件写入?

时间:2016-11-08 09:32:59

标签: java transactions apache-camel rollback file-writing

我在Camel中使用具有两个管道的多播路由。 一个管道将数据添加到数据库中,另一个管道对文件执行一些写操作。 我要求在发生故障或任何错误时回滚整个过程。 我已成功回滚数据库插入,但无法找到任何方法来回滚对文件执行的写入操作。 任何人都可以帮我解决回滚过程。 这是我的路线背景:

    <routeContext id="s1Route" xmlns="http://camel.apache.org/schema/spring">
    <route id="sRoute">
        <from uri="activemq:queue:{{s.queue}}" />
        <log message="Message received from myprocess queue is ${body}"></log>
        <unmarshal ref="gsonUnmarshelling"></unmarshal>
        <bean beanType="com.***.upload.***.GetMyBean"
            method="process(com.**.upload.beans.MyEvenets,${exchange})" />
        <log message="Multicasting  data ${body} to file system and database" />
        <multicast parallelProcessing="true">
            <pipeline>
                <choice>
                    <when>
                        <simple>${properties:s.write.file} == true</simple>
                        <setHeader headerName="path">
                            <simple>${properties:s.write.folder}</simple>
                        </setHeader>
                        <log message="Going to write to file : ${body}"></log>
                        <bean beanType="com.***.upload.processors.ToFile"
                            method="process(${exchange},com.***.upload.beans.MyFile)" />
                        <to uri="file://?fileExist=Append"></to>
                    </when>
                </choice>
            </pipeline>
            <pipeline>
                <log message="Going to insert in database"></log>
                <transform>
                    <method ref="insertBean" method="MyBatchInsertion"></method>
                </transform>
                <choice>
                    <when>
                        <simple>${in.header.myCount} == ${properties:batch.size}</simple>
                        <to uri="sql:{{sql.my.insertBatch}}?batch=true"></to>
                        <log message="Inserted rows ${body}"></log>
                    </when>
                </choice>
            </pipeline>
        </multicast>
    </route>
</routeContext>

1 个答案:

答案 0 :(得分:1)

Apache Commons Transaction可以处理任何文件系统上的事务读写操作。

您可以设置处理器com.***.upload.processors.ToFileprocess()方式以事务方式处理读写操作。

此SO Apache Transaction:write file transactionally - how to use resourceId可以帮助您在代码中集成常见事务。