...
- Camel uses Spring Transactions as the default transaction handling in components like JMS and JPA
- Camel works with Spring 2 XML processing with the Xml Configuration
- Camel Spring XML Schema's is defined at Xml Reference
- Camel supports a powerful version of Spring Remoting which can use powerful routing between the client and server side along with using all of the available Components for the transport
- Camel provides powerful Bean Integration with any bean defined in a Spring
ApplicationContext
. - Camel integrates with various Spring helper classes; such as providing Type Converter support for Spring Resources etc
- Allows Spring to dependency inject Component instances or the CamelContext instance itself and auto-expose Spring beans as components and endpoints.
- Allows you to reuse the Spring Testing framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel's powerful Mock and Test endpoints
- From Camel 2.15 onwards : Camel supports Spring Boot using the
camel-spring-boot
component. - From Camel 2.17.1 onwards 1: Camel supports Spring Cache based Idempotent repository
Using Spring to configure the CamelContext
You can configure a a CamelContext
inside any any spring.xml
using the CamelContextFactoryBean. This will automatically start the CamelContext along with any referenced Routes along any referenced Component and Endpoint instances.
...
You need to add Camel to the schemaLocation
declaration
Code Block |
---|
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd |
...
Code Block | ||||
---|---|---|---|---|
| ||||
<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"> |
...
Using camel:
...
Namespace
Or you can refer to the camel XSD in the XML declaration:
...
... so the declaration is:
Wiki Markup |
---|
{snippet:id=xsdlocation|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBean.xml} |
camel:
namespace prefix, and you can omit the inline namespace declaration:Wiki Markup |
---|
{snippet:id=example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextFactoryBean.xml} |
Advanced
...
Configuration Using Spring
See more details at Advanced configuration of CamelContext using Spring
...
You can use Java Code to define your RouteBuilder implementations. These can be defined as beans in spring and then referenced in your camel context e.g.
Wiki Markup |
---|
{snippet:id=example5|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/camelContextRouteBuilderRef.xml} |
Using Using <package>
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 in 1.X
, requires a a <package></package>
tag specifying a comma separated list of packages that should be searched e.g.
...
Warning |
---|
Use caution when specifying the package name as |
Info | ||
---|---|---|
| ||
The The |
Using Using <packageScan>
In Camel 2.0 this has been extended to allow selective inclusion and exclusion of discovered route classes using Ant like path matching. In spring this is specified by adding a a <packageScan/>
tag. The tag must contain one or more 'more package
' elements (similar to 1.x
), and optionally one or more 'includes' or 'excludes' more includes
or excludes
elements specifying patterns to be applied to the fully qualified names of the discovered classes. e.g.,
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://camel.apache.org/schema/spring"> <packageScan> <package>org.example.routes</package> <excludes>**.*Excluded*</excludes> <includes>**.*</includes> </packageScan> </camelContext> |
...
In the above example, camel will scan all the 'the org.example.routes
' package and any subpackages for for RouteBuilder
classes. Say the scan finds two RouteBuilders
, one in in org.example.routes
called ' called MyRoute
" and another 'another MyExcludedRoute
' in a subpackage 'excluded
'. The fully qualified names of each of the classes are extracted (org.example.routes.MyRoute
, org.example.routes.excluded.MyExcludedRoute
) and the include and exclude patterns are applied.
The exclude pattern pattern **.*Excluded*
is going to match the fqcn 'FQCN org.example.routes.excluded.MyExcludedRoute
' and veto camel from initializing it.
...
Code Block |
---|
? matches one character * matches zero or more characters ** matches zero or more segments of a fully qualified name |
For example:
**.*Excluded*
would match org.simple.Excluded
, org.apache.camel.SomeExcludedRoute
or org.example.RouteWhichIsExcluded
.
**.??cluded*
would match org.simple.IncludedRoute
, org.simple.Excluded
but not match org.simple.PrecludedRoute
.
Using Using contextScan
Available as of Camel 2.4
You can allow Camel to scan the container context, e.g. the Spring ApplicationContext
for route builder instances. This allow you to use the Spring <component-scan>
feature and have Camel pickup any any RouteBuilder
instances which was created by Spring in its scan process.
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/issues/contextscan/SpringRouteIsComponentAnnotatedTest.xml} |
@Component
and have those routes included by Camel:Code Block |
---|
@Component
public class MyRoute extends SpringRouteBuilder {
@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 <packageScan>
documentation.
Include Page | ||||
---|---|---|---|---|
|
Test
...
Time Exclusion.
At test time it is often desirable to be able to selectively exclude matching routes from being initalized initialized that are not applicable or useful to the test scenario. For instance you might a spring context file file routes-context.xml
and three Route builders RouteA
, RouteB
and and RouteC
in the 'the org.example.routes
' package. The The packageScan
definition would discover all three of these routes and initialize them.
Say Say RouteC
is not applicable to our test scenario and generates a lot of noise during test. It would be nice to be able to exclude this route from this specific test. The The SpringTestSupport
class has been modified to allow this. It provides two methods (excludedRoute
and excludedRoutes
) that may be overridden to exclude a single class or an array of classes.
Code Block | ||||
---|---|---|---|---|
| ||||
public class RouteAandRouteBOnlyTest extends SpringTestSupport { @Override protected Class excludeRoute() { return RouteC.class; } } |
In order to hook into the the camelContext
initialization by spring to exclude the the MyExcludedRouteBuilder.class
we need to intercept the spring context creation. When overriding overriding createApplicationContext
to create the spring context, we call the the getRouteExcludingApplicationContext()
method to provide a special parent spring context that takes care of the exclusion.
Code Block | ||||
---|---|---|---|---|
| ||||
@Override protected AbstractXmlApplicationContext createApplicationContext() { return new ClassPathXmlApplicationContext(new String[] {"routes-context.xml"}, getRouteExcludingApplicationContext()); } |
RouteC
will now be excluded from initialization. Similarly, in another test that is testing only RouteC
, we could exclude exclude RouteB
and and RouteA
by overriding:
Code Block |
---|
@Override protected Class[] excludeRoutes() { return new Class[]{RouteA.class, RouteB.class}; } |
...
You can configure your Component or Endpoint instances in your Spring XML as follows in this example.
Wiki Markup |
---|
{snippet:id=example|lang=xml|url=camel/trunk/components/camel-jms/src/test/resources/org/apache/camel/component/jms/jmsRouteUsingSpring.xml} |
activemq
in the above example), then you can refer to the component using activemq:[queue:|topic:]destinationName
. This works by the the SpringCamelContext
lazily fetching components from the spring context for the scheme name you use for Endpoint URIs.For more detail see Configuring Endpoints and Components.
Spring Cache
...
Idempotent Repository
Available as of Camel 2.17.1
...
If you want to be injected with the CamelContext in your POJO just implement the CamelContextAware interface; then when Spring creates your POJO the the CamelContext
will be injected into your POJO. Also see the Bean Integration for further injections.
...