Search

Chapter 3. Getting started with Camel K

download PDF

This chapter explains how to set up your development environment and how to develop and deploy simple Camel K integrations written in Java, XML, and YAML. It also shows how to use the kamel command line to manage Camel K integrations at runtime. For example, this includes running, describing, logging, and deleting integrations,

3.1. Setting up your Camel K development environment

You must set up your environment with the recommended development tooling before you can automatically deploy the Camel K quick start tutorials. This section explains how to install the recommended Visual Studio (VS) Code IDE and the extensions that it provides for Camel K.

Note

VS Code is recommended for ease of use and the best developer experience of Camel K. This includes automatic completion of Camel DSL code and Camel K traits, and automatic execution of tutorial commands. However, you can manually enter your code and tutorial commands using your chosen IDE instead of VS Code.

Procedure

  1. Install VS Code on your development platform. For example, on Red Hat Enterprise Linux:

    1. Install the required key and repository:

      $ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
      $ sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'
    2. Update the cache and install the VS Code package:

      $ yum check-update
      $ sudo yum install code

      For details on installing on other platforms, see the VS Code installation documentation.

  2. Enter the code command to launch the VS Code editor. For more details, see the VS Code command line documentation.
  3. Install the VS Code Camel Extension Pack, which includes the extensions required for Camel K. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Apache Camel.
    3. Select the Extension Pack for Apache Camel by Red Hat, and click Install.

      Selecting the VS Code Extension Pack for Apache Camel by Red Hat

      For more details, see the instructions for the Extension Pack for Apache Camel by Red Hat.

  4. Install the VS Code Didact extension, which you can use to automatically run quick start tutorial commands by clicking links in the tutorial. For example, in VS Code:

    1. In the left navigation bar, click Extensions.
    2. In the search box, enter Didact.
    3. Select the extension, and click Install.

      For more details, see the instructions for the Didact extension.

3.2. Developing Camel K integrations in Java

This section shows how to develop a simple Camel K integration in Java DSL. Writing an integration in Java to be deployed using Camel K is the same as defining your routing rules in Camel. However, you do not need to build and package the integration as a JAR when using Camel K.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple Java integration file. For example:

    $ kamel init HelloCamelK.java
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the HelloCamelK.java integration automatically includes the Camel timer and log components to help you get started:

    // camel-k: language=java
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class HelloCamelK extends RouteBuilder {
      @Override
      public void configure() throws Exception {
    
          // Write your routes here, for example:
          from("timer:java?period=1s")
            .routeId("java")
            .setBody()
              .simple("Hello Camel K from ${routeId}")
            .to("log:info");
    
      }
    }

3.3. Developing Camel K integrations in XML

This section explains how to develop a simple Camel K integration in classic XML DSL. Writing an integration in XML to be deployed using Camel K is the same as defining your routing rules in Camel.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple XML integration file. For example:

    $ kamel init hello-camel-k.xml
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the hello-camel-k.xml integration automatically includes the Camel timer and log components to help you get started:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- camel-k: language=xml -->
    
    <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://camel.apache.org/schema/spring"
            xsi:schemaLocation="
                http://camel.apache.org/schema/spring
                http://camel.apache.org/schema/spring/camel-spring.xsd">
    
        <!-- Write your routes here, for example: -->
        <route id="xml">
            <from uri="timer:xml?period=1s"/>
            <setBody>
                <simple>Hello Camel K from ${routeId}</simple>
            </setBody>
            <to uri="log:info"/>
        </route>
    
    </routes>

3.4. Developing Camel K integrations in YAML

This section explains how to develop a simple Camel K integration in YAML DSL. Writing an integration in YAML to be deployed using Camel K is the same as defining your routing rules in Camel.

You can use any Camel component directly in your integration routes. Camel K automatically handles the dependency management and imports all the required libraries from the Camel catalog using code inspection.

Procedure

  1. Enter the kamel init command to generate a simple XML integration file. For example:

    $ kamel init hello.camelk.yaml
  2. Open the generated integration file in your IDE and edit as appropriate. For example, the hello.camelk.yaml integration automatically includes the Camel timer and log components to help you get started:

    # camel-k: language=yaml
    
    # Write your routes here, for example:
    - from:
        uri: "timer:yaml"
        parameters:
          period: "1s"
        steps:
          - set-body:
              constant: "Hello Camel K from yaml"
          - to: "log:info"
    Important

    Integrations written in YAML must have a file name with the pattern *.camelk.yaml or a first line of # camel-k: language=yaml.

3.5. Running Camel K integrations

You can run Camel K integrations in the cloud on your OpenShift cluster from the command line using the kamel run command.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command to run your integration in the cloud on OpenShift. For example:

    Java example

    $ kamel run HelloCamelK.java
    integration "hello-camel-k" created

    XML example

    $ kamel run hello-camel-k.xml
    integration "hello-camel-k" created

    YAML example

    $ kamel run hello.camelk.yaml
    integration "hello" created

  4. Enter the kamel get command to check the status of the integration:

    $ kamel get
    NAME       PHASE           KIT
    hello      Building Kit    kit-bq666mjej725sk8sn12g

    When the integration runs for the first time, Camel K builds the integration kit for the container image, which downloads all the required Camel modules and adds them to the image classpath.

  5. Enter kamel get again to verify that the integration is running:

    $ kamel get
    NAME       PHASE   KIT
    hello      Running kit-bq666mjej725sk8sn12g
  6. Enter the kamel log command to print the log to stdout:

    $ kamel log hello
    [1] 2020-04-11 14:26:43.449 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@5223e5ee executed in phase Starting
    [1] 2020-04-11 14:26:43.457 INFO  [main] RuntimeSupport - Looking up loader for language: yaml
    [1] 2020-04-11 14:26:43.655 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.yaml.YamlSourceLoader@1224144a for language yaml from service definition
    [1] 2020-04-11 14:26:43.658 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/hello.camelk.yaml?language=yaml
    [1] 2020-04-11 14:26:43.658 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@36c88a32 executed in phase ConfigureRoutes
    [1] 2020-04-11 14:26:43.661 INFO  [main] BaseMainSupport - Using properties from: file:/etc/camel/conf/application.properties
    [1] 2020-04-11 14:26:43.878 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@65466a6a executed in phase ConfigureContext
    [1] 2020-04-11 14:26:43.879 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) is starting
    [1] 2020-04-11 14:26:43.880 INFO  [main] DefaultManagementStrategy - JMX is disabled
    [1] 2020-04-11 14:26:44.147 INFO  [main] DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    [1] 2020-04-11 14:26:44.157 INFO  [main] DefaultCamelContext - Route: route1 started and consuming from: timer://yaml?period=1s
    [1] 2020-04-11 14:26:44.161 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [1] 2020-04-11 14:26:44.162 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.283 seconds
    [1] 2020-04-11 14:26:44.163 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@1c93084c executed in phase Started
    [1] 2020-04-11 14:26:45.183 INFO  [Camel (camel-k) thread #1 - timer://yaml] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from yaml]
    ...
  7. Press Ctrl-C to terminate logging in the terminal.

Additional resources

3.6. Running Camel K integrations in development mode

You can run Camel K integrations in development mode on your OpenShift cluster from the command line. Using development mode, you can iterate quickly on integrations in development and get fast feedback on your code.

When you specify the kamel run command with the --dev option, this deploys the integration in the cloud immediately and shows the integration logs in the terminal. You can then change the code and see the changes automatically applied instantly to the remote integration Pod on OpenShift. The terminal automatically displays all redeployments of the remote integration in the cloud.

Note

The artifacts generated by Camel K in development mode are identical to those that you run in production. The purpose of development mode is faster development.

Prerequisites

Procedure

  1. Log into your OpenShift cluster using the oc client tool, for example:

    $ oc login --token=my-token --server=https://my-cluster.example.com:6443
  2. Ensure that the Camel K Operator is running, for example:

    $ oc get pod
    NAME                               READY   STATUS    RESTARTS   AGE
    camel-k-operator-86b8d94b4-pk7d6   1/1     Running   0          6m28s
  3. Enter the kamel run command with --dev to run your integration in development mode on OpenShift in the cloud. The following shows a simple Java example:

    $ kamel run HelloCamelK.java --dev
    integration "hello-camel-k" created
    Progress: integration "hello-camel-k" in phase Initialization
    Progress: integration "hello-camel-k" in phase Building Kit
    Progress: integration "hello-camel-k" in phase Deploying
    Progress: integration "hello-camel-k" in phase Running
    IntegrationPlatformAvailable for Integration hello-camel-k: camel-k
    Integration hello-camel-k in phase Initialization
    No IntegrationKitAvailable for Integration hello-camel-k: creating a new integration kit
    Integration hello-camel-k in phase Building Kit
    IntegrationKitAvailable for Integration hello-camel-k: kit-bq8t5cudeam3u3sj13tg
    Integration hello-camel-k in phase Deploying
    No CronJobAvailable for Integration hello-camel-k: different controller strategy used (deployment)
    DeploymentAvailable for Integration hello-camel-k: deployment name is hello-camel-k
    No ServiceAvailable for Integration hello-camel-k: no http service required
    No ExposureAvailable for Integration hello-camel-k: no target service found
    Integration hello-camel-k in phase Running
    [2] Monitoring pod hello-camel-k-866ccb5976-sjh8x[1] Monitoring pod hello-camel-k-866ccb5976-l288p[2] 2020-04-11 14:44:53.691 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.ContextConfigurer@159f197
    [2] 2020-04-11 14:44:53.694 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesConfigurer@1f0f1111
    [2] 2020-04-11 14:44:53.695 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesDumper@6e0dec4a
    [2] 2020-04-11 14:44:53.695 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.PropertiesFunctionsConfigurer@794cb805
    [2] 2020-04-11 14:44:53.712 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@794cb805 executed in phase Starting
    [2] 2020-04-11 14:44:53.721 INFO  [main] RuntimeSupport - Looking up loader for language: java
    [2] 2020-04-11 14:44:53.723 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.java.JavaSourceLoader@3911c2a7 for language java from service definition
    [2] 2020-04-11 14:44:54.220 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/HelloCamelK.java?language=java
    [2] 2020-04-11 14:44:54.220 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@1f0f1111 executed in phase ConfigureRoutes
    [2] 2020-04-11 14:44:54.224 INFO  [main] BaseMainSupport - Using properties from: file:/etc/camel/conf/application.properties
    [2] 2020-04-11 14:44:54.385 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@159f197 executed in phase ConfigureContext
    [2] 2020-04-11 14:44:54.386 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) is starting
    [2] 2020-04-11 14:44:54.387 INFO  [main] DefaultManagementStrategy - JMX is disabled
    [2] 2020-04-11 14:44:54.630 INFO  [main] DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    [2] 2020-04-11 14:44:54.639 INFO  [main] DefaultCamelContext - Route: java started and consuming from: timer://java?period=1s
    [2] 2020-04-11 14:44:54.643 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [2] 2020-04-11 14:44:54.643 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.258 seconds
    [2] 2020-04-11 14:44:54.644 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@6e0dec4a executed in phase Started
    [2] 2020-04-11 14:44:55.671 INFO  [Camel (camel-k) thread #1 - timer://java] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Hello Camel K from java]
    ...
  4. Edit the content of your integration DSL file, save your changes, and see the changes displayed instantly in the terminal. For example:

    ...
    integration "hello-camel-k" updated
    ...
    [3] 2020-04-11 14:45:06.792 INFO  [main] DefaultCamelContext - Route: java started and consuming from: timer://java?period=1s
    [3] 2020-04-11 14:45:06.795 INFO  [main] DefaultCamelContext - Total 1 routes, of which 1 are started
    [3] 2020-04-11 14:45:06.796 INFO  [main] DefaultCamelContext - Apache Camel 3.0.1 (CamelContext: camel-k) started in 0.323 seconds
    [3] 2020-04-11 14:45:06.796 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@6e0dec4a executed in phase Started
    [3] 2020-04-11 14:45:07.826 INFO  [Camel (camel-k) thread #1 - timer://java] info - Exchange[ExchangePattern: InOnly, BodyType: String, Body: Ciao Camel K from java]
    ...
  5. Press Ctrl-C to terminate logging in the terminal.

Additional resources

3.7. Running Camel K integrations using modeline

You can use the Camel K modeline to specify multiple configuration options in a Camel K integration source file, which are executed at runtime. This creates efficiencies by saving you the time of re-entering multiple command line options and helps to prevent input errors.

The following example shows a modeline entry from a Java integration file that configures traits for Prometheus monitoring and 3scale API Management, and includes a dependency on an external Maven library:

// camel-k: language=java trait=prometheus.enabled=true trait=3scale.enabled=true dependency=mvn:org.my/app:1.0

Prerequisites

Procedure

  1. Add a Camel K modeline entry to your integration file. For example:

    Hello.java

    // camel-k: language=java trait=prometheus.enabled=true trait=3scale.enabled=true dependency=mvn:org.my/application:1.0 1
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class Hello extends RouteBuilder {
      @Override
      public void configure() throws Exception {
    
          from("timer:java?period=1000")
            .bean(org.my.BusinessLogic) 2
            .log("${body}");
    
      }
    }

    1
    The modeline entry enables monitoring in Prometheus, API management with 3scale, and specifies a dependency on an external Maven library.
    2
    This bean uses a business logic class from the external Maven library configured in the modeline.
  2. Enter the following command to run the integration:

    $ kamel run Hello.java
    Modeline options have been loaded from source files
    Full command: kamel run Hello.java --trait=prometheus.enabled=true --dependency mvn:org.my/application:1.0

    The kamel run command outputs any modeline options specified in the integration.

Additional resources

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.