| = Writing Integrations in Kotlin |
| |
| An integration written in Kotlin looks very similar to a Java one except it can leverages Kotlin's language enhancements over Java: |
| |
| [source,kotlin] |
| ---- |
| 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 |
| |
| [source,kotlin] |
| ---- |
| 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 Configuration |
| |
| The registry is accessible using the _registry_ block inside the _context_ one: |
| |
| [source,kotlin] |
| ---- |
| context { |
| registry { |
| bind("my-cache", Caffeine.newBuilder().build()) // <1> |
| bind("my-processor", processor { // <2> |
| e -> e.getIn().body = "Hello" |
| }) |
| bind("my-predicate", predicate { // <3> |
| e -> e.getIn().body != null |
| }) |
| } |
| } |
| ---- |
| <1> bind a simple bean to the context |
| <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 _context_ one: |
| |
| [source,kotlin] |
| ---- |
| 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 |
| } |
| } |
| } |
| } |
| ---- |
| <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_ |
| |
| [NOTE] |
| ==== |
| As for Groovy, you can provide your custom extension to the DSL |
| ==== |
| |
| == Rest Endpoints |
| |
| Integrations's REST endpoints can be configured using the top level _rest_ block: |
| |
| [source,kotlin] |
| ---- |
| rest { |
| configuration { // <1> |
| host = "my-host" |
| port = "9192" |
| } |
| |
| path("/my/path") { // <2> |
| // standard Rest DSL |
| } |
| } |
| ---- |
| <1> Configure the rest engine |
| <2> Configure the rest endpoint for the base path '/my/path' |