源文件夹为空后停止驼峰路径(文件传输)

时间:2014-11-28 14:24:35

标签: java apache-camel spring-dsl

我正在尝试创建一个简单的camel应用程序,用于将文件从一个文件夹传输到另一个文件夹。

File Transfer Camel Flow

我有两个问题

 1. Is there a way to stop the route once the source folder is empty.
 2. Is there a way to signel camel to stop the process, but in this case the camel should wait till the in-flight messages are processed.

对于,1,我尝试了一些类似的事情(基于camel stop when there are no files in the folder http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html

    <bean id="shutDownProcessor" class="com.acme.framework.util.ShutDownProcessor" />
    <route customId="true" id="ftpSend">
            <from uri="file:in"/>
            <choice>
                <when>
                    <simple>${body} != null</simple>
                    <wireTap uri="file:copy?fileName=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}-${file:size}.${file:ext}&amp;sendEmptyMessageWhenIdle=true">
                        <setHeader headerName="fileName">
                            <simple>${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}-${file:size}.${file:ext}</simple>
                        </setHeader>
                    </wireTap>
                    <to uri="file:out"/>
                </when>
                <otherwise>
                    <process ref="shutDownProcessor"/>
                </otherwise>
            </choice>
        </route>

shutDownProcessor处理器看起来像,

public class ShutDownProcessor implements Processor{
    Thread stop;

    @Override
    public void process(final Exchange exchange) throws Exception {
        if (stop == null) {
            stop = new Thread() {
                @Override
                public void run() {
                    try {
                        CamelContext context = exchange.getContext();
                        String currentRoute = context.getRoutes().get(0).getId();
                        context.stopRoute(currentRoute);
                        context.stop();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            };
        }
        stop.start();
    }
}

但是,即使源文件夹为空,似乎也没有调用shutDownProcessor处理器。 任何指针都会对我们有所帮助。

谢谢, Kallada

1 个答案:

答案 0 :(得分:1)

要在春季DSL中停止驼峰路线,只需添加&lt; camel:stop&gt;&lt; / camel:stop&gt;在其他部分。