Writing Integrations in Kotlin

The Kotlin DSL is experimental.

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")

You can run it with the standard command:

kamel run example.kts

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.

Beans DSL

To register beans as you would do with a Camel registry you can use the Beans DSL

beans {
    bean<org.apache.commons.dbcp2.BasicDataSource>("dataSource") { (1)
        driverClassName = "org.h2.Driver"
        url = "jdbc:h2:mem:camel"
        username = "sa"
        password = ""
    }

    bean("filterStrategy") { (1)
        org.apache.camel.support.DefaultHeaderFilterStrategy()
    }

    processor("myProcessor") { (2)
        it.getIn().body = "Hello"
    }

    predicate("myPredicate") { (3)
        false
    }
}
1 bind beans to the context for the database and filter strategy
2 define a custom processor to be used later in the routes by ref
3 define a custom predicate to be used later in the routes by ref

Components Configuration

Components can be configured within the components block inside the camel one:

camel {
    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
            }
        }
    }
}
1 configure the properties of a component whit type SedaComponent and name seda
2 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.
3 configure the properties of the component whit name log

As for Groovy, you can provide your custom extension to the DSL

Rest Endpoints

Integrations REST endpoints can be configured using the top level rest block:

rest {
    configuration { (1)
        host = "my-host"
        port = "9192"
    }

    path("/my/path") { (2)
        get("/get") {
            consumes("application/json")
            produces("application/json")
            to("direct:get")
        }
    }
}
1 Configure the rest engine
2 Configure the rest endpoint for the base path '/my/path'