Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
Spring BootAvailable as of Camel 2.15 Spring Boot component provides auto-configuration for Apache Camel. Our opinionated auto-configuration of the Camel context auto-detects Camel routes available in the Spring context and registers the key Camel utilities (like producer template, consumer template and the type converter) as beans. Maven users will need to add the following dependency to their <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot</artifactId> <version>${camel.version}</version> <!-- use the same version as your Camel core version --> </dependency>
Camel Spring Boot StarterAvailable as of Camel 2.17 Apache Camel ships a Spring Boot Starter module that allows you to develop Spring Boot applications using starters. There is a sample application in the source code also. To use the starter, add the following to your spring boot <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>2.17.0</version> </dependency> Then you can just add classes with your Camel routes such as: package com.example; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class MyRoute extends RouteBuilder { @Override public void configure() throws Exception { from("timer:foo") .to("log:bar"); } } Then these routes will be started automatically. To keep the main thread blocked so that Camel stays up, either include the You can further customize the Camel application in the Auto-Configured Camel ContextThe most important piece of functionality provided by the Camel auto-configuration is @Configuration public class MyAppConfig { @Autowired CamelContext camelContext; @Bean MyService myService() { return new DefaultMyService(camelContext); } } Auto-Detecting Camel RoutesCamel auto-configuration collects all the @Component public class MyRouter extends RouteBuilder { @Override public void configure() throws Exception { from("jms:invoices").to("file:/invoices"); } }
@Configuration public class MyRouterConfiguration { @Bean RoutesBuilder myRouter() { return new RouteBuilder() { @Override public void configure() throws Exception { from("jms:invoices") .to("file:/invoices"); } }; } } Camel propertiesSpring Boot auto-configuration automatically connects to Spring Boot external configuration (like properties placeholders, OS environment variables or system properties) with the Camel properties support. It basically means that any property defined in route.from = jms:invoices ...or set via system property... java -Droute.to=jms:processed.invoices -jar mySpringApp.jar ...can be used as placeholders in Camel route: @Component public class MyRouter extends RouteBuilder { @Override public void configure() throws Exception { from("{{route.from}}") .to("{{route.to}}"); } } Custom Camel Context ConfigurationIf you would like to perform some operations on @Configuration public class MyAppConfig { ... @Bean CamelContextConfiguration contextConfiguration() { return new CamelContextConfiguration() { @Override void beforeApplicationStart(CamelContext context) { // your custom configuration goes here } }; } } Method C Disabling JMXTo disable JMX of the auto-configured camel.springboot.jmxEnabled = false Auto-Configured Consumer and Producer TemplatesCamel auto-configuration provides pre-configured @Component public class InvoiceProcessor { @Autowired private ProducerTemplate producerTemplate; @Autowired private ConsumerTemplate consumerTemplate; public void processNextInvoice() { Invoice invoice = consumerTemplate.receiveBody("jms:invoices", Invoice.class); ... producerTemplate.sendBody("netty-http:http://invoicing.com/received/" + invoice.id()); } } By default consumer templates and producer templates come with the endpoint cache sizes set to camel.springboot.consumerTemplateCacheSize = 100 camel.springboot.producerTemplateCacheSize = 200 Auto-Configured TypeConverterCamel auto-configuration registers a @Component public class InvoiceProcessor { @Autowired private TypeConverter typeConverter; public long parseInvoiceValue(Invoice invoice) { String invoiceValue = invoice.grossValue(); return typeConverter.convertTo(Long.class, invoiceValue); } } Spring Type Conversion API BridgeSpring comes with the powerful type conversion API. Spring API happens to be very similar to the Camel type converter API. As those APIs are so similar, Camel Spring Boot automatically registers a bridge converter ( @Component public class InvoiceProcessor { @Autowired private TypeConverter typeConverter; public UUID parseInvoiceId(Invoice invoice) { // Using Spring's StringToUUIDConverter UUID id = invoice.typeConverter.convertTo(UUID.class, invoice.getId()); } }
Under the hood Camel Spring Boot delegates conversion to the Spring's Disabling Type Conversions FeaturesIf you don't want Camel Spring Boot to register type-conversions related features (like camel.springboot.typeConversion = false Fat Jars and Fat WarsThe easiest way to create a Camel-aware Spring Boot fat jar/war is to extend the
package com.example; ... // imports @SpringBootApplication public class MyFatJarRouter extends FatJarRouter { @Override public void configure() throws Exception { from("netty-http:http://0.0.0.0:18080"). setBody().simple("ref:helloWorld"); } @Bean String helloWorld() { return "helloWorld"; } }
...and add the following property to your
spring.main.sources = com.example.MyFatJarRouter It is also recommended to define your main class explicitly in the Spring Boot Maven plugin configuration: <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>org.apache.camel.spring.boot.FatJarRouter</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> In order to turn your fat jar into fat war, add the following class extending package com.example; ... // imports public class MyFatJarRouterWarInitializer extends FatWarInitializer { @Override protected Class<? extends FatJarRouter> routerClass() { return MyFatJarRouter.class; } } Blocking Main ThreadThis feature is available starting from Camel 2.15.2. Camel applications extending FatJarRouter by default block the main thread of the application. It means that after you start your fat jar, your application waits for public static void main(String... args) { ApplicationContext applicationContext = new SpringApplication(MyCamelApplication.class).run(args); CamelSpringBootApplicationController applicationController = applicationContext.getBean(CamelSpringBootApplicationController.class); applicationController.blockMainThread(); } Adding XML RoutesBy 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 2.17: you can configure the directory name or turn this off using the configuration option: // 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 (not CamelContext) such as <routes xmlns="http://camel.apache.org/schema/spring"> <route id="test"> <from uri="timer://trigger"/> <transform> <simple>ref:myBean</simple> </transform> <to uri="log:out"/> </route> </routes> Adding Rest-DSLAvailable since Camel 2.18 By default you can put Camel Rest-DSL XML routes in the classpath under the directory
You can configure the directory name or turn this off using the configuration option: // 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 (not CamelContext) such as <rests xmlns="http://camel.apache.org/schema/spring"> <rest> <post uri="/persons"> <to uri="direct:postPersons"/> </post> <get uri="/persons"> <to uri="direct:getPersons"/> </get> <get uri="/persons/{personId}"> <to uri="direct:getPersionId"/> </get> <put uri="/persons/{personId}"> <to uri="direct:putPersionId"/> </put> <delete uri="/persons/{personId}"> <to uri="direct:deletePersionId"/> </delete> </rest> </rests> Unit TestsBelow is a sample unit test set up for camel spring-boot. @ActiveProfiles("test") @RunWith(CamelSpringBootRunner.class) @SpringBootTest @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @DisableJmx(true) public class MyRouteTest extends CamelTestSupport { @Autowired private CamelContext camelContext; @Override protected CamelContext createCamelContext() throws Exception { return camelContext; } @EndpointInject(uri = "direct:myEndpoint") private ProducerTemplate endpoint; @Override public void setUp() throws Exception { super.setUp(); RouteDefinition definition = context().getRouteDefinitions().get(0); definition.adviceWith(context(), new RouteBuilder() { @Override public void configure() throws Exception { onException(Exception.class).maximumRedeliveries(0); } }); } @Override public String isMockEndpointsAndSkip() { return "myEndpoint:put*"; } @Test public void shouldSucceed() throws Exception { assertNotNull(camelContext); assertNotNull(endpoint); String expectedValue = "expectedValue"; MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put"); mock.expectedMessageCount(1); mock.allMessages().body().isEqualTo(expectedValue); mock.allMessages().header(MY_HEADER).isEqualTo("testHeader"); endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader"); mock.assertIsSatisfied(); } } |