如何在处理完所有文件后调用bean?

时间:2014-07-03 09:27:21

标签: apache-camel

我写了以下路线,并期望豆子“teaserService”'在处理所有文件时应该只调用一次,但是...在处理完每个文件后调用它:

        <route id="teaserInterface">
        <from
            uri="file://{{teaser.dropInDir}}?readLock=changed&amp;delete=true&amp;delay=60000" />
        <choice>
            <when>
                <simple>${file:ext} == 'properties'</simple>
                <to uri="file://{{teaser.config.directory}}" />
            </when>
            <when>
                <simple>${file:ext} == 'jpg' || ${file:ext} == 'JPG'</simple>
                <to uri="sftp://{{apache.ftp.user}}@{{apache.ftp.host}}/{{apache.teaser.ftp.targetDir}}?password={{apache.ftp.password}}&amp;binary=true&amp;separator=UNIX" />
            </when>
            <otherwise>
                <transform>
                    <simple>Dear user,\n\n the Teaser interface only accept *.jpg and *.properties files, but we found the file ${file.name}.\n\n Have a nice day,\nYour lovely Teaser interface</simple>
                </transform>
                <to
                    uri="smtp://smtp.blabla.com?contentType=text/plain&amp;from=blabla@blabla.com&amp;to=chica@chicas.com&amp;subject=A problem occured while setting up new teaser!" />
            </otherwise>
        </choice>
        <bean ref="teaserService" method="updateTeaser" />
    </route>

如何实现这样的行为?

由于

2 个答案:

答案 0 :(得分:1)

Camel文件组件是batch consumer,并向交换机添加有关其正在处理的批处理的属性。您可以测试属性CamelBatchComplete,如果设置为true,则调用您的bean。

答案 1 :(得分:0)

如果您只想在读取所有文件后继续操作,则必须以某种方式对其进行采样。这可以使用aggregator模式实现:

<route>
    <from uri="file://src/data/aggregate-and-process?readLock=changed&amp;delete=true&amp;delay=60000" />
    <aggregate strategyRef="aggregationStrategy" completionFromBatchConsumer="true">
        <correlationExpression>
            <constant>true</constant>
        </correlationExpression>
        <to uri="direct:sub" />
    </aggregate>
</route>

<route>
    <from uri="direct:sub" />
    <!-- processing aggregated body -->
</route>

请注意,我设置了completionFromBatchConsumer="true"。来自Camel文档:

  

此选项是指交易所来自批量消费者。然后,当启用时,Aggregator2将使用邮件头CamelBatchSize中批处理使用者确定的批处理大小。 [...]这可以用于聚合在给定轮询中从File端点消耗的所有文件。