此内容没有您所选择的语言版本。
Chapter 181. JXPath Language (deprecated)
Available as of Camel version 1.3
Camel supports JXPath to allow XPath expressions to be used on beans in an Expression or Predicate to be used in the DSL or Xml Configuration. For example you could use JXPath to create an Predicate in a Message Filter or as an Expression for a Recipient List.
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")
181.1. JXPath Options 复制链接链接已复制到粘贴板!
The JXPath language supports 2 options which are listed below.
| Name | Default | Java Type | Description |
|---|---|---|---|
| lenient |
|
| Allows to turn lenient on the JXPathContext. When turned on this allows the JXPath expression to evaluate against expressions and message bodies which may be invalid / missing data. This option is by default false |
| trim |
|
| Whether to trim the value to remove leading and trailing whitespaces and line breaks |
181.2. Variables 复制链接链接已复制到粘贴板!
| Variable | Type | Description |
|---|---|---|
| this | Exchange | the Exchange object |
| in | Message | the exchange.in message |
| out | Message | the exchange.out message |
181.3. Options 复制链接链接已复制到粘贴板!
| Option | Type | Description |
|---|---|---|
| lenient | boolean | Camel 2.11/2.10.5: Allows to turn lenient on the JXPathContext. When turned on this allows the JXPath expression to evaluate against expressions and message bodies which may be invalid / missing data. See more details at the JXPath Documentation This option is by default false. |
181.4. Using XML configuration 复制链接链接已复制到粘贴板!
If 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.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/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>
181.5. Examples 复制链接链接已复制到粘贴板!
Here is a simple example using a JXPath expression as a predicate in a Message Filter
181.6. JXPath injection 复制链接链接已复制到粘贴板!
You 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
}
}
181.7. Loading script from external resource 复制链接链接已复制到粘贴板!
Available as of Camel 2.11
You can externalize the script and have Camel load it from a resource such as "classpath:", "file:", or "http:".
This is done using the following syntax: "resource:scheme:location", eg to refer to a file on the classpath you can do:
.setHeader("myHeader").jxpath("resource:classpath:myjxpath.txt")
181.8. Dependencies 复制链接链接已复制到粘贴板!
To 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>x.x.x</version>
</dependency>
Otherwise, you’ll also need Commons JXPath.