<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vyacheslav Boyko on Medium]]></title>
        <description><![CDATA[Stories by Vyacheslav Boyko on Medium]]></description>
        <link>https://medium.com/@bvn13?source=rss-1b267a49ee95------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Q9O2w1Va6c_ilNMmlNRynA.jpeg</url>
            <title>Stories by Vyacheslav Boyko on Medium</title>
            <link>https://medium.com/@bvn13?source=rss-1b267a49ee95------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 03:12:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bvn13/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Normalized Logger for OpenFeign]]></title>
            <link>https://bvn13.medium.com/normalized-logger-for-openfeign-8727b4e5d776?source=rss-1b267a49ee95------2</link>
            <guid isPermaLink="false">https://medium.com/p/8727b4e5d776</guid>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[feign]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[api-development]]></category>
            <dc:creator><![CDATA[Vyacheslav Boyko]]></dc:creator>
            <pubDate>Mon, 25 Jul 2022 13:02:22 GMT</pubDate>
            <atom:updated>2022-08-12T14:44:45.138Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*qVFxgztCLiFpVIbvfoLi4A.png" /><figcaption>normalizing logs for OpenFeign</figcaption></figure><p>Standard <a href="https://github.com/OpenFeign/feign">OpenFeign</a> <a href="https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Logger.java">logger</a> provides the only approach to log communications — it logs every header in separated log entries, the body goes into another log entry.</p><p>It is very inconvenient to deal with such logs in production especially in multithreaded systems.</p><p>‘Normalized Logger’ is intended to combine all log entries related to one request-reply communication into one log entry.</p><h3>Old bad Logger</h3><p>All parts are separeted from each other:</p><ol><li>Request:<br>a. request headers — every header is put into separated entry<br>b. requst body — at separated entry</li><li>Response:<br>a. response headers — separately<br>b. response body — at separated entry as well</li></ol><pre>2022-07-25 14:12:43.572 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] ---&gt; POST https://example.com/api/v1/login HTTP/1.1<br>2022-07-25 14:12:43.573 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] Content-Length: 23<br>2022-07-25 14:12:43.573 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] Content-Type: application/json<br>2022-07-25 14:12:43.574 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4464.5 Safari/537.36<br>2022-07-25 14:12:43.575 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] <br>2022-07-25 14:12:43.576 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] {&quot;login&quot;:&quot;123456789&quot;}<br>2022-07-25 14:12:43.576 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] ---&gt; END HTTP (21-byte body)<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] &lt;--- UNKNOWN 200  (324ms)<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] cache-control: no-cache<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] cf-cache-status: DYNAMIC<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] cf-ray: 730476518ea441ce-AMS<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] content-type: application/json<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] date: Mon, 25 Jul 2022 11:12:43 GMT<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] expect-ct: max-age=604800, report-uri=&quot;https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct&quot;<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] feature-policy: accelerometer &#39;none&#39;; camera &#39;none&#39;; geolocation &#39;none&#39;; gyroscope &#39;none&#39;; magnetometer &#39;none&#39;; microphone &#39;none&#39;; payment *; usb &#39;none&#39;<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] referrer-policy: strict-origin-when-cross-origin<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] server: cloudflare<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] set-cookie: ACCESS_TOKEN=eyJh9uygCUMA659bAZ54SHpSNy_KFXQ; Max-Age=1800; Domain=.example.com; Path=/; Secure; SameSite=None<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] strict-transport-security: max-age=31536000<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] x-content-type-options: nosniff<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] x-frame-options: sameorigin<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] x-xss-protection: 1; mode=block<br>2022-07-25 14:12:43.901 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] <br>2022-07-25 14:12:43.902 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] {&quot;status&quot;:{&quot;code&quot;:&quot;OK&quot;,&quot;message&quot;:&quot;OK&quot;},&quot;body&quot;:{&quot;id&quot;:&quot;20826&quot;}}<br>2022-07-25 14:12:43.902 DEBUG 1032530 --- [Executor] feign.Logger  : [AuthApi#login] &lt;--- END HTTP (61-byte body)</pre><h3>New Normalized Logger</h3><p>The whole communication (request and response parts) is combined into one log entry.</p><pre>2022-07-25 14:16:06.217  INFO 1057053 --- [Executor] me.bvn13.openfeign.logger.NormalizedFeignLogger  : normalized feign request {AuthApi#login(LoginRequestDto)=[AuthApi#login] }: [<br>---&gt; POST https://example.com/api/v1/login HTTP/1.1<br>Content-Length: 23<br>Content-Type: application/json<br>user-agent: bvn13 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4464.5 Safari/537.36<br><br>{&quot;phone&quot;:&quot;123456789&quot;}<br>---&gt; END HTTP (21-byte body)<br>] has response [<br>&lt;--- UNKNOWN 200  (411ms)<br>cache-control: no-cache<br>cf-cache-status: DYNAMIC<br>cf-ray: 73047b41ffd2fa4c-AMS<br>content-type: application/json<br>date: Mon, 25 Jul 2022 11:16:06 GMT<br>expect-ct: max-age=604800, report-uri=&quot;https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct&quot;<br>feature-policy: accelerometer &#39;none&#39;; camera &#39;none&#39;; geolocation &#39;none&#39;; gyroscope &#39;none&#39;; magnetometer &#39;none&#39;; microphone &#39;none&#39;; payment *; usb &#39;none&#39;<br>referrer-policy: strict-origin-when-cross-origin<br>server: cloudflare<br>set-cookie: ACCESS_TOKEN=eyJhboft6rzD6Be16dXY5lgQNCzOZNFe4ra_NDIdmXlXi19hlvaQ; Max-Age=1800; Domain=.example.com; Path=/; Secure; SameSite=None<br>strict-transport-security: max-age=31536000<br>x-content-type-options: nosniff<br>x-frame-options: sameorigin<br>x-xss-protection: 1; mode=block<br><br>{&quot;status&quot;:{&quot;code&quot;:&quot;OK&quot;,&quot;message&quot;:&quot;OK&quot;},&quot;body&quot;:{&quot;id&quot;:&quot;20826&quot;}}<br>&lt;--- END HTTP (61-byte body)<br>]</pre><h3>How to use</h3><p>In order to use Normalized Logger into the application they must the following.</p><h4>1. Add dependency</h4><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;me.bvn13.openfeign.logger&lt;/groupId&gt;<br>    &lt;artifactId&gt;feign-normalized-logger&lt;/artifactId&gt;<br>    &lt;version&gt;0.1.0&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h4>2. Create Feign configuration and enable logger</h4><pre>import feign.Logger;<br><br>public class MyFeignConfig {<br>    <br>    @Bean<br>    public Logger logger() {<br>        return new NormalizedFeignLogger();<br>    }<br>    <br>}</pre><h4>3. Use this configuration into @FeignClient objects</h4><pre>@FeignClient(name = &quot;auth&quot;, configuration = MyFeignConfig.class)<br>public interface AuthApi {<br>/*...methods...*/<br>}</pre><h4>4. Adjust DEBUG level for Normalized Logger</h4><p>for Slf4J + Logback</p><pre>logging:<br>  level:<br>    me.bvn13.openfeign.logger.NormalizedFeignLogger: DEBUG</pre><h4>5. Don’t forget to specify FULL log level for your API class</h4><pre>feign:<br>  client:<br>    config:<br>      auth:<br>        logger-level: FULL</pre><p>Main code of project is placed into <a href="https://gitea.bvn13.me/bvn13/OpenFeign-NormalizedLogger">https://gitea.bvn13.me/bvn13/OpenFeign-NormalizedLogger</a></p><p>Github clone of the repo is <a href="https://github.com/bvn13/OpenFeign-NormalizedLogger">https://github.com/bvn13/OpenFeign-NormalizedLogger</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8727b4e5d776" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Coding in a Couple]]></title>
            <link>https://bvn13.medium.com/coding-in-a-couple-48572ed348d7?source=rss-1b267a49ee95------2</link>
            <guid isPermaLink="false">https://medium.com/p/48572ed348d7</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leading-teams]]></category>
            <category><![CDATA[teams-and-teamwork]]></category>
            <dc:creator><![CDATA[Vyacheslav Boyko]]></dc:creator>
            <pubDate>Mon, 11 Jul 2022 13:32:30 GMT</pubDate>
            <atom:updated>2022-07-11T13:32:30.587Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Qpe-Equ8Y-gbIMlu.jpeg" /></figure><p>There is an approach to code in a couple in IT world.</p><h3>What is this?</h3><p>Coding in a Couple is a technique, an approach when two programmers are coding together at one computer at the moment. One programmer starts as a writer (driver), the other one — as a commentator (navigator). The commentator may truck down all errors the writer made and suggest the best workaround. At the same time the commentator is disallowed to change the code themselves.</p><p>A time letter they are changing they roles: the writer becomes the commentator and the commentator starts to write code.</p><h3>Why do they do it?</h3><p>Coding in a couple is for better productivity and for faster studying how to code. It is because the very writing code takes about 10% of all time, the others 90% is taken by a search for a better solution.</p><p>The writer and the commentator both are thinking about the task together and they are exchanging their thoughts shortly.</p><p>Statistics says that those tasks, which takes more than 1–2 days usually, may be solved in several hours if coding in a couple.</p><h3>So, the pros of coding in a couple</h3><p>1. The speed of task solving is increasing<br>2. Less errors left in code while coding in a couple<br>3. Looking at one is coding the other one is thinking about the coding style of the writer and adopt it<br>4. Coding in a couple helps to solve such tasks which seemed unsolvable before</p><h3>And the cons of coding in a couple, of course</h3><p>1. Coding in a couple is a resource intensive process — it is hard to concentrate for more than 4 hours along<br>2. Two programmers must have almost equaling qualification to code in a couple<br>3. Two programmers must be comfortable to each other as being a person otherwise the one could loose the focus and its motivation</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48572ed348d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Request-Reply pattern using Apache Kafka or How not to loose your data]]></title>
            <link>https://bvn13.medium.com/request-reply-pattern-using-apache-kafka-or-how-not-to-loose-your-data-b30180918c55?source=rss-1b267a49ee95------2</link>
            <guid isPermaLink="false">https://medium.com/p/b30180918c55</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[eip]]></category>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[request-reply]]></category>
            <dc:creator><![CDATA[Vyacheslav Boyko]]></dc:creator>
            <pubDate>Thu, 17 Feb 2022 13:23:52 GMT</pubDate>
            <atom:updated>2022-03-09T09:54:34.974Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This article contains only one line of code :)</em></p><h3>Preface</h3><p>There are many patterns to help with software design. The Gang-of-Fourth provides us with patterns to organize the code better. The other list of patterns called Enterprise Integration patterns (<strong>EIP</strong>) is intended to help with communications between applications.</p><p>There are two kinds of communications between a couple of applications:<br>1. <strong>Synchronous</strong> — HTTP, Sockets<br>2. <strong>Asynchronous</strong> — Message Queues, Databases, Files, E-Mail, Cloud storage</p><h4>One of EIP is Request-Reply.</h4><p>It is very simple. When one service needs in some data it sends a Request to the other service which is responsible of such data. Then responsible service prepares an Response and provides the Requestor with it.</p><p>It is not a secret, that when two applications communicate each other using sockets or HTTP, responses are sent back to Requestor right after request is caught. No mess, no miscommunications.</p><p>But sometimes there are no possibility to choose which kind of communication to use. The Asynchronicity bring us some sort of complexity.</p><p>There are no worries in case when an application does not required in response just-in-time (event driven).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*rQa1GBfaasTCLaJk" /><figcaption>Request-Reply pattern</figcaption></figure><p>But sometimes it needs to gather some data from others services for further processing (e.g. in transaction). I call such behavior “Sync through Async”. Such cases are very interesting in their deviations.</p><p>My case I want to tell you about is such one.</p><p>The company where I work at is to provide E-Wallets and E-Payments for people. A few weeks ago I have implemented the service for risk scoring of incoming payments. What is it? It is a service for qualitative assessment of any payment and making the verdict — should the payment be processed or not.</p><p>Payment risk scoring in our case is to calculate a value depending on three incoming parameters:</p><ol><li>Risk scoring of user (wallet)</li><li>Risk scoring of country where the payment is going to (according to political verdicts)</li><li>Risk scoring of payment type</li></ol><p>So if resulting value (after calculating) becomes greater than the critical bordered value, the payment should be declined.</p><p>We have had the service responsible to user risk scoring already when I started. So my goal was to implement the service to calculate the very payment risk, but the service should have gathered user score from the responsible service. We used Apache Kafka for service communications only.</p><p>Here are a principle scheme of the microservice was called RMS (risk management system).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/0*tgWvdtNqmhK_ZOic" /></figure><ol><li>when the main payment processing service meet a payment having the status “antifraud is to be checked for”, it send Payment ID into Kafka topic with name “PAYMENT-READY-FOR-ANTIFRAUD”</li><li>Antifraud service catch this payment ID from Kafka, enrich it with full payment data reading it from database, then sends payment model into Kafka topic named “PAYMENT-DETAILS”</li><li>PAYMENT DETAILS Kafka topic is read by RMS in order to:<br>a) receives payment<br>b) asks user risk score from Know-Your-Customer (KYC) service sending the Request into Kafka topic with name “WALLET-KYC-REQUEST”<br>c) Reply for such Request is waited from Kafka topic having name “WALLET-KYC-RESPONSE” by RMS . <em>Here is Request-Reply pattern — each request waits its reply to proceed further calculations.</em><br>d) country score and payment type score are gathered from local database by RMS both<br>e) combining all three risk score values altogether RMS is to calculate payment risk score<br>f) payment risk score is sent into Kafka topic with name “PAY-DECISION”</li><li>Antifraud service listens to payment decisions from Kafka topic, processes them in order to approve or decline the payment</li></ol><h3>The Situation</h3><p>The described above solution was successfully tested onto two of our independent QA stands. But it started to fail right after it was deployed onto production infrastructure (we use Kubernetes).</p><p>Checking application logs it was noted that RMS service sent user score Request into Kafka topic “WALLET-KYC-REQUEST”, then KYC service sent the Reply into Kafka topic “WALLET-KYC-RESPONSE”, but RMS service did not use them together in order to calculate decision. All Requests failed with timeout while were waiting their Responses. All Responses did not “see” corresponding Requests and failed with timeout as well.</p><p>We decided that Request has not been aggregated with corresponding Response due to they had different correlation keys (please see <a href="https://camel.apache.org/components/3.15.x/eips/aggregate-eip.html">Aggregate EIP</a> implemented in Apache Camel for details). But actually it was not true. We have checked it up by adding more logging messages.</p><p>And the cause was revealed right after I have noticed that RMS service was running in two instances in one Kubernetes cluster. It explained all we have seen in logs.</p><p>One RMS instance sent Request and another instance received corresponding Response. Obviously they were not able to be aggregated together.</p><h3>The Analysis</h3><p>Such the problem is usual in microservice architecture. Developers must keep in mind that their applications might be started in more that one instances in parallel. And when the data is needed to be processed inside one instance of application (in one transaction or without any) from the beginning till the end, it really important to be convicted that the application does not consume the data not intending to itself. Being more precise, the application should consume the data intending only to itself.</p><p>After understanding how it should have been implemented we started to redesign RMS service to make it consuming Responses in the instance which has the corresponding Request only.</p><p>In order to explain how it should work I should tell about Kafka managing.</p><h3>Kafka topics</h3><p>All Kafka topics are divided into partitions.</p><p>Please read good article describing <a href="https://medium.com/event-driven-utopia/understanding-kafka-topic-partitions-ae40f80552e8">Kafka partitioning</a> if you are not familiar with it yet. (Thanks, <a href="https://medium.com/u/a927ff67ee5">Dunith Dhanushka</a>)</p><h4>Kafka clients</h4><p>Kafka clients are of types: producer and consumer. First one puts the data into Kafka topic and the latter reads the data listening (polling) it from Kafka topic accordingly.</p><h4>Kafka producers</h4><p>When the Kafka producer sends message into Kafka topic it is put only in one partition of the topic. The magic is how the producer chooses the partition to put message in. Every message might have the Key (it might be null at all). All Producers have their Hash-Function which helps to select a concrete partition to put the message to. So that Hash-Function takes the message Key as an argument to calculate the Result. Then this Result is the Partition number where the message will be put to.</p><p>In example:</p><pre>partitionNumber = messageKey % partitionsCount</pre><p>The partitions allow writing messages in parallel. If a message is published with a key, then, by default, the producer will hash the given key to determine the destination partition. This provides a guarantee that all messages with the same key will be sent to the same partition. In addition, a consumer will have the guarantee of getting messages delivered in order for that partition.</p><h4>Kafka consumers</h4><p>In other side the Consumer does not read the data along. It have to work in Consumer Group (remember group-id in connection properties?). Kafka guarantee every message is delivered every consumer group. Kafka guarantee that every message is delivered to only one consumer in each consumer group.</p><p>It is complex scheme how consumers work in their groups — there is one leader and the others interacting by communication protocol having such steps as connecting, leader selecting, partitions distribution and others. All is need to know now — partition distribution uses a Strategy which could be selected on startup.</p><p>Please read a great article about <a href="https://medium.com/streamthoughts/understanding-kafka-partition-assignment-strategies-and-how-to-write-your-own-custom-assignor-ebeda1fc06f3">how Kafka consumers connect to Kafka</a> by <a href="https://medium.com/u/c6b43e354c6">Florian Hussonnois</a> for deep knowledge.</p><h3>The Problem</h3><p>RMS service had one producer to write into WALLET-KYC-RESPONSE and one consumer to read from WALLET-KYC-RESPONSE. KYC service was not scaled at that moment — it listened all the partitions and wrote in one of partitions of topic WALLET-KYC-RESPONSE. All RMS consumers (in all instances of RMS) worked into one consumer group. Please take a look at the scheme below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/0*qN_Wyozs9zEUk0mc" /><figcaption>Miscommunication into the World of Microservices</figcaption></figure><p>Our problem became how to make the consumer listen the very partition which the producer will put the message into.</p><h3>The Solution</h3><p>The good solution would be to adjust all producers to put messages right into the topics that was listened to by consumers which was in need of the very data. It became a mess! The other side — such scenario is very expansive to maintain. They must to keep in mind the whole scheme of interactions between four microservices and to tune them all if only one of them needs scaling. If they do not, the data would have might be lost.</p><h3>The better solution</h3><p>Our solution takes for about two hours to be implemented. And the maintaining will be inexpensive.</p><p>We use Kafka polices to administer all Kafka connections — Kafka users and Consumer groups both.</p><p>So we added into the Kafka polices a rule to allow consumer group described with a mask in group-id. It allows us to set up consumer group in RMS service into runtime right after it starts.</p><p>We literally have written:</p><pre>String groupId = &quot;rms-&quot; + UUID.random().toString();</pre><p>It allows not to have a headache while scaling RMS service. <strong>All RMS instances receives all Responses. </strong>It does not matter whether the instance has sent Request right before and is awaiting the Response or not.</p><p>Therefore, whereas Responses are gathered by every running instance of RMS service, not all of they could be paired with according Request. And it is normal for us, because it is a good scenario! We have the Aggregator to pair Request with Reply. And if any of them becomes not paired it failed with timeout, writing a log message. Much better — we get guarantee that every Request with master-data will be paired with Response and the process will not be interrupted.</p><h3>Conclusions</h3><ol><li>The design of asynchronous interaction should take into account not only aspects of the interaction itself, but also the stages of architecture expansion.</li><li>Synchronous interaction would be cheaper to support the solution we found.</li><li>When implementing Request-Reply on Kafka, it is important that the retention periods of messages in the topic in the Kafka broker are set to values greater than the time of a possible restart of applications — this will reduce the risk of data loss during restarts.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b30180918c55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing Apache Camel 3.13.x in SpringBoot application in examples]]></title>
            <link>https://bvn13.medium.com/testing-apache-camel-3-13-x-in-springboot-application-in-examples-1529260ec8e0?source=rss-1b267a49ee95------2</link>
            <guid isPermaLink="false">https://medium.com/p/1529260ec8e0</guid>
            <category><![CDATA[apache-camel]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[routing]]></category>
            <category><![CDATA[advice]]></category>
            <dc:creator><![CDATA[Vyacheslav Boyko]]></dc:creator>
            <pubDate>Tue, 23 Nov 2021 11:02:20 GMT</pubDate>
            <atom:updated>2021-11-23T11:13:55.808Z</atom:updated>
            <content:encoded><![CDATA[<p>Apache Camel is a framework for data transformation and delivery.</p><p><em>In a few words</em>:</p><ol><li>Apache Camel gives you a DSL (Domain Specific Language) that helps you to describe how the data should be transformed.</li><li>Apache Camel has over 330 different connectors, by means of which you could easily gather your data and deliver it wherever it needs.</li><li>Apache Camel provides you a mechanism of thread manipulation under the hood to process any job in parallel.</li></ol><p>So…</p><p>Consider I have a folder. And text files are being collected in this folder by the other application.</p><p>My task is to check every file on bad words (e.g. duck) to replace it with a good one (e.g. ostrich).</p><p>You could think about fetching each file to process it line-by-line superseding each bother word with permitted one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1008/1*JutsAJGVJ93bXk-qoPbrLA.png" /><figcaption>Schema describing data transformation of superseding bad words with good ones in every incoming file</figcaption></figure><p>You are correct. I’ve implemented it for you in Camel. Take a look.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*xTquz7V31dSQOysxOOOaKQ.png" /><figcaption>Spring Boot bean to implement Camel Route — I like such charm. By the way you should not write such comments.</figcaption></figure><p>How to test this code? - you may ask. And the answer is — let’s write a test.</p><p>After reading <a href="https://camel.apache.org/manual/testing.html">official documentation</a> about how to test Apache Camel applications I need to add all needed dependencies (consider of using maven or gradle).</p><p>In gradle:</p><pre>testImplementation <br><strong>&#39;org.apache.camel:camel-test-spring-junit5:3.13.0&#39;</strong></pre><p><strong>N.B.! </strong>When writing a test covering Apache Camel Route <strong>it should not test the side of work that Apache Camel does</strong>. It should not test how Camel connects to AWS or sends email — all such stuff is tested already in Apache Camel sources. <strong>You should implement a test covering only functionalities instead</strong>.</p><p>According to this rule I should not listen to new files in a folder, should not write the result into another folder. All I should do — is to mock my IN and OUT folders and test if my data transformation is correct.</p><p>In order to mock my from- and to-endpoints I’m going to remove folders names explicitly specified into endpoints definition and put them in another Spring Bean — property provider. It will be straightforward to substitute folders with mocks in my test.</p><p>I am to construct an interface providing input and output endpoints and the class implementing it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*MS_Luon3PI57i4kvYc-qXQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*zcqeAR_Wkhi6ZZfGZ1VGzg.png" /><figcaption>notice this class is not for tests</figcaption></figure><p>And another property provider for test with mocked endpoints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*Wqg7l1ono5TmcrS35Ln0aw.png" /><figcaption>this class is for tests only — it is placed into src/test/java path</figcaption></figure><p>So my test is below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*_p1hHsewtxWiNNq8me6Pfw.png" /></figure><p><strong>Notice the following</strong>:</p><ol><li>Test is annotated with @SpringBootTest and@CamelSpringBootTest both</li><li>@SpringBootTest has classes property specified with Application class</li><li>In test method mocked endpoint is adjusted with several expectations</li><li>After a message is sent into from-endpoint the mocked endpoint checks all caught messages on expectations to be satisfied</li></ol><p>So when you run this application with options <br>-Dapp.input.folder=/tmp/in -Dapp.output.folder=/tmp/out <br>-Dapp.bad-word=duck -Dapp.good-word=ostrich <br>creating two folders /tmp/in and /tmp/out beforehand, every file placed into /tmp/in is to be processed and every ‘duck’ becomes ‘ostrich’ now. Let’s see it.</p><pre>$ echo &quot;123 duck<br>        456 luck<br>        789 duck tales&quot; &gt; /tmp/in/test</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IXwVMDu_c7VgwZB5rvbCow.png" /><figcaption>incoming file has been processed</figcaption></figure><pre>$ cat /tmp/out/test<br>123 ostrich<br>456 luck<br>689 ostrich tales</pre><p>Now consider I’m interested in supervision each file with external Supervisor over HTTP. The Camel Route becomes the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*jxvUsytZkio83ZlVUGz5Hw.png" /></figure><p>My test becomes red as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n3mvVKO5oks1qNfBv8B59g.png" /><figcaption>it needs to add dependency for HTTP Camel component, but it is not necessary for testing</figcaption></figure><p>How do I deal with it now? What is if external service for supervising is not available from CI/CD where tests to be run? What is if this external service is not available even from my laptop? The answer is — to disable external service call in test without changing route definition.</p><p>I am to move external endpoint definition into Property Provider to segregate production and test code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cSXLrjZyiN4K8F74O4XGsw.png" /><figcaption>segregating production and test external service — mock it in test</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*D0hl5IVZigu4lozbbTGByg.png" /><figcaption>route definition use endpoint definition provided by property provider</figcaption></figure><p>Ok, how about my test?</p><p>Apache Camel Test KIT provides a tool called Advice which helps you to mock dealing with external services. You can “advice” Camel to (e.g.) set a concrete value into body instead of calling external service. The test becomes as following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*wOQGe0FIpR68xKLL_YxSTQ.png" /><figcaption>I advice Camel to set a concrete body instead of calling supervisor service</figcaption></figure><p>But the only thing you should remember is how to enable this Advice mechanism. It is enabled with annotation @UseAdviceWith on Test class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*5ClCRl712mIJIc0OfHqtgg.png" /><figcaption>enabling Advice mechanism</figcaption></figure><p>By enabling Advice mechanism it disables automatically Camel Context starting. It leads you to enable it manually in test.</p><pre><strong>camelContext</strong>.start(); <em>// start CamelContext explicitly<br>// perform test - send a message into route<br></em><strong>camelContext</strong>.stop(); <em>// stop CamelContext explicitly</em></pre><p>The debug log shows the use of advice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nLGSIQnKAycw15pqs4wJGw.png" /><figcaption>notice that Camel Context is not started automatically</figcaption></figure><p>A debug log of test is the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zIUA_mnlP_sAtMAf8yvGAQ.png" /></figure><p>For tests taking good sense it is necessary to implement another case — if supervisor tells to skip a file. I set mock endpoint is expected to receive 0 messages (0 files expected to move into /tmp/out folder). And I set the Camel Advice to put “SKIP” as the response of supervisor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*XmaOdKsNxLdbLaysVuZptQ.png" /></figure><p>This test is green, be sure.</p><p>You can find my sources on <a href="https://github.com/bvn13/JavaLessons/tree/master/camel-testing">GitHub</a>.</p><p><em>I hope my story helps you to test your Camel routes better and more straightforward.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1529260ec8e0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using .when(), .end(), .endChoice() in Apache Camel with examples]]></title>
            <link>https://bvn13.medium.com/using-when-end-endchoice-in-apache-camel-with-examples-5e6e4f4f7f16?source=rss-1b267a49ee95------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e6e4f4f7f16</guid>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[apache-camel]]></category>
            <category><![CDATA[eip]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[Vyacheslav Boyko]]></dc:creator>
            <pubDate>Wed, 10 Nov 2021 13:16:44 GMT</pubDate>
            <atom:updated>2021-11-10T14:19:31.840Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using .when(), .end(), .endChoice() in Apache Camel with examples</h3><p>1.If your <strong>.when()</strong> block contains multiple choices, than all nested <strong>.when()</strong>/<strong>.otherwise()</strong> need to be closed explicitly with <strong>.endChoice()</strong>.</p><p>You cannot use <strong>.end()</strong> for closing nested <strong>.choice()</strong> block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/0*4Hr5PsW_U4mbD7xn" /></figure><p>It must be closed with .endChoice().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*GC6Yn0jZJ6iaomPmZ5zhhw.png" /></figure><p>2.If your <strong>.when()</strong> block contains more complex block (i.e. <strong>.loadBalance()</strong> block), than it must be closed explicitly with <strong>.endChoice()</strong>.</p><p>You cannot close <strong>.when()</strong> having nested<strong> .loadBalance() </strong>with <strong>.end()</strong> as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*c68iUXwykaeQwlzT" /></figure><p>It must be closed with <strong>.endChoice()</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/0*gmqqFJ3obTmk9Y_U" /></figure><p>3.You are allowed not to close <strong>.when()</strong>/<strong>.otherwice()</strong> block when it contains only primitive nested operators. But you should close <strong>.choice()</strong> block with <strong>.end()</strong> if there are any further logic in the same route.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*ZMg9OYjqAQ4TeNQqmtOGzA.png" /></figure><p><em>Read the documentation: </em><a href="https://camel.apache.org/components/3.12.x/eips/choice-eip.html">https://camel.apache.org/components/3.12.x/eips/choice-eip.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e6e4f4f7f16" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>