Reasonable developers don’t write code

Something stuck in my head a long time ago, when I was a young junior developer. I was working trying to speed up data access in a web application. The first iteration of the code called the database every time the user viewed the data. In search of optimization, I decided to cache the original returned data so as not to return to the database. It made sense.

So, with my head down, I spent days coming up with something that would be the best Java caching code I’ve ever seen! I came up with complex algorithms for calling, storing, paging and deleting data for one use case. After 5 days and nights of hard work, I proudly met my lead developer and introduced him to my code; I fooled him with the brilliance of the algorithms.

It was embarrassing that I was not greeted with congratulations on the implementation of the revolutionary data paging technique, but with a deep sigh and the promise of a review.

The next day, the lead called me and said: “I don’t know what you did there, but I deleted all your code and just saved the data in a list that updates with every new search.”

First, he told me that my code was over-engineering, and then that I wrote something that solves a problem for which there are already templates and tools.

Tail between my legs, I accepted that and learned the most important lesson for a junior developer, metaphorically getting a slap on the head.

One of my earliest lessons in software engineering is that when solving a problem, don’t go straight to code.

This tutorial reminded me of a recent conversation with a young developer who presented a problem using code in RedHat OpenShift (warning: I work at Red Hat). In great detail, he explained that he wrote many microservices to sit in front of many APIs that read data from various sources and manipulate it before sending it to various endpoints. He was very proud of the many microservices he developed to solve the problem. There were aggregators, translators, and other patterns.

I listened and sighed

I told him that the solution looks really complicated and that he overdid it with engineering. In a polite way, I told him that people have solved all these and other problems many times. Pointed to Apache Camel and said no need to develop microservices for the sake of microservices.

Don’t build microservices for microservices!

In IT, we seem to think that we need to reinvent something every time, always. I am totally for exciting new paradigms that change our approach, but not at the expense of throwing away what is already there.

Apache Camel is one of the great examples of not having to reinvent the wheel.

I’m a big fan of Apache Camel, both as an integrator and in terms of the great success of the Open Source community. The story claims that Camel emerged from a conversation between Gregory Hof and James Straken. Gregor wrote the bible of integration Enterprise Integration Patterns, a book with projects of common integration tasks as a set of templates … so you don’t have to invent the same architectural patterns.

James, Open Source Developer and Founder Groovy languageand Gregor was talking at a conference, and James thought it would be nice to create reusable implementations of these patterns in Java … so you don’t have to keep writing the same code when you need to do something! Apache Camel was born out of this conversation as a set of library integration patterns.

Back to a recent conversation with a junior developer

So we took a look at Red Hat Integration, which is the Apache Camel and a bunch of other things for deploying and running integration services. I told the junior developer that not only can he do whatever he needs, but he can fit everything into his microservices architecture and looking at some new developments like Camel K, he can also serve his microservices … serverless … [have his microservices served…err, serverless! – заставить работать свои микросервисы с помощью технологии serverless, бессерверно]…

Serverless Microservices Integration

Camel K is the most exciting thing about integration right now. Some of the biggest challenges integrators face have always been how to achieve large-scale availability with integration solution architectures. Tools like Camel K and OpenShift take all aspects of deployment, launch, and scaling into account for this task.

Apache Camel provides templating implementations and hundreds of adapters. In-depth knowledge is needed only to figure out which connectors and patterns to use, and importantly, you need to know how components are deployed in a scalable and accessible architecture in a way that supports the requirements.

Projects like Camel K are a new breed of native kubernetes tools that seek to harness the power of Kubernetes to take advantage of the platform’s sheer scale and availability. Some of the tricky things in defining and designing scale and availability have already been implemented with Kubernetes, so I don’t have to re-invent scaling and availability!

What is Apache Camel?

Ok, I’ll tell you quickly, if you haven’t come across Apache Camel, then this is a tool of stunning beauty that really makes it easy to integrate and connect hundreds of different things:

Let’s take a quick look at Apache Camel.

  1. It provides over 50 integration patterns.

  2. Has over 350 connectors.

  3. He is beautiful.

This is what lies at the heart of every integration. The data or call comes from the source (FROM), then goes to the handler (PROCESSOR), in which it is somehow processed, and then goes to the (TO) destination.

FROM and TO components or adapters connect technologies from native AWS services, CSV and SAP files to reactive streams and Kafka connectors … there are more than three hundred and fifty of them.

This story is about how Camel components are a brilliant way to showcase the power of open source communities and projects. I’ve been a huge Camel fan for many years and have seen the number of components grow with each release.

Here is the power of Open Source

This is what usually happens: the developer is using Camel, and the plugin he needs is not yet available in Camel. So, the developer writes a component that satisfies his requirements. As an honest citizen of the Open Source country, the developer pleases the Camel community by contributing a component to the Camel source code, and this component will be included in the next Camel release. Now that you need a component, you don’t have to re-create it … it’s awesome!

Lots of sources can help you with Camel. One of the best is the book by Klaus Ibson and Jonathan Ansti, Camel in action – Camel Bible. James Stracken has often said that he invented Camel, but Klaus made the tool work. Also, take a look at the shiny articles by Tom Donahue about Camel.

But what about Camel K?

I’ll quickly show you something amazing (okay, that would be amazing for an old integration hack like me). I will accomplish a fairly simple integration task in just a few lines of code.

I am going to create a Telegram bot that will send compliments to the Telegram channel. Telegram has a download API that I can use to send messages to channels that I can use.

And here is the compliment service Grant Codes: complimentr.comthat brings back pleasant words to cheer you up.

Wow, Grant's awesome color schemes!
Wow, Grant’s awesome color schemes!

Now I want to integrate these two services to send compliments to my friends on Telegram. So I’m going to dive into the code? No, damn it!

Take a look at Apache Camel connectors – among 350 components with which I can connect different things, I found Camel Telegram: someone who wanted to chat with Telegram already wrote something for this, so I don’t have to write anything!

Camel standard components will help with invoking plain HTTP in my compliment service. I frivolously say “standard” because there is actually an HTTP component that works with anything I could write, but I don’t need it!

So, I’m going to use the component Camel Telegramto receive a message from the bot, then I call an external service to receive a compliment and send it to the Telegram bot. The thing is not the most exciting, but a little fun. This is what I want from the bot: that I send a name and get a compliment in return.

I have a kubernetes cluster, so I’ll build a bot with Camel K and use Red Hat OpenShift and Camel K Operator to set things up.

Here’s what to do:

1. Download CLI Camel K.

2. Install Camel K Operator.

3. Run my code.

Download Camel K CLI

There are a lot of articles and documentation about this, so I won’t go into a bunch of details, but the Camel K CLI (or kamel) is essentially the CLI for interacting with your Kubernetes platform (or more precisely, with the Camel K custom resources on the platform) for deploying integrations (there are also plugins for VSCode).

The CLI allows you to install Camel K statements, run integrations in interactive development mode (with updates to running integration code on the fly), etc.

Installing Camel K Operator

Camel K has a number of Kubernetes operators to build, deploy, and run integrations. The whole point of Kubernetes operators is to abstract the complexity of everything that runs on the platform, and Camel K does a great job! So just grab and install the statement from the CLI using the command line:

Or, if you are working in Openshift, open the Operators tab to set the operators.

Run my code

Once installed, I can send the integration code to my running OpenShift platform from the IDE or from the command line.

Let’s take a look at the code. Remember I am working with Telegram component. I just need to take the Telegram authorization token and pass it to a component that will read messages from the bot.

from("telegram:bots?authorizationToken=[YOUR TOKEN]”)

If I add some simple logging to see how the route works:

from("telegram:bots?authorizationToken=[YOUR TOKEN]")

Then the output is just a couple of lines of code. Having saved them in the hello.groovy file, I will get a groovy script, which can be used as a CamelK integration that will connect to my telegram bot and log everything that I type!

Running hello.groovy

This is where something amazing begins. I want my two line groovy script to deploy and run as a scalable integration component. That’s what Camel K is! From the command line, I run a simple command:

With the run command, I turned my two-line groovy script into a container deployed and running as an Apache Camel route on my OpenShift cluster.

Why the –dev flag

By adding the –dev flag, I ended up in a strange world where I can change my hello.groovy file and the changes will be reflected in the OpenShift cluster! The –dev option makes development a lot easier.

Entire bot

You’ve already seen how Camel K works, and here is the rest of my coding evasion script:

from("telegram:bots?authorizationToken=[YOUR TOKEN]")
.setHeader("USER", body())
.setBody(simple("${body} ${in.header.USER}"))
.to("telegram:bots?authorizationToken=[YOUR TOKEN]")
  • To receive messages from a Telegram channel, I am using the Camel Telegram component.

  • Hide the name in the header passed in the body of the Telegram message.

  • Call the Compliment API.

  • Refer to the user by name.

  • Return the compliment to the Telegram channel.

Here are six lines of code doing the integration by reusing the components.

There are some really brilliant Camel integration patterns like aggregators, brokers, dynamic routing, and even more amazing things about Camel K like serverless or scaling to zero. [отключение, когда платформа не используется] upon request, they will help you not to write code!

Reasonable developers who don’t program are no longer news. The reasons why we create frameworks and tools are in the desire to abstract from the routine and work more productively. Sometimes you need to figure out which frameworks and tools will help you avoid coding. I hope I showed you one of these frameworks!

Find out the detailshow to get a Level Up in skills and salary or an in-demand profession from scratch by taking SkillFactory online courses with a 40% discount and a promotional code HABR, which will give another + 10% discount on training.

Other professions and courses

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *