What’s all this integration agitation, anyway?
Tags: freedom, integration
Published: 2020-10-06 18:11:03

---
title: What’s all this integration agitation, anyway?
published: true
date: 2020-10-06 18:05:00 UTC
tags: Freedom,Integration
canonical_url: http://delawen.com/2020/10/what-is-integration/
---

You may have heard any or all of these keywords before: middleware, integration, orchestration. And you may be wondering why and when to use them. Take a walk with me to understand when and how integration frameworks are useful.

Imagine you are in charge of solving a new need of your company. There is no complete software stack for what you need. You will have to involve your team to create something new. Even if you reuse some components, you have to make them interact and talk to each other.

You are an experienced software engineer and have solved previously many of the requirements with some components you are already familiar with. But now you have to orchestrate all these components together and make them work like a clock. Now you need a proper <em>integration.</em> You want all of them to cooperate smoothly in your architecture.

The first thing any good developer thinks about is building a custom software that acts as the glue between all these components. Maybe adding some fancy extra functionality. And, (why not?) as we are at the beginning of a new exciting project, probably we want to try all these new technologies you have been reading and hearing about. Whatever the fad buzzword is now, you are willing to try it.

Although this may be appealing, your inner experienced engineer tells you to stop. There&#8217;s <a rel="noreferrer noopener nofollow" href="https://www.freecodecamp.org/news/what-is-middleware-with-example-use-cases/" target="_blank">something</a> you also read about, these<a rel="noreferrer noopener nofollow" href="https://dzone.com/articles/when-use-apache-camel" target="_blank"> integration frameworks</a>. Could they be useful here?

## The Integration Paradigm

As much as we would like to start a new clean project from scratch and throw all our ingeniousness into it, we shouldn&#8217;t reinvent the wheel. Let&#8217;s take a look at what is this middleware or integration software.

Middleware, or integration software, can help us orchestrate and automate the interaction between different applications, APIs, third party services or any other software piece we may have to connect.

A proper integration tool should provide us with the following features: transformation, integration patterns and connectors to existing protocols and components.

### Transformations

When we connect different components of an architecture, they rarely speak the same languages or, on this case, data formats. Some components will output an xml that has to be fed to the following component on a json form. Maybe we even need to add or remove some attributes on that json data.

We need some way to easily transform the data traveling from one component to the following so it fits properly.

If we want to do this with our own script, there are many libraries that can help us doing this like <a href="http://You are already an experienced engineer and have solved previously many of the requirements with some components you are already familiar with. But now you have to orchestrate all these components together and make them work like a clock. Now we need a proper integration." target="_blank" rel="noreferrer noopener">Jackson</a> or the <a href="https://docs.python.org/3/library/xml.html" target="_blank" rel="noreferrer noopener">built-in xml libraries on Python</a> . We even have the <a href="https://www.w3schools.com/xml/xsl_client.asp" target="_blank" rel="noreferrer noopener">XSLT</a> language to transform XML. But to use any of these properly, we would have to learn first how to use them. And any code we generate will have to be maintained and upgraded properly.

An integration framework allows us to define what is the mapping between the output of one component and the input of the following so we can forget about the explicit implementation. The less code we have to maintain, the better.

### Enterprise Integration Patterns

Not all workflows in the architecture will be lineal. Some of the steps will require broadcasting, some of them will require conditional flowing. Some will require waiting the output of different components to conflate the data. These action patterns are something that have been studied for a long time. And as with software development patterns, you can classify them and study them to create better integrations.

You can find all of these patterns prettily explained in the classic <a rel="noreferrer noopener" href="https://www.enterpriseintegrationpatterns.com/" target="_blank">Enterprise Integration Patterns</a> book.

### Connectors

All of the above is useless if we can&#8217;t connect to (and from) the specific component we need. 

Our ideal integration framework should offer support for common protocols like ftp, http, jdbc,&#8230; Also it should offer support to connect to common components like a mail server, messaging services, atom,&#8230; We could claim even that no integration tool would be good if it doesn&#8217;t also support specific well known services like being able to send a message through a Telegram bot or store information on Elastic Search.

Being able to seamlessly connect from one component to the next without having to worry about the specifics of their interfaces is what distinguishes an average integration tool from a good integration tool.

## Apache Camel

Let&#8217;s talk about something less abstract. At this point you may be wondering where you can find a good integration framework.

<a href="https://camel.apache.org/" target="_blank" rel="noreferrer noopener">Apache Camel</a> is not only one of the most active projects inside the Apache Software Foundation, it is also the lightest and most complete integration framework available. And on top of it, it is also <strong>Free and Open Source Software</strong>!

Camel is already an old actor on the integration world. It has support for hundreds of components, protocols and formats. Some of these protocols come very handy allowing the user, for example, to connect to any REST API that they need. 

Camel uses its own DSL, a simplified language to define easily the workflows step by step. 

### Camel-K

Camel is also available in Knative. This means, we can use it on a serverless environment, making sure the orchestration between services runs and escalates properly.

# Camel K Orchestration Example

This example demonstrates how to orchestrate integrations using Camel-K and <a href="https://kafka.apache.org/" target="_blank" rel="noreferrer noopener">Kafka</a> as a messaging service. We are going to implement two integrations that interact through a database to simulate how cat adoptions work. 

<a href="https://github.com/openshift-integration/camel-k-example-orchestration" target="_blank" rel="noreferrer noopener">The full example can be found on Github.</a>

<figure class="wp-block-image"><a href="https://github.com/openshift-integration/camel-k-example-orchestration/blob/master/images/flux_diagram.svg" target="_blank" rel="noreferrer noopener"><img src="https://github.com/openshift-integration/camel-k-example-orchestration/raw/master/images/flux_diagram.svg" alt="Flux diagram"/></a><figcaption>Two integration workflows that simulate how cat adoptions work</figcaption></figure>

One integration will store cats coming from Kafka to the database waiting for a person to adopt them. The second integration will receive people interested in adopting and will match cats with them.

### Cat Input from Kafka to Database

First we are going to implement the storage of cat input messages to the database. 

As you can see, the Camel DSL is very intuitive: this integration listens to the proper Kafka broker and for every message that arrives, it unmarshalls the json to extract the data and pushes it to the database. The Cat class is just a simple bean with getters and setters for the attributes.


<pre class="brush: java; title: ; notranslate">
// camel-k: language=java

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;
import model.Cat;

public class CatInput extends RouteBuilder {
  @Override
  public void configure() throws Exception {

    //Listen to kafka cat broker
    from(&quot;kafka:cat?brokers=my-cluster-kafka-bootstrap:9092&quot;)
    .log(&quot;Message received from Kafka : ${body}&quot;)
    .unmarshal().json(JsonLibrary.Gson, Cat.class)
  
    //Store it on the database with a null person
    .setBody().simple(&quot;INSERT INTO cat (name, image) VALUES ('${body.name}', '${body.image}')&quot;)
    .to(&quot;jdbc:postgresBean?&quot;)
  
    //Write some log to know it finishes properly
    .log(&quot;Cat stored.&quot;);}
  }
}
</pre>


### Person Input from Kafka to Adopt

Now we are going to implement the reception of people wanting to adopt a cat.

This integration is a bit more complex, as we are going to introduce a conditional choice: if there is a cat available on the database, it will be assigned to the person. If there is no cat (otherwise), a message will be returned saying no cat is available.


<pre class="brush: java; title: ; notranslate">
// camel-k: language=java

import org.apache.camel.builder.RouteBuilder;

public class PersonInput extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    //Listen to kafka person broker
    from(&quot;kafka:person?brokers=my-cluster-kafka-bootstrap:9092&quot;)
    .log(&quot;Message received from Kafka : ${body}&quot;)
    .log(&quot;${body} wants to adopt a cat&quot;)
    
    //Store the name of the person
    .setProperty(&quot;person&quot;, simple(&quot;${body}&quot;))
    
    //Search for a lonely cat
    .log(&quot;...looking for available cats...&quot;)
    .setBody().simple(&quot;SELECT id, name, image FROM cat WHERE person is NULL LIMIT 1;&quot;)
    .to(&quot;jdbc:postgresBean?&quot;)
    
    .choice()
      .when(header(&quot;CamelJdbcRowCount&quot;).isGreaterThanOrEqualTo(1))
        .setProperty(&quot;catname&quot;, simple(&quot;${body[0][name]}&quot;))
        .setProperty(&quot;catimage&quot;, simple(&quot;${body[0][image]}&quot;))
        .setProperty(&quot;catid&quot;, simple(&quot;${body[0][id]}&quot;))
        .log(&quot;Cat found called ${exchangeProperty.catname} with ID ${exchangeProperty.catid}&quot;)
        //There's a cat available, adopt it!
        .setBody().simple(&quot;UPDATE cat SET person='${exchangeProperty.person}' WHERE id=${exchangeProperty.catid}&quot;)
        .to(&quot;jdbc:postgresBean?&quot;)
  
        //Write some log to know it finishes properly
        .setBody().simple(&quot;Congratulations! ${exchangeProperty.catname} adopted ${exchangeProperty.person}. See how happy is on ${exchangeProperty.catimage}.&quot;)
        .to(&quot;log:info&quot;)
      .otherwise()
        //Write some log to know it finishes properly
        .setBody().simple(&quot;We are sorry, there's no cat looking for a family at this moment.&quot;)
        .to(&quot;log:info&quot;)
    .end();
  }
}
</pre>


### Feeding data automatically

As an extra step on this exercise, we are going to implement a final job that sends random new cat data to the Kafka &#8220;cat&#8221; topic with a timer.

The complexity on this class is not the Camel side, but the random generator of cat names.

<pre class="brush: java; title: ; notranslate">
// camel-k: language=java dependency=camel:gson

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;

public class AutoCat extends RouteBuilder {
  @Override
  public void configure() throws Exception {

    // Preparing properties to build a GeoJSON Feature
    Processor processor = new Processor() {

      String[] title = new String[] { &quot;&quot;, &quot;Lady&quot;, &quot;Princess&quot;, &quot;Mighty&quot;, &quot;Your Highness&quot;, &quot;Little&quot;, &quot;Purry&quot;, &quot;Empress&quot;, &quot;Doctor&quot;, &quot;Professor&quot; };
      String[] firstname = new String[] { &quot;Dewey&quot;, &quot;Butter&quot;, &quot;Merlin&quot;, &quot;Epiphany&quot;, &quot;Blasfemy&quot;, &quot;Metaphor&quot;, &quot;Fuzzy&quot;,
          &quot;Whity&quot;, &quot;Astro&quot;, &quot;Salty&quot;, &quot;Smol&quot;, &quot;Whiskers&quot;, &quot;Scully&quot; };
      String[] lastname = new String[] { &quot;&quot;, &quot;Luna&quot;, &quot;Wild&quot;, &quot;Dragonis&quot;, &quot;Firefly&quot;, &quot;Puff&quot;, &quot;Purrcy&quot;, &quot;Priss&quot;,
          &quot;Catsie&quot; };

      Random r = new Random();

      @Override
      public void process(Exchange exchange) throws Exception {
        Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
        map.put(&quot;image&quot;, exchange.getProperty(&quot;catimage&quot;).toString());

        StringBuilder name = new StringBuilder();
        name.append(title[r.nextInt(title.length)]);
        name.append(&quot; &quot;);
        name.append(firstname[r.nextInt(firstname.length)]);
        name.append(&quot; &quot;);
        name.append(lastname[r.nextInt(lastname.length)]);

        exchange.setProperty(&quot;catname&quot;, name.toString());
        map.put(&quot;name&quot;, name.toString().trim());

        exchange.getMessage().setBody(map);

      }

    };

    // Listen to kafka cat broker
    from(&quot;timer:java?period=10s&quot;)
      
      // Take a random image
      .to(&quot;https://api.thecatapi.com/v1/images/search&quot;)
      .unmarshal().json(JsonLibrary.Gson)
      .log(&quot;A new cat arrived today ${body[0][url]}&quot;)
      .setProperty(&quot;catimage&quot;, simple(&quot;${body[0][url]}&quot;))

      // name cat and prepare json
      .process(processor)
      .log(&quot;${body}&quot;)
      .marshal().json(JsonLibrary.Gson)
      .log(&quot;We named them ${exchangeProperty.catname}&quot;)

      // Send it to Kafka cat broker
      .to(&quot;kafka:cat?brokers=my-cluster-kafka-bootstrap:9092&quot;)

      // Write some log to know it finishes properly
      .log(&quot;Cat is looking for a family.&quot;);

  }
}
</pre>

Now you are ready to implement your own orchestrations with Kafka and Camel K.


The post <a rel="nofollow" href="http://delawen.com/2020/10/what-is-integration/">What&#8217;s all this integration agitation, anyway?</a> appeared first on <a rel="nofollow" href="http://delawen.com">Délawen's blog</a> written by <a rel="nofollow" href="http://delawen.com/author/delawen/">María Arias de Reyna Domínguez</a>.