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

Compare with Current View Page History

Version 1 Next »

Eclipse Kura component

This documentation page covers the integration options of Camel with the Eclipse Kura M2M gateway.

KuraRouter activator

The easiest way to deploy Apache Camel routes into the Kura is to create an OSGi bundle containing the class extending org.apache.camel.compKuraRouter class

public class MyKuraRouter extends KuraRouter {

    @Override
    public void configure() throws Exception {
        from("timer:trigger").
          to("netty-http:http://app.mydatacenter.com/api");
    }

}

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>com.example.MyFatJarRouter</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;
  }

}


  • No labels