Eclipse Kura component
Info |
---|
Kura component is available starting from Camel 2.15. |
This documentation page covers the integration options of Camel with the Eclipse Kura M2M gateway. The common reason to deploy Camel routes into the Eclipse Kura is to provide enterprise integration patterns and Camel components to the messaging M2M gateway. For example you might want to install Kura on Raspberry PI, then read temperature from the sensor attached to that Raspberry PI using Kura services and finally forward the current temperature value to your data center service using Camel EIP and components.
KuraRouter activator
The Bundles deployed to the Eclipse Kura are usually developed as bundle activators. So 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 kura.KuraRouter
class:
Code Block | ||
---|---|---|
| ||
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:
Code Block | ||
---|---|---|
| ||
@Component
public class MyRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("jms:invoices").to("file:/invoices");
}
} |
...
Code Block | ||
---|---|---|
| ||
@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
Keep in mind that KuraRouter
implements the org.osgi.framework.BundleActivator
interface, so you need to register its start
and stop
lifecycle methods while creating Kura bundle component class.
Kura router starts its own OSGi-aware CamelContext
. It means that for every class extending KuraRouter
, there will be a dedicated CamelContext
instance. Ideally we recommend to deploy one KuraRouter
per OSGi bundle.
Deploying KuraRouter
Bundle containing your Kura router class should import the following packages in the OSGi manifest:
Code Block | ||||
---|---|---|---|---|
| ||||
Import-Package: org.osgi.framework;version="1.3.0",
org.slf4j;version="1.6.4",
org.apache.camel,org.apache.camel.impl,org.apache.camel.core.osgi,org.apache.camel.builder,org.apache.camel.model,
org.apache.camel.component.kura |
Keep in mind that you don't have to import every Camel component bundle you plan to use in your routes, as Camel components are resolved as the services on the runtime level.
Before you deploy your router bundle, be sure that you have deployed (and started) the following Camel core bundles (using Kura GoGo shell)...
Code Block | ||||
---|---|---|---|---|
| ||||
install file:///home/user/.m2/repository/org/apache/camel/camel-core/2.15.0/camel-core-2.15.0.jar
start <camel-core-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-core-osgi/2.15.0/camel-core-osgi-2.15.0.jar
start <camel-core-osgi-bundle-id>
install file:///home/user/.m2/repository/org/apache/camel/camel-kura/2.15.0/camel-kura-2.15.0.jar
start <camel-kura-bundle-id> |
...and all the components you plan to use in your routes:
Code Block | ||||
---|---|---|---|---|
| ||||
install file:///home/user/.m2/repository/org/apache/camel/camel-stream/2.15.0/camel-stream-2.15.0.jar
start <camel-stream-bundle-id> |
Then finally deploy your router bundle:
Code Block | ||||
---|---|---|---|---|
| ||||
install file:///home/user/.m2/repository/com/example/myrouter/1.0/myrouter-1.0.jar
start <your-bundle-id> |
KuraRouter utilities
Kura router base class provides many useful utilities. This section explores each of them.
SLF4J logger
Kura uses SLF4J facade for logging purposes. Protected member log
returns SLF4J logger instance associated with the given Kura router.
Code Block | ||
---|---|---|
| ||
public class MyKuraRouter extends KuraRouter {
@Override
|
...can be used as placeholders in Camel route:
Code Block | ||
---|---|---|
| ||
@Component public class MyRouter extends RouteBuilder { @Override public void configure() throws Exception { log.info("Configuring Camel routes!"); 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:
BundleContext
Protected member bundleContext
returns bundle context associated with the given Kura router.
Code Block | ||
---|---|---|
| ||
@Configuration public class MyKuraRouter extends MyAppConfigKuraRouter { ...@Override @Bean public CamelContextConfigurationvoid contextConfigurationconfigure() throws Exception { ServiceReference<MyService> serviceRef = return new CamelContextConfiguration() { bundleContext.getServiceReference(LogService.class.getName()); MyService myService = bundleContext.getService(serviceRef); @Override... } } |
CamelContext
Protected member camelContext
is the CamelContext
associated with the given Kura router.
Code Block | ||
---|---|---|
| ||
public class voidMyKuraRouter beforeApplicationStart(CamelContext context)extends KuraRouter { @Override //public yourvoid customconfigure() configurationthrows goes hereException { camelContext.getStatus(); } ... }; } } |
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
ProducerTemplate
Protected member producerTemplate
is the ProducerTemplate
instance associated with the given Camel context.
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:
Code Block | ||
---|---|---|
| ||
@Component public class InvoiceProcessorMyKuraRouter extends KuraRouter { @Autowired private ProducerTemplate producerTemplate; @Autowired private ConsumerTemplate consumerTemplate; @Override public void processNextInvoiceconfigure() throws Exception { Invoice invoice = consumerTemplate.receiveBody producerTemplate.sendBody("jms:invoicestemperature", Invoice22.class0); ... 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
ConsumerTemplate
Protected member consumerTemplate
is the ConsumerTemplate
instance associated with the given Camel
Auto-configured TypeConverter
Camel auto-configuration registers TypeConverter
instance named typeConverter
in the Spring context.
Code Block | ||
---|---|---|
| ||
@Component public class MyKuraRouter extends InvoiceProcessorKuraRouter { @Autowired private@Override TypeConverter typeConverter; public longvoid parseInvoiceValueconfigure(Invoice invoice)) throws Exception { String invoiceValue double currentTemperature = invoiceproducerTemplate.grossValue(receiveBody("jms:temperature", Double.class); return typeConverter..convertTo(Long.class, invoiceValue); . } } |
Spring type conversion API bridge
OSGi service resolver
OSGi service resolver (service(Class<T> serviceType)
) can be used to easily retrieve service by type from the OSGi bundle context.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:
Code Block | ||
---|---|---|
| ||
@Component public class MyKuraRouter InvoiceProcessorextends KuraRouter { @Autowired private@Override TypeConverter typeConverter; public UUIDvoid parseInvoiceIdconfigure(Invoice invoice)) throws Exception { MyService myService // Using Spring's StringToUUIDConverter = service(MyService.class); 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
If service is not found, a null
value is returned. If you want your application to fail if the service is not available, use requiredService(Class)
method instead. The requiredService
throws IllegalStateException
if a service cannot be found.
Code Block | ||
---|---|---|
| ||
public class MyKuraRouter extends KuraRouter |
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.F
class...atJarRouter
Code Block | ||
---|---|---|
| ||
package com.example; ... // imports @SpringBootApplication public class MyFatJarRouter extends FatJarRouter { @Override public void configure() throws Exception { MyService myService from("netty-http:http://0.0.0.0:18080").= requiredService(MyService.class); ... 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
}
} |
KuraRouter activator callbacks
Kura router comes with the lifecycle callbacks that can be used to customize the way the Camel router works. For example to configure the CamelContext
instance associated with the router just before the former is started, override beforeStart
method of the KuraRouter
class:
Code Block | ||
---|---|---|
| ||
public class MyKuraRouter extends KuraRouter {
...
protected void beforeStart(CamelContext camelContext) {
OsgiDefaultCamelContext osgiContext = (OsgiCamelContext) camelContext;
osgiContext.setName("NameOfTheRouter");
}
} |
Loading XML routes from ConfigurationAdmin
Sometimes it is desired to read the XML definition of the routes from the server configuration. This a common scenario for IoT gateways where over-the-air redeployment cost may be significant. To address this requirement each KuraRouter
looks for the kura.camel.BUNDLE-SYMBOLIC-NAME.route
property from the kura.camel
PID using the OSGi ConfigurationAdmin. This approach allows you to define Camel XML routes file per deployed KuraRouter
. In order to update a route, just edit an appropriate configuration property and restart a bundle associated with it. The content of the kura.camel.BUNDLE-SYMBOLIC-NAME.route
property is expected to be Camel XML route file, for example:
Code Block |
---|
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="loaded"> |
It is also recommended to define your main class explicitly in the Spring Boot Maven plugin configuration:
Code Block | ||
---|---|---|
xml | xml | <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><from uri="direct:bar"/> </goals> </execution><to uri="mock:bar"/> </executions>route> </plugin> |
In order to turn your fat jar into fat war, add the following class extending org.apache.camel.spring.boot.F
to your project:atWarInitializer
routes> |
Deploying Kura router as a declarative OSGi service
If you would like to deploy your Kura router as a declarative OSGi service, you can use activate
and deactivate
methods provided by KuraRouter
.
Code Block | ||
---|---|---|
<scr:component name="org.eclipse.kura.example.camel.MyKuraRouter" activate="activate" deactivate="deactivate" enabled="true" immediate="true">
<implementation class="org.eclipse.kura.example.camel.MyKuraRouter"/>
</scr:component> | ||
Code Block | ||
| ||
package com.example;
... // imports
public class MyFatWarInitializer extends FatWarInitializer {
@Override
protected Class<? extends FatJarRouter> routerClass() {
return MyFatJarRouter.class;
}
} |
Include Page | ||||
---|---|---|---|---|
|