You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »

Spring Boot

Available as of Camel 2.15

Spring Boot component provide auto-configuration for the 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 pom.xml for this component:

<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 jar comes with the spring.factories file, so as soon as you add that dependency into your classpath, Spring Boot will automatically auto-configure the Camel for you.

Auto-configured Camel context

The most important piece of functionality provided by the Camel auto-configuration is CamelContext instance. Camel auto-configuration creates SpringCamelContext for your and take care of the proper initialization and shutdown of that context. Created Camel context is also registered in the Spring application context (under camelContext bean name), so you can access it just as the any other Spring bean.

@Configuration
public class MyAppConfig {

  @Autowired
  CamelContext camelContext;

  @Bean
  MyService myService() {
    return new DefaultMyService(camelContext);
  }

}

Auto-detecting Camel routes

Camel auto-configuration collects all the RoutesBuilder instances from the Spring context and automatically injects them into the provided CamelContext. It means that creating new Camel route with the Spring Boot starter is as simple as adding the @Component annotated class into your classpath:

@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("jms:invoices").to("file:/invoices");
  }

}


...or creating new route RoutesBuilder bean in your @Configuration class:

@Configuration
public class MyRouterConfiguration {

  @Bean
  RoutesBuilder myRouter() {
    return new RouteBuilder() {

      @Override
      public void configure() throws Exception {
        from("jms:invoices").to("file:/invoices");
      }

    };
  }
 
}

Camel properties

Spring Boot auto-configuration automatically connect 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 application.properties file:  

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 configuration

If you would like to perform some operations on CamelContext bean created by Camel auto-configuration, register CamelContextConfiguration instance in your Spring context:

@Configuration
public class MyAppConfig {

  ...

  @Bean
  CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
      @Override
      void beforeApplicationStart(CamelContext context) {
        // your custom configuration goes here
      }
    };
  }

}

Method CamelContextConfiguration#beforeApplicationStart(CamelContext) will be called just 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 - all of them will be executed.

Disabling JMX

To disable JMX of the auto-configured CamelContext use camel.springboot.jmxEnabled property (JMX is enabled by default). For example you could add the following property to your application.properties file:

camel.springboot.jmxEnabled = false

Auto-configured consumer and producer templates

Camel auto-configuration provides a pre-configured ConsumerTemplate and ProducerTemplate instances. You can simply inject them into your Spring-managed beans:

@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 and producer templates comes with the endpoint cache sizes equal to 1000. You can change that values via the following Spring properties:

camel.springboot.consumerTemplateCacheSize = 100
camel.springboot.producerTemplateCacheSize = 200

Auto-configured TypeConverter

Camel auto-configuration registers TypeConverter instance named typeConverter in the Spring context.

@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 bridge

Spring comes with the powerful type conversion API. Spring API happens not to be much different from the Camel type converter API. As those APIs are so similar, Camel Spring Boot automatically registers bridge converter (SpringTypeConverter) that delegates to the Spring conversion API. It basically means that out-of-the-box Camel will threat Spring Converters as Camel ones. With this approach you can enjoy both Camel and Spring converters accessed via Camel TypeConverter API:

@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 ConversionService instances available in the application context. If no ConversionService instance is available, Camel Spring Boot auto-configuration will create one for you.

Disabling type conversions features

If you don't want Camel Spring Boot to register type-conversions related features (like TypeConverter instance or Spring bridge) set the camel.springboot.typeConversion property to false.

camel.springboot.typeConversion = false

Fat jars and fat wars

The easiest way to create Camel-aware Spring Boot fat jar/war is to extend the org.apache.camel.spring.boot.FatJarRouter class...

 

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 application.properties file:

 

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  org.apache.camel.spring.boot.FatWarInitializer to your project:

package com.example;
 
... // imports

public class MyFatWarInitializer extends FatWarInitializer {


  @Override
  protected Class<? extends FatJarRouter> routerClass() {
    return MyFatJarRouter.class;
  }

}

Blocking main thread

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 Ctrl+C signal and doesn't exit immediately. If you would like to achieve similar behavior for non-FatJarRouter applications, retrieve CamelSpringBootApplicationController bean from your ApplicationContext and use the former to block the main thread of your application using CamelSpringBootApplicationController#blockMainThread() method.

public static void main(String... args) {
    ApplicationContext applicationContext = new SpringApplication(MyCamelApplication.class).run(args);
    CamelSpringBootApplicationController applicationController =
            applicationContext.getBean(CamelSpringBootApplicationController.class);
    applicationController.blockMainThread();
}


  • No labels