Reputation: 845
I am using Apache Camel to pass messages. The program is in Java. I want to check if the incoming message contain text("Fault Code: 0") and ("Warning Code: 0") and if it does then do not send it forward....I have a route as below
from("direct:operation-send-"+getId())
.id("direct:operation-send-"+getId())
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.doTry()
.choice()
.when( ! body().contains("Fault Code: 0") )
.to(httpUrl.getUri())
.endChoice()
.endDoTry()
.doCatch(Exception.class)
.to("log:HTTP_SEND_CONNECTION_ERROR?level=ERROR&showHeaders=true")
.to("direct:operation-store-"+getId())
But this part when( ! body().contains("Fault Code: 0") ) is giving me a compilation error. I want to know how to check for a condition(of the text in the message) in a Camel route...........Would appreciate any help!!
Upvotes: 3
Views: 7683
Reputation: 3426
Static method body() from BuilderSupport class produces a ValueBuilder object. This object provides a fluent way to build a Predicate
, which is one of the idioms in the world of functional Java.
I'm not going to get into details of how Predicate
s work (you can read about them more here), but they cannot be used as a simple boolean
. This means, a predicate cannot be negated with negation sign - !
, and that is what you're trying to do here.
So instead of:
.when( ! body().contains("Fault Code: 0") )
you need to use ValueBuilder
's methods to form your conditions.
Also, notice that you are trying to check if your body contains a String
object. I am not sure if you need to tell ValueBuilder
about it, but it seems like a common practice in Camel (which I have never used myself).
Knowing those two things, the solution seems simple. Just replace the offending line with:
.when(bodyAs(String.class).not().contains("Fault Code: 0"))
Camel is a huge library that heavily operates on Java 8 features. Make sure you know how to use them properly first (there are many tutorials, this one for example).
EDIT: the body(Class) method is deprecated in newer versions of Camel (in 2.18.3 at least), so you should use bodyAs(Class) instead.
Always check javadocs when you see a warning, because they should contain information on what to replace deprecates with.
Suppressing a warning it is usually not a good idea!
The deprecated method will be deleted in future releases, and when that happens, it'll break your build.
Upvotes: 1