An integration written in Groovy looks very similar to a Java one except it can leverages Groovy’s language enhancements over Java such as closures:
from('timer:tick')
.process { it.in.body = 'Hello Camel K!' }
.to('log:info')
You can run it with the standard command:
kamel run example.groovy
Camel K extends the Camel Java DSL making it easier to configure Camel’s behavior using the top level camel block
camel {
// configure camel here
}
The camel block allows to configure the following Camel features:
-
Components
camel { components { seda { // (1) queueSize = 1234 concurrentConsumers = 12 } log { // (2) exchangeFormatter = { 'body ==> ' + it.in.body } as org.apache.camel.spi.ExchangeFormatter } mySeda(SedaComponent) { // (3) queueSize = 4321 concurrentConsumers = 21 } } }
-
configure the properties of the component whit name seda
-
configure the properties of the component whit name log
-
creates and configure a component of type
SedaComponent
whose name is mySedaSetting 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 simplifies configuring the exchangeFormatter so you can rewrite your DSL as
camel { 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.
-
-
Languages
camel { languages { language("bean") { // (1) beanType = String.class method = "toUpperCase" } myBean(BeanLanguage) { // (2) beanType = String.class method = "toLowerCase" } simple { // (3) } } }
-
configure the properties of the language whit name bean
-
creates and configure a language of type
BeanLanguage
whose name is myBean -
configure the properties of the language whit name simple
-
-
DataFormats
camel { dataFormats { dataFormat("json-jackson") { // (1) unmarshalType = Map.class prettyPrint = true } myJackson(JacksonDataFormat) { // (2) unmarshalType = String.class prettyPrint = false } csv { // (3) } } }
-
configure the properties of the data format whit name json-jackson
-
creates and configure a data format of type
JacksonDataFormat
whose name is myJackson -
configure the properties of the data format whit name csv
-
Beans can be bound to the registry using a dedicated bean DSL :
beans {
myCache = Caffeine.newBuilder().build() // (1)
myProcessor = processor { // (2)
it.in.body = 'Hello Camel K!'
}
myPredicate = predicate { // (3)
it.in.body != null
}
dataSource(org.apache.commons.dbcp2.BasicDataSource) { //(4)
driverClassName = "org.h2.Driver"
url = "jdbc:h2:mem:camel"
username = "sa"
password = ""
}
}
-
define a bean
-
define a custom processor
-
define a custom predicate
-
define a custom bean with name
dataSource
and typeorg.apache.commons.dbcp2.BasicDataSource
Integrations REST endpoints can be configured using the top level rest block:
rest {
configuration { // (1)
host = 'my-host'
port '9192'
}
path('/my/path') {
get('/get') { // (2)
consumes 'application/json'
produces 'application/json'
to 'direct:get'
}
}
post { // (3)
path '/post'
consumes 'application/json'
produces 'application/json'
to 'direct:post'
}
}
-
Configure the rest engine
-
Configure the behavior of the method GET for the path '/my/path/get' and invoke the endpoint 'direct:get'
-
Configure the behavior of the method POST for the path '/post' and invoke the endpoint 'direct:post'