Skip to content

Instantly share code, notes, and snippets.

@rockavoldy
Last active July 5, 2023 23:46
Show Gist options
  • Save rockavoldy/99fd9c2a483810ea5ec04468dde3a605 to your computer and use it in GitHub Desktop.
Save rockavoldy/99fd9c2a483810ea5ec04468dde3a605 to your computer and use it in GitHub Desktop.

Apache Camel

Apache Camel is an integration framework that allows to integrate different systems using various protocols and technologies. By default, there already a lot of pre-built connectors, components, and processors that make it easy to integrate different systems and applications.

Apache Camel works by create a route to specify how messages should be processed and passed between endpoints. a route usually starts with source, and ends with a destination, then we can manipulate data between source and destination using pre-built processor that Camel has.

There is some ways to use Apache Camel, but in this documentation, we will use Camel K which deployed in kubernetes cluster, which we can easily interact with it using API, and it will treat a route as a dedicated pod.

Setup Camel K first

  1. Install Camel K First on a cluster, can use minikube, or k3d to create a cluster on top of docker
  2. Install Kamel, it's a client app that will be used to interfacing with Camel K in a Kubernetes cluster
  3. Test if it's work by running a "Hello World" for Integration
  4. If the log show like this, then it's good to go image

Create an integration

Camel uses a Java Domain Specific Language or DSL to create Routes, we can see the DSL that are fully supported by Camel here. But there is also some DSL that are still in experiment like JavaScript DSL, in this documentation, we're going to use JavaScript DSL.

  1. For starter, we can use timer component to run our integration with interval time, says to run it every 2 seconds, and log the result
    from('timer:tick?period=2000')
      .process((e) => {
          e.getIn().setBody('Hello Camel K!')
      })
      .to('log:info')
      .to('log:warning')
    and the result will be like this image
  2. We can use transform to manipulate the data inside process before it reached destination
    from('timer:tick?period=2000')
      .process((e) => {
          const data = ['Hello', 'Camel', 'K!']
          e.getIn().setBody(data)
      })
      .to('log:info-before-transform')
      .process((e) => {
          const body = e.getIn().getBody(Java.type('java.lang.String[]'))
          e.getIn().setBody(`Hai ${body[1]}`)
      })
      .to('log:info-after-transform')
    And we can see the result here, before transform is an array, and after transform, we only take second index to be printed image
  3. Using pre-built component to integrate with services
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment