Chapter 4. Using Camel with Spring XML
Chapter 4. Using Camel with Spring XML Using Camel with Spring XML files is a way of using XML DSL with Camel. Camel has historically been using Spring XML for a long time. The Spring framework started with XML files as a popular and common configuration for building Spring applications. Example of Spring application <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 xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:a"/> <choice> <when> <xpath>USDfoo = 'bar'</xpath> <to uri="direct:b"/> </when> <when> <xpath>USDfoo = 'cheese'</xpath> <to uri="direct:c"/> </when> <otherwise> <to uri="direct:d"/> </otherwise> </choice> </route> </camelContext> </beans> 4.1. Using Java DSL with Spring XML files You can use Java Code to define your RouteBuilder implementations. These are defined as beans in spring and then referenced in your camel context, as shown: <camelContext xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="myBuilder"/> </camelContext> <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/> 4.1.1. Configure Spring Boot Application To use Spring Boot Autoconfigure XML routes for beans, you musy import the XML resource. To do this, you can use a Configuration class. For example, given that the Spring XML file is located to src/main/resources/camel-context.xml you can use the following configuration class to load the camel-context: Example: using a Configuration class import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; /** * A Configuration class that import the Spring XML resource */ @Configuration // load the spring xml file from classpath @ImportResource("classpath:camel-context.xml") public class CamelSpringXMLConfiguration { } 4.2. Specifying Camel routes using Spring XML You can use Spring XML files to specify Camel routes using XML DSL as shown: <camelContext id="camel-A" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="seda:start"/> <to uri="mock:result"/> </route> </camelContext> 4.3. Configuring Components and Endpoints You can configure your Component or Endpoint instances in your Spring XML as follows in this example. <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> </camelContext> <bean id="jmsConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> </property> </bean> This allows you to configure a component using any name, but its common to use the same name, for example, jms . Then you can refer to the component using jms:destinationName . This works by the Camel fetching components from the Spring context for the scheme name you use for Endpoint URIs. 4.4. Using package scanning Camel also provides a powerful feature that allows for the automatic discovery and initialization of routes in given packages. This is configured by adding tags to the camel context in your spring context definition, specifying the packages to be recursively searched for RouteBuilder implementations. To use this feature add a <package></package> tag specifying a comma separated list of packages that should be searched. For example, <camelContext> <packageScan> <package>com.foo</package> <excludes>**.*Excluded*</excludes> <includes>**.*</includes> </packageScan> </camelContext> This scans for RouteBuilder classes in the com.foo and the sub-packages. You can also filter the classes with includes or excludes such as: <camelContext> <packageScan> <package>com.foo</package> <excludes>**.*Special*</excludes> </packageScan> </camelContext> This skips the classes that has Special in the name. Exclude patterns are applied before the include patterns. If no include or exclude patterns are defined then all the Route classes discovered in the packages are returned. ? matches one character, * matches zero or more characters, ** matches zero or more segments of a fully qualified name. 4.5. Using context scanning You can allow Camel to scan the container context, for example, the Spring ApplicationContext for route builder instances. This allows you to use the Spring <component-scan> feature and have Camel pickup any RouteBuilder instances which was created by Spring in its scan process. <!-- enable Spring @Component scan --> <context:component-scan base-package="org.apache.camel.spring.issues.contextscan"/> <camelContext xmlns="http://camel.apache.org/schema/spring"> <!-- and then let Camel use those @Component scanned route builders --> <contextScan/> </camelContext> This allows you to just annotate your routes using the Spring @Component and have those routes included by Camel: @Component public class MyRoute extends RouteBuilder { @Override public void configure() throws Exception { from("direct:start") .to("mock:result"); } } You can also use the ANT style for inclusion and exclusion, as mentioned above in the package scan section.
<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 xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:a"/> <choice> <when> <xpath>USDfoo = 'bar'</xpath> <to uri="direct:b"/> </when> <when> <xpath>USDfoo = 'cheese'</xpath> <to uri="direct:c"/> </when> <otherwise> <to uri="direct:d"/> </otherwise> </choice> </route> </camelContext> </beans> <camelContext xmlns="http://camel.apache.org/schema/spring"> <routeBuilder ref="myBuilder"/> </camelContext> <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/> import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; /** * A Configuration class that import the Spring XML resource */ @Configuration // load the spring xml file from classpath @ImportResource("classpath:camel-context.xml") public class CamelSpringXMLConfiguration { } <camelContext id="camel-A" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="seda:start"/> <to uri="mock:result"/> </route> </camelContext> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> </camelContext> <bean id="jmsConnectionFactory" class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> <bean id="jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp:someserver:61616"/> </bean> </property> </bean> <camelContext> <packageScan> <package>com.foo</package> <excludes>**.*Excluded*</excludes> <includes>**.*</includes> </packageScan> </camelContext> <camelContext> <packageScan> <package>com.foo</package> <excludes>**.*Special*</excludes> </packageScan> </camelContext> <!-- enable Spring @Component scan --> <context:component-scan base-package="org.apache.camel.spring.issues.contextscan"/> <camelContext xmlns="http://camel.apache.org/schema/spring"> <!-- and then let Camel use those @Component scanned route builders --> <contextScan/> </camelContext> @Component public class MyRoute extends RouteBuilder { @Override public void configure() throws Exception { from("direct:start") .to("mock:result"); } }