Stop camel route(File transfer) once the source folder is empty

1.9k views Asked by At

I'm trying to create a simple camel application for transferring files from one folder to another.

File Transfer Camel Flow

I have two questions in mind

 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.

For, 1, I tried some thing like ( based on 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>

The shutDownProcessor processor looks something like,

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();
    }
}

But it seems the shutDownProcessor processor is not being invoked even if the source folder is empty. Any pointers will help us a lot.

Thanks, Kallada

1

There are 1 answers

0
Panneerselvam On BEST ANSWER

To stop the camel route in spring DSL simply add <camel:stop></camel:stop> inside Otherwise section.