0

I am new to Apache camel. Does anyone know how to use camel to process the content of a text file to check if a particular string e.g "error" is present within a text file. I cant seem to go past the first line below with java. Any help will be appreciated

 from("file://inputdir/").convertBodyTo(String.class).
Beanieman
  • 7
  • 3

2 Answers2

1

Use bodyAs and contains. For example:

from("file://inputdir/")
    .choice()
        .when(bodyAs(String.class).contains("error"))
            .to(/* a route for errors */)
        .otherwise()
            .to(/* a route for non-errors */);
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
  • I tried using bodyAs but eclipse is highlighting an error. 'The method bodyAs(Class) is undefined for the type'. I am guessing its deprecated. – Beanieman Jun 20 '17 at 13:23
  • What version of Camel are you using? [`bodyAs`](http://static.javadoc.io/org.apache.camel/camel-core/2.19.1/org/apache/camel/builder/BuilderSupport.html#bodyAs-java.lang.Class-) is available in 2.19.1. – Jeffrey Chung Jun 20 '17 at 13:36
  • The method was found in 2.19.1 just like you said. I was using 2.15. Then I Changed to 2.19.1 because i was desperate to fix this issue but my Wildfly 8.2.0 couldnt run the camel context because of imcompatibility . Then I had to upgrade Wildfly to 10.1. But my App Would not run all together. Is there another way without Using bodyAs Or could you suggest an easier App Server to power camel as I am new to the technology – Beanieman Jun 21 '17 at 00:41
  • This Works. Thanks – Beanieman Jun 25 '17 at 03:48
0

You can use ${bodyAs(String)} as follows:

<route id="_route1">
<from id="_from1" uri="file:work/cbr/input"/>
<when id="_when1">
<simple>${bodyAs(String)} contains 'ABC'</simple>
<log id="_log1" message="contains string ABC"/>
<to id="_to1" uri="file:work/cbr/output"/>
</when>
</route>
Rajani
  • 1
  • 1