Languages Supported AppendixTo support flexible and powerful Enterprise Integration Patterns Camel supports various Languages to create an Expression or Predicate within either the Routing Domain Specific Language or the Xml Configuration. The following languages are supported Bean LanguageThe purpose of the Bean Language is to be able to implement an Expression or Predicate using a simple method on a bean. So the idea is you specify a bean name which will then be resolved in the Registry such as the Spring ApplicationContext then a method is invoked to evaluate the Expression or Predicate. If no method name is provided then one is attempted to be chosen using the rules for Bean Binding; using the type of the message body and using any annotations on the bean methods. The Bean Binding rules are used to bind the Message Exchange to the method parameters; so you can annotate the bean to extract headers or other expressions such as XPath or XQuery from the message. Using Bean Expressions from the Java DSLfrom("activemq:OrdersTopic"). filter().method("myBean", "isGoldCustomer"). to("activemq:BigSpendersQueue"); Using Bean Expressions from XML<route> <from uri="activemq:OrdersTopic"/> <filter> <method bean="myBean" method="isGoldCustomer"/> <to uri="activemq:BigSpendersQueue"/> </filter> </route> Writing the expression beanThe bean in the above examples is just any old Java Bean with a method called isGoldCustomer() that returns some object that is easily converted to a boolean value in this case, as its used as a predicate. So we could implement it like this... public class MyBean { public boolean isGoldCustomer(Exchange exchange) { ... } } We can also use the Bean Integration annotations. For example you could do... public boolean isGoldCustomer(String body) {...} or public boolean isGoldCustomer(@Header(name = "foo") Integer fooHeader) {...} So you can bind parameters of the method to the Exchange, the Message or individual headers, properties, the body or other expressions. Non registry beansAs of Camel 1.5 the Bean Language also supports invoking beans that isn't registered in the Registry. This is usable for quickly to invoke a bean from Java DSL where you don't need to register the bean in the Registry such as the Spring ApplicationContext. Camel can instantiate the bean and invoke the method if given a class or invoke an already existing instance. This is illustrated from the example below: from("activemq:OrdersTopic"). filter().expression(bean(MyBean.class, "isGoldCustomer")). to("activemq:BigSpendersQueue"); The 2nd parameter isGoldCustomer is an optional parameter to explicit set the method name to invoke. If not provided Camel will try to invoke the best suited method. If case of ambiguity Camel will thrown an Exception. In these situations the 2nd parameter can solve this problem. Also the code is more readable if the method name is provided. The 1st parameter can also be an existing instance of a Bean such as: private MyBean my; from("activemq:OrdersTopic"). filter().expression(bean(my, "isGoldCustomer")). to("activemq:BigSpendersQueue"); Other examplesWe have some test cases you can look at if it'll help
DependenciesThe Bean language is part of camel-core. Constant Expression LanguageThe Constant Expression Language is really just a way to specify constant strings as a type of expression. Available as of Camel 1.5 Example usageThe setHeader element of the Spring DSL can utilize a constant expression like: <route> <from uri="seda:a"/> <setHeader headerName="theHeader"> <constant>the value</constant> </setHeader> <to uri="mock:b"/> </route> in this case, the Message coming from the seda:a Endpoint will have 'theHeader' header set to the constant value 'the value'. And the same example using Java DSL: from("seda:a").setHeader("theHeader", constant("the value")).to("mock:b"); DependenciesThe Constant language is part of camel-core. ELCamel supports the unified JSP and JSF Expression Language via the JUEL For example you could use EL inside a Message Filter in XML <route> <from uri="seda:foo"/> <filter> <el>${in.headers.foo == 'bar'}</el> <to uri="seda:bar"/> </filter> </route> You could also use slightly different syntax, e.g. if the header name is not a valid identifier: <route> <from uri="seda:foo"/> <filter> <el>${in.headers['My Header'] == 'bar'}</el> <to uri="seda:bar"/> </filter> </route> You could use EL to create an Predicate in a Message Filter or as an Expression for a Recipient List Variables
SamplesYou can use EL dot notation to invoke operations. If you for instance have a body that contains a POJO that has a getFamiliyName method then you can construct the syntax as follows: "$in.body.familyName"
You have the full power of EL at your hand so you can invoke methods to for instance replace text. In the sample below we replace the text in the body where id is replaced with orderId. <from uri="seda:incomingOrders"> <setBody> <el>${in.body.replaceAll('id','orderId')}</el> </setBody> <to uri="seda:processOrder"/> And the sample using the Java DSL: from("seda:incomingOrders").setBody().el("${in.body.replaceAll('id','orderId')}").to("seda:processOrder"); DependenciesTo use EL in your camel routes you need to add the a dependency on camel-juel which implements the EL language. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-juel</artifactId> <version>1.4.0</version> </dependency> Header Expression LanguageThe Header Expression Language allows you to extract values of named headers. Available as of Camel 1.5 Example usageThe recipientList element of the Spring DSL can utilize a header expression like: <route>
<from uri="direct:a"/>
<recipientList>
<header>recipientListHeader</header>
</recipientList>
</route>
In this case, the list of recipients are contained in the header 'recipientListHeader'. And the same example in Java DSL: from("direct:a").recipientList(header("recipientListHeader")); And with a slighly different syntax where you use the builder to the fullest (i.e. avoid using parameters but using stacked operations, notice that header is not a parameter but a stacked method call) from("direct:a").recipientList().header("recipientListHeader"); DependenciesThe Header language is part of camel-core. JXPathCamel supports JXPath From 1.3 of Camel onwards you can use XPath expressions directly using smart completion in your IDE as follows from("queue:foo").filter(). jxpath("/in/body/foo"). to("queue:bar") Variables
Using XML configurationIf you prefer to configure your routes in your Spring XML file then you can use JXPath expressions as follows <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="activemq:MyQueue"/> <filter> <jxpath>in/body/name = 'James'</xpath> <to uri="mqseries:SomeOtherQueue"/> </filter> </route> </camelContext> </beans> ExamplesHere is a simple example from("direct:start"). filter().jxpath("in/body/name='James'"). to("mock:result"); JXPath injectionYou can use Bean Integration to invoke a method on a bean and use various languages such as JXPath to extract a value from the message and bind it to a method parameter. For example public class Foo { @MessageDriven(uri = "activemq:my.queue") public void doSomething(@JXPath("in/body/foo") String correlationID, @Body String body) { // process the inbound message here } } DependenciesTo use JXpath in your camel routes you need to add the a dependency on camel-jxpath which implements the JXpath language. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jxpath</artifactId> <version>1.4.0</version> </dependency> OGNLCamel allows OGNL You could use OGNL to create an Predicate in a Message Filter or as an Expression for a Recipient List You can use OGNL dot notation to invoke operations. If you for instance have a body that contains a POJO that has a getFamiliyName method then you can construct the syntax as follows: "request.body.familyName" // or "getRequest().getBody().getFamilyName()" Variables
SamplesFor example you could use OGNL inside a Message Filter in XML <route> <from uri="seda:foo"/> <filter> <ognl>request.headers.foo = 'bar'</ognl> <to uri="seda:bar"/> </filter> </route> And the sample using Java DSL: from("seda:foo").filter().ognl("request.headers.foo = 'bar'").to("seda:bar"); DependenciesTo use OGNL in your camel routes you need to add the a dependency on camel-ognl which implements the OGNL language. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ognl</artifactId> <version>1.4.0</version> </dependency> Scripting LanguagesCamel supports a number of scripting languages which can be used to create an Expression or Predicate via the standard JSR 223 The following scripting languages are integrated into the DSL: However any JSR 223 ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> See AlsoBeanShellCamel supports BeanShell To use a BeanShell expression use the following Java code ... beanShell("someBeanShellExpression") ...
For example you could use the beanShell function to create an Predicate in a Message Filter or as an Expression for a Recipient List ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> JavaScriptCamel supports JavaScript/ECMAScript To use a JavaScript expression use the following Java code ... javaScript("someJavaScriptExpression") ...
For example you could use the javaScript function to create an Predicate in a Message Filter or as an Expression for a Recipient List ExampleIn the sample below we use JavaScript to create a Predicate use in the route path, to route exchanges from admin users to a special queue. from("direct:start") .choice() .when().javaScript("request.headers.get('user') == 'admin'").to("seda:adminQueue") .otherwise() .to("seda:regularQueue"); And a Spring DSL sample as well: <route> <from uri="direct:start"/> <choice> <when> <javaScript>request.headers.get('user') == 'admin'</javaScript> <to uri="seda:adminQueue"/> </when> <otherwise> <to uri="seda:regularQueue"/> </otherwise> </choice> </route> ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> GroovyCamel supports Groovy To use a Groovy expression use the following Java code ... groovy("someGroovyExpression") ...
For example you could use the groovy function to create an Predicate in a Message Filter or as an Expression for a Recipient List Example// lets route if a line item is over $100 from("queue:foo").filter(groovy("request.lineItems.any { i -> i.value > 100 }")).to("queue:bar") And the Spring DSL: <route> <from uri="queue:foo"/> <filter> <groovy>request.lineItems.any { i -> i.value > 100 }</groovy> <to uri="queue:bar"/> </filter> </route> ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> PythonCamel supports Python To use a Python expression use the following Java code ... python("somePythonExpression") ...
For example you could use the python function to create an Predicate in a Message Filter or as an Expression for a Recipient List ExampleIn the sample below we use Python to create a Predicate use in the route path, to route exchanges from admin users to a special queue. from("direct:start") .choice() .when().python("request.headers['user'] == 'admin'").to("seda:adminQueue") .otherwise() .to("seda:regularQueue"); And a Spring DSL sample as well: <route> <from uri="direct:start"/> <choice> <when> <python>request.headers['user'] == 'admin'</python> <to uri="seda:adminQueue"/> </when> <otherwise> <to uri="seda:regularQueue"/> </otherwise> </choice> </route> ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> PHPCamel supports PHP To use a PHP expression use the following Java code ... php("somePHPExpression") ...
For example you could use the php function to create an Predicate in a Message Filter or as an Expression for a Recipient List ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> RubyCamel supports Ruby To use a Ruby expression use the following Java code ... ruby("someRubyExpression") ...
For example you could use the ruby function to create an Predicate in a Message Filter or as an Expression for a Recipient List ExampleIn the sample below we use Ruby to create a Predicate use in the route path, to route exchanges from admin users to a special queue. from("direct:start") .choice() .when().ruby("$request.headers['user'] == 'admin'").to("seda:adminQueue") .otherwise() .to("seda:regularQueue"); And a Spring DSL sample as well: <route> <from uri="direct:start"/> <choice> <when> <ruby>$request.headers['user'] == 'admin'</ruby> <to uri="seda:adminQueue"/> </when> <otherwise> <to uri="seda:regularQueue"/> </otherwise> </choice> </route> ScriptContextThe JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
AttributesYou can add your own attributes with the attribute(name, value) DSL method, such as: In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned. from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser").to("seda:users"); Any scripting languageCamel can run any JSR-223 scripting languages using the script DSL method such as: from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser").to("seda:users"); This is a bit different using the Spring DSL where you use the expression element that doesn't support setting attributes (yet): <from uri="direct:in"/> <setHeader headerName="firstName"> <expression language="jaskel">user.firstName</expression> </setHeader> <to uri="seda:users"/> DependenciesTo use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-script</artifactId> <version>1.4.0</version> </dependency> Simple Expression LanguageThe Simple Expression Language is a really simple language you can use. Its primarily intended for being a really small and simple language for testing without requiring any new dependencies or knowledge of XPath; so its ideal for testing in camel-core. However for real world use cases you are generally recommended to choose a more expressive and powerful language such as:
The simple language uses ${body} placeholders for complex expressions where the expression contains constant literals. The ${ } placeholders can be omitted if the expression is only the token itself. To get the body of the in message: "body", or "in.body" or "${body}". A complex expression must use ${ } placeholders, such as: "Hello ${in.header.name} how are you?". You can have multiple tokens in the same expression: "Hello ${in.header.name} this is ${in.header.me} speaking". Variables
SamplesIn the Spring XML sample below we filter based on a header value: <from uri="seda:orders"> <filter> <simple>in.header.foo</simple> <to uri="mock:fooOrders"/> </filter> </from> The Simple language can be used for the predicate test above in the Message Filter pattern, where we test if the in message has a foo header (a header with the key foo exists). If the expression evaluates to true then the message is routed to the mock:foo endpoint, otherwise its lost in the deep blue sea The same example in Java DSL: from("seda:orders") .filter().simple("in.header.foo").to("seda:fooOrders"); You can also use the simple language for simple text concatenations such as: from("direct:hello").transform().simple("Hello ${in.header.user} how are you?").to("mock:reply"); Notice that we must use ${ } placeholders in the expression now to let Camel be able to parse it correctly. And this sample uses the date command to output current date. from("direct:hello").transform().simple("The today is ${date:now:yyyyMMdd} and its a great day.").to("mock:reply"); And in the sample below we invoke the bean language to invoke a method on a bean to be included in the returned string: from("direct:order").transform().simple("OrderId: ${bean:orderIdGenerator}").to("mock:reply"); Where orderIdGenerator is the id of the bean registered in the Registry. If using Spring then its the Spring bean id. DependenciesThe Bean language is part of camel-core. File Expression LanguageAvailable as of Camel 1.5 The File Expression Language is an extension to the Simple language, adding file related capabilities. These capabilities is related to common use cases working with file path and names. The goal is to allow expression to be used with the File and FTP components for setting dynamic file patterns for both consumer and producer. SyntaxThis language is an extension to the Simple language so the Simple syntax applies also. So the table below only lists the additional. All the file tokens uses the same expression name as the method on the java.io.File object, for instance file:absolute
File token exampleWe have a java.io.File handle for the file hello.txt in the following directory: D:\project\camel\camel-core\target\filelanguage. The file tokens will return as:
SamplesYou can enter a fixed Constant expression such as myfile.txt: expression="myfile.txt"
Lets assume we use the file consumer to read files and want to move the read files to backup folder with the current date as a sub folder. This can be archived using an expression like: expression="backup/${date:now:yyyyMMdd}/${file:name.noext}.bak"
relative folder names is also supported so suppose the backup folder should be a sibling folder then you can append .. as: expression="../backup/${date:now:yyyyMMdd}/${file:name.noext}.bak"
As this is an extension to the Simple language we have access to all the goodies from this language also, so in this use case we want to use the in.header.type as a parameter in the dynamic expression: expression="../backup/${date:now:yyyyMMdd}/type-${in.header.type}/backup-of-${file:name.noext}.bak"
If you have a custom Date you want to use in the expression then Camel supports retrieving dates from the message header. expression="orders/order-${in.header.customerId}-${date:in.header.orderDate:yyyyMMdd}.xml"
And finally we can also use a bean expression to invoke a POJO class that generates some String output (or convertible to String) to be used: expression="uniquefile-${bean:myguidgenerator.generateid}.txt"
And of course all this can be combined in one expression where you can use the File Language, Simple and the Bean language in one combined expression. This is pretty powerful for those common file path patterns. DependenciesThe File language is part of camel-core. SQLThe SQL support is added by JoSQL Camel supports SQL from("queue:foo").setBody().sql("select * from MyType").to("queue:bar") And the spring DSL: <from uri="queue:foo"/> <setBody> <sql>select * from MyType</sql> </setBody> <to uri="queue:bar"/> Variables
DependenciesTo use SQL in your camel routes you need to add the a dependency on camel-josql which implements the SQL language. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-josql</artifactId> <version>1.4.0</version> </dependency> XPathCamel supports XPath from("queue:foo"). filter().xpath("//foo")). to("queue:bar") from("queue:foo"). choice().xpath("//foo")).to("queue:bar"). otherwise().to("queue:others"); NamespacesIn 1.3 onwards you can easily use namespaces with XPath expressions using the Namespaces helper class. Namespaces ns = new Namespaces("c", "http://acme.com/cheese"); from("direct:start").filter(). xpath("/c:person[@name='James']", ns). to("mock:result"); VariablesVariables in XPath is defined in different namespaces. The default namespace is http://activemq.apache.org/camel/schema/spring
Camel will resolve variables according to either:
Namespace givenIf the namespace is given then Camel is instructed exactly what to return. However when resolving either in or out Camel will try to resolve a header with the given local part first, and return it. If the local part has the value body then the body is returned instead. No namespace givenIf there is no namespace given then Camel resolves only based on the local part. Camel will try to resolve a variable in the following steps:
FunctionsCamel adds the following XPath functions that can be used to access the exchange:
Here's an example showing some of these functions in use. from("direct:start").choice() .when().xpath("in:header('foo') = 'bar'").to("mock:x") .when().xpath("in:body() = '<two/>'").to("mock:y") .otherwise().to("mock:z"); Using XML configurationIf you prefer to configure your routes in your Spring XML file then you can use XPath expressions as follows <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:foo="http://example.com/person" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd"> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="activemq:MyQueue"/> <filter> <xpath>/foo:person[@name='James']</xpath> <to uri="mqseries:SomeOtherQueue"/> </filter> </route> </camelContext> </beans> Notice how we can reuse the namespace prefixes, foo in this case, in the XPath expression for easier namespace based XPath expressions! ExamplesHere is a simple example from("direct:start"). filter().xpath("/person[@name='James']"). to("mock:result"); If you have a standard set of namespaces you wish to work with and wish to share them across many different XPath expressions you can use the NamespaceBuilder as shown in this example // lets define the namespaces we'll need in our filters Namespaces ns = new Namespaces("c", "http://acme.com/cheese") .add("xsd", "http://www.w3.org/2001/XMLSchema"); // now lets create an xpath based Message Filter from("direct:start"). filter(ns.xpath("/c:person[@name='James']")). to("mock:result"); In this sample we have a choice construct. The first choice evaulates if the message has a header key type that has the value Camel. from("direct:in").choice() // using $headerName is special notation in Camel to get the header key .when().xpath("$type = 'Camel'") .to("mock:camel") // here we test for the body name tag .when().xpath("//name = 'Kong'") .to("mock:donkey") .otherwise() .to("mock:other") .end(); And the spring XML equivalent of the route: <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:in"/> <choice> <when> <xpath>$type = 'Camel'</xpath> <to uri="mock:camel"/> </when> <when> <xpath>//name = 'Kong'</xpath> <to uri="mock:donkey"/> </when> <otherwise> <to uri="mock:other"/> </otherwise> </choice> </route> </camelContext> XPath injectionYou can use Bean Integration to invoke a method on a bean and use various languages such as XPath to extract a value from the message and bind it to a method parameter. The default XPath annotation has SOAP and XML namespaces available. If you want to use your own namespace URIs in an XPath expression you can use your own copy of the XPath annotation i.e. cut and paste the XPath annotation code For example public class Foo { @MessageDriven(uri = "activemq:my.queue") public void doSomething(@Path("/foo/bar/text()") String correlationID, @Body String body) { // process the inbound message here } } DependenciesThe XPath language is part of camel-core. XQueryCamel supports XQuery |