Camel K supports integration written in the following languages:name: value
Language | Description |
---|---|
Java |
Both integrations in source |
XML |
Integrations written in plain XML DSL are supported (Spring XML or Blueprint not supported). |
Groovy |
Groovy |
JavaScript |
JavaScript |
Kotlin |
Kotlin Script |
Warning
|
Work In Progress |
Using Java to write an integration to be deployed using camel-k is not different from defining your routing rules in Camel with the only difference that you do not need to build and package it as a jar.
import org.apache.camel.builder.RouteBuilder;
public class Sample extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:tick")
.setBody()
.constant("Hello Camel K!")
.to("log:info");
}
}
Camel K support the standard Camel Routes XML DSL:
<routes xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="timer:tick"/>
<setBody>
<constant>Hello Camel K!</constant>
</setBody>
<to uri="log:info"/>
</route>
</routes>
An integration written in Groovy looks very similar to a Java one except it can leverages Groovy’s language enhancements over Java:
from('timer:tick')
.process { it.in.body = 'Hello Camel K!' }
.to('log:info')
Camel K extends the Camel Java DSL making it easier to configure the context in which the integration runs using the top level context block
context {
// configure the context here
}
At the moment the enhanced DSL provides a way to bind items to the registry, to configure the components the context creates and some improvements over the REST DSL.
-
Registry
The registry is accessible using the registry block inside the context one:
context { registry { bind "my-cache", Caffeine.newBuilder().build() // (1) bind "my-processor", processor { // (2) it.in.body = 'Hello Camel K!' } bind "my-predicate", predicate { // (3) it.in.body != null } } }
-
bind a bean to the context
-
define a custom processor to be used later in the routes by ref
-
define a custom predicate to be used later in the routes by ref
-
-
Components
Components can be configured within the components block inside the context one:
context { components { 'seda' { // (1) queueSize = 1234 concurrentConsumers = 12 } 'log' { // (2) exchangeFormatter = { 'body ==> ' + it.in.body } as org.apache.camel.spi.ExchangeFormatter } } }
-
configure the properties of the component whit name seda
-
configure the properties of the component whit name log
Setting the property exchangeFormatter looks a little ugly as you have to declare the type of your closure. For demonstration purpose we have created a Groovy extension module that simplify configuring the exchangeFormatter so you can rewrite your DSL as
context { components { ... 'log' { formatter { 'body ==> ' + it.in.body } } } }
which is much better.
TipYou can provide your custom extensions by packaging them in a dependency you declare for your integration.
-
-
Rest
Integrations’s REST endpoints can be configured using the top level rest block:
rest { configuration { // (1) host = 'my-host' port '9192' } path('/my/path') { // (2) // standard Rest DSL } }
-
Configure the rest engine
-
Configure the rest endpoint for the base path '/my/path'
-
An integration written in Kotlin looks very similar to a Java one except it can leverages Kotlin’s language enhancements over Java:
from('timer:tick')
.process { e -> e.getIn().body = 'Hello Camel K!' }
.to('log:info');
Camel K extends the Camel Java DSL making it easier to configure the context in which the integration runs using the top level context block
context {
// configure the context here
}
At the moment the enhanced DSL provides a way to bind items to the registry, to configure the components the context creates and some improvements over the REST DSL.
-
Registry
The registry is accessible using the registry block inside the context one:
context { registry { bind("my-cache", Caffeine.newBuilder().build()) // (1) bind("my-processor", processor { // (2) e -> e.getIn().body = "Hello" }) bind("my-predicate", predicate { // (2) e -> e.getIn().body != null }) } }
-
bind a simple bean to the context
-
define a custom processor to be used later in the routes by ref
-
define a custom predicate to be used later in the routes by ref
-
-
Components
Components can be configured within the components block inside the context one:
context { components { component<SedaComponent>("seda") { //(1) queueSize = 1234 concurrentConsumers = 12 } component<SedaComponent>("mySeda") { // (2) queueSize = 4321 concurrentConsumers = 21 } component<LogComponent>("log") { // (3) setExchangeFormatter { e: Exchange -> "" + e.getIn().body } } } }
-
configure the properties of a component whit type SedaComponent and name seda
-
configure the properties of a component with type SedaComponent and name mySeda, note that as mySeda does not represent a valid component scheme, a new component of the required type will be instantiated.
-
configure the properties of the component whit name log
NoteAs for Groovy, you can provide your custom extension to the DSL
-
-
Rest
Integrations’s REST endpoints can be configured using the top level rest block:
rest { configuration { host = "my-host" port = "9192" } path("/my/path") { // (2) // standard Rest DSL } }
-
Configure the rest engine
-
Configure the rest endpoint for the base path '/my/path'
-
The integration written in JavaScript looks very similar to a Java one:
function proc(e) {
e.getIn().setBody('Hello Camel K!')
}
from('timer:tick')
.process(proc)
.to('log:info')
For JavaScript integrations, Camel K does not yet provide an enhanced DSL but you can access to some global bounded objects such as a writable registry and the camel context so to set the property exchangeFormatter of the LogComponent as done in previous example, you can do something like:
l = context.getComponent('log', true, false)
l.exchangeFormatter = function(e) {
return "log - body=" + e.in.body + ", headers=" + e.in.headers
}