Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
Chapter 5. Develop Camel Applications in Spring Boot
5.1. Introduction to Camel Spring Boot 复制链接链接已复制到粘贴板!
Spring Boot component provides auto configuration for Apache Camel. Auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities such as producer template, consumer template, and the type converter as beans.
Every Camel Spring Boot application should use dependencyManagement
with productized versions, see quickstart pom. Versions that are tagged later can be omitted to not override the versions from BOM.
camel-spring-boot
jar comes with the spring.factories
file which allows you to add that dependency into your classpath and hence Spring Boot will automatically auto-configure Camel.
5.2. Introduction to Camel Spring Boot Starter 复制链接链接已复制到粘贴板!
Apache Camel contains Spring Boot Starter module that allows you to develop Spring Boot applications using starters.
For more details, see sample application in the source code.
To use the starter, add the following snippet to your Spring Boot pom.xml
file:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
The starter allows you to add classes with your Camel routes, as shown in the snippet below. Once these routes are added to the class path the routes are started automatically.
You can customize the Camel application in the application.properties
or application.yml
file.
5.3. Auto-configured Camel context 复制链接链接已复制到粘贴板!
Camel auto-configuration provides a CamelContext
instance and creates a SpringCamelContext
. It also initializes and performs shutdown of that context. This Camel context is registered in the Spring application context under camelContext
bean name and you can access it like other Spring bean.
For example, you can access the camelContext
as shown below:
5.4. Auto-detecting Camel routes 复制链接链接已复制到粘贴板!
Camel auto configuration collects all the RouteBuilder
instances from the Spring context and automatically injects them into the CamelContext
. It simplifies the process of creating new Camel route with the Spring Boot starter. You can create the routes by adding the @Component
annotated class to your classpath.
To create a new route RouteBuilder
bean in your @Configuration
class, see below:
5.5. Camel properties 复制链接链接已复制到粘贴板!
Spring Boot auto configuration automatically connects to Spring Boot external configuration such as properties placeholders, OS environment variables, or system properties with Camel properties support.
These properties are defined in application.properties
file:
route.from = jms:invoices
route.from = jms:invoices
Use as system property
java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
java -Droute.to=jms:processed.invoices -jar mySpringApp.jar
Use as placeholders in Camel route:
5.6. Custom Camel context configuration 复制链接链接已复制到粘贴板!
To perform operations on CamelContext
bean created by Camel auto configuration, you need to register CamelContextConfiguration
instance in your Spring context as shown below:
The method CamelContextConfiguration
and beforeApplicationStart(CamelContext)
will be called before the Spring context is started, so the CamelContext
instance passed to this callback is fully auto-configured. You can add many instances of CamelContextConfiguration
into your Spring context and all of them will be executed.
5.7. Disabling JMX 复制链接链接已复制到粘贴板!
To disable JMX of the auto-configured CamelContext
use camel.springboot.jmxEnabled
property as JMX is enabled by default.
For example, you could add the following property to your application.properties
file:
camel.springboot.jmxEnabled = false
camel.springboot.jmxEnabled = false
5.8. Auto-configured consumer and producer templates 复制链接链接已复制到粘贴板!
Camel auto configuration provides pre-configured ConsumerTemplate
and ProducerTemplate
instances. You can inject them into your Spring-managed beans:
By default consumer templates and producer templates come with the endpoint cache sizes set to 1000. You can change those values using the following Spring properties:
camel.springboot.consumerTemplateCacheSize = 100 camel.springboot.producerTemplateCacheSize = 200
camel.springboot.consumerTemplateCacheSize = 100
camel.springboot.producerTemplateCacheSize = 200
5.9. Auto-configured TypeConverter 复制链接链接已复制到粘贴板!
Camel auto configuration registers a TypeConverter
instance named typeConverter
in the Spring context.
5.10. Spring type conversion API bridge 复制链接链接已复制到粘贴板!
Spring consist of type conversion API. Spring API is similar to the Camel type converter API. Due to the similarities between the two APIs Camel Spring Boot automatically registers a bridge converter (SpringTypeConverter
) that delegates to the Spring conversion API. That means that out-of-the-box Camel will treat Spring Converters similar to Camel.
This allows you to access both Camel and Spring converters using the Camel TypeConverter
API, as shown below:
Here, Spring Boot delegates conversion to the Spring’s ConversionService
instances available in the application context. If no ConversionService
instance is available, Camel Spring Boot auto configuration creates an instance of ConversionService
.
5.11. Disabling type conversions features 复制链接链接已复制到粘贴板!
To disable registering type conversion features of Camel Spring Boot such as TypeConverter
instance or Spring bridge, set the camel.springboot.typeConversion
property to false
as shown below:
camel.springboot.typeConversion = false
camel.springboot.typeConversion = false
5.12. Adding XML routes 复制链接链接已复制到粘贴板!
By default, you can put Camel XML routes in the classpath under the directory camel, which camel-spring-boot
will auto detect and include. From Camel version 2.17 onwards you can configure the directory name or disable this feature using the configuration option, as shown below:
// turn off camel.springboot.xmlRoutes = false // scan in the com/foo/routes classpath camel.springboot.xmlRoutes = classpath:com/foo/routes/*.xml
// turn off
camel.springboot.xmlRoutes = false
// scan in the com/foo/routes classpath
camel.springboot.xmlRoutes = classpath:com/foo/routes/*.xml
The XML files should be Camel XML routes and not CamelContext
such as:
5.13. Adding XML Rest-DSL 复制链接链接已复制到粘贴板!
By default, you can put Camel Rest-DSL XML routes in the classpath under the directory camel-rest
, which camel-spring-boot
will auto detect and include. You can configure the directory name or disable this feature using the configuration option, as shown below:
// turn off camel.springboot.xmlRests = false // scan in the com/foo/routes classpath camel.springboot.xmlRests = classpath:com/foo/rests/*.xml
// turn off
camel.springboot.xmlRests = false
// scan in the com/foo/routes classpath
camel.springboot.xmlRests = classpath:com/foo/rests/*.xml
The Rest-DSL XML files should be Camel XML rests and not CamelContext
such as: