Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes CAMEL-14975 (1 of 3) Add language docs moved from camel-k and infrastructure #315

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
36 changes: 36 additions & 0 deletions docs/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Camel K Documentation
=====================

== Environment Setup

To setup the environment you need to execute the following command once (and every time you change yarn dependencies):

```
yarn install
```

== Local development

To build the partial website from the local worktree, execute:

```
yarn build
```

== Build and preview

To build the partial website from the local worktree and preview the results, execute:

```
yarn preview
```

== xref check

To check the xrefs in the partial site, execute:

```
yarn checks
```

Valid xrefs to parts of the site in other sources will appear as (spurious) errors.
13 changes: 13 additions & 0 deletions docs/antora-playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
site:
title: Camel K Runtime Dev Playbook
url: https://camel.apache.org/
start_page: camel-k:languages:languages.adoc
content:
sources:
- url: ../
branches: HEAD
start_path: docs
ui:
bundle:
url: https://gitlab.com/antora/antora-ui-default/-/jobs/artifacts/master/raw/build/ui-bundle.zip?job=bundle-stable
snapshot: true
2 changes: 2 additions & 0 deletions docs/antora.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: camel-k
version: latest
6 changes: 6 additions & 0 deletions docs/modules/languages/nav-languages.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* xref:languages:languages.adoc[Languages]
** xref:languages:groovy.adoc[Groovy]
** xref:languages:kotlin.adoc[Kotlin]
** xref:languages:javascript.adoc[JavaScript]
** xref:languages:java.adoc[Java]
** xref:languages:xml.adoc[XML]
189 changes: 189 additions & 0 deletions docs/modules/languages/pages/groovy.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
= Writing Integrations in Groovy

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:

[source,groovy]
----
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
```

== Configuring the Application

Camel K extends the Camel Java DSL making it easier to configure Camel's behavior using the top level _camel_ block

[source,groovy]
----
camel {
// configure camel here
}
----

The _camel_ block allows to configure the following Camel features:

- **Components**
+
[source,groovy]
----
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
}
}
}
----
<1> configure the properties of the component whit name _seda_
<2> configure the properties of the component whit name _log_
<3> creates and configure a component of type `SedaComponent` whose name is _mySeda_
+
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
+
[source,groovy]
----
camel {
components {
log {
formatter {
'body ==> ' + it.in.body
}
}
}
}
----
+
which is much better.
+
[TIP]
====
You can provide your custom extensions by packaging them in a dependency you declare for your integration.
====

- **Languages **
+
[source,groovy]
----
camel {
languages {
language("bean") { // <1>
beanType = String.class
method = "toUpperCase"
}
myBean(BeanLanguage) { // <2>
beanType = String.class
method = "toLowerCase"
}
simple { // <3>
}
}
}
----
<1> configure the properties of the language whit name _bean_
<2> creates and configure a language of type `BeanLanguage` whose name is _myBean_
<3> configure the properties of the language whit name _simple_

- **DataFormats**
+
[source,groovy]
----
camel {
dataFormats {
dataFormat("json-jackson") { // <1>
unmarshalType = Map.class
prettyPrint = true
}
myJackson(JacksonDataFormat) { // <2>
unmarshalType = String.class
prettyPrint = false
}
csv { // <3>
}
}
}
----
<1> configure the properties of the data format whit name _json-jackson_
<2> creates and configure a data format of type `JacksonDataFormat` whose name is _myJackson_
<3> configure the properties of the data format whit name _csv_


== Beans

Beans can be bound to the _registry_ using a dedicated _bean DSL_ :

[source,groovy]
----
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 = ""
}
}
----
<1> define a bean
<2> define a custom processor
<3> define a custom predicate
<4> define a custom bean with name `dataSource` and type `org.apache.commons.dbcp2.BasicDataSource`


== Rest Support

Integrations's REST endpoints can be configured using the top level _rest_ block:

[source,groovy]
----
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'
}
}
----
<1> Configure the rest engine
<2> Configure the behavior of the method GET for the path '/my/path/get' and invoke the endpoint 'direct:get'
<3> Configure the behavior of the method POST for the path '/post' and invoke the endpoint 'direct:post'
25 changes: 25 additions & 0 deletions docs/modules/languages/pages/java.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
= Writing Integrations in Java

Using Java to write an integration to be deployed using Camel K is no 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.

[source,java]
.Example.java
----
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");
}
}
----

You can run it with the standard command:

```
kamel run Example.java
```
35 changes: 35 additions & 0 deletions docs/modules/languages/pages/javascript.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
= Writing Integrations in Javascript

An integration written in JavaScript looks very similar to a Java one:

[source,js]
.hello.js
----
const Processor = Java.extend(Java.type("org.apache.camel.Processor"));

function proc(e) {
e.getIn().setBody('Hello Camel K!')
}

from('timer:tick')
.process(new Processor(proc))
.to('log:info')
----

To run it, you need just to execute:

```
kamel run hello.js
```

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:

[source,js]
----

l = context.getComponent('log', true, false)
l.exchangeFormatter = function(e) {
return "log - body=" + e.in.body + ", headers=" + e.in.headers
}
----