Vijay Nandwana
Vijay Nandwana

Reputation: 2634

Exception handling Camel

I'm trying to pick a file from a directory, split a file and add each splitted lines to activemq. I'm facing a problem with exception handling during this process. Lets say a file in the directory is a binary file (executable), then splitter thows org.apache.camel.RuntimeCamelException and java.nio.charset.MalformedInputException exceptions. If this occurs, then I need to catch these exceptions, nothing should add in activemq and that just particular thread should exit after logging exception. Referred online and wrote the following code but don't know how to stop adding to activemq and quit specific thread.

<route id="msg_producer">
        <from uri="input.file.from" />
        <doTry>
        <split parallelProcessing="true" executorServiceRef="msgProducer"
            streaming="true">
            <tokenize token="\n"></tokenize>
            <to uri="input.activemq.to" />
        </split>
        <doCatch>
            <exception>org.apache.camel.RuntimeCamelException</exception>
            <exception>java.nio.charset.MalformedInputException</exception>
                <handled> <constant>true</constant></handled>
                <setBody>
                    <simple>${exception.stacktrace}</simple>
                </setBody>
                <setHeader headerName="CamelFileName">
                    <simple>${file:onlyname.noext}_error.log</simple>
                </setHeader>
        </doCatch>
        </doTry>
    </route>

Upvotes: 1

Views: 422

Answers (1)

Dilip
Dilip

Reputation: 91

as @claus Ibsen said to filter the files you can use a filefilter property so that you pick only files based on extension and some standard pattern something like this

    <bean id="FileFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter">
    <!--        ?   matches one character
                *   matches zero or more characters
                **  matches zero or more directories in a path -->
    <property name="includes" value="#{databaseProperties.getProperties().getProperty('file.name.pattern')}"/>
    <!-- if you wan to exclude specific files say bad in name or .exe files. Use comma to separate multiple excludes -->
    <!-- <property name="excludes" value="**/*bad*,**/*.exe"/> -->
    </bean>

and your file.name.pattern can be something like this **/contract.csv

Upvotes: 1

Related Questions