<?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 Abdullah YILDIRIM on Medium]]></title>
        <description><![CDATA[Stories by Abdullah YILDIRIM on Medium]]></description>
        <link>https://medium.com/@abdullahyildirim?source=rss-9a13da8bf81f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*yDWxxNYCG7nsNQC4Sizx1g.jpeg</url>
            <title>Stories by Abdullah YILDIRIM on Medium</title>
            <link>https://medium.com/@abdullahyildirim?source=rss-9a13da8bf81f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:17:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdullahyildirim/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[Transaction Log Tailing With Debezium - Part 2:]]></title>
            <link>https://medium.com/trendyol-tech/transaction-log-tailing-with-debezium-part-2-9ecaebf063b9?source=rss-9a13da8bf81f------2</link>
            <guid isPermaLink="false">https://medium.com/p/9ecaebf063b9</guid>
            <category><![CDATA[installation]]></category>
            <category><![CDATA[debezium]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[transaction-log-tailing]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Abdullah YILDIRIM]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 07:20:13 GMT</pubDate>
            <atom:updated>2020-03-09T08:18:18.692Z</atom:updated>
            <content:encoded><![CDATA[<h3>Transaction Log Tailing With Debezium — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*-Yxrt1a7RPMzPrYHul1npw.jpeg" /></figure><p>In the previous post of this post series, I explained what Transactional Log Tailing and Debezium are and why we need to use them. In this post, I will give information about how to install Debezium and what configuration settings should be made. I will be advancing the pot into two sections. The first of these sections will be about what steps we need to follow when we try to install Debezium in our local environment. In the other section, I will explain how we deploy Debezium to Kubernetes clusters for use in the Prod environment as the Seller Core team in Trendyol.</p><h3>Installing Debezium on Local Docker</h3><p>Before starting the installation, make sure that Docker is installed and running on your operating system.</p><p>We will need 3 different service setups to run Debezium. These are Zookeeper, Apache Kafka and Debezium Connect services. Zookeeper and Kafka keep the data in the container. Therefore, its directories must be mounted to a volume on the host machine. We will not apply this structure to this post. Therefore, if the docker images are deleted, all data will be lost.</p><h4>Getting started with ZooKeeper</h4><p>Let’s open a new terminal and run the following command to start a new container with ZooKeeper inside.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8248b85cfb73ffad4d93a84fa8f8a62/href">https://medium.com/media/b8248b85cfb73ffad4d93a84fa8f8a62/href</a></iframe><p>After running this command, you should see an output of ZooKeeper similar to the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jAnxoHOOvNFH0pmLJl3yQg.gif" /></figure><p>Then open a new terminal and run the following command to start a new container with Kafka inside.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f7420f157f655ffcbc8ef5ef214a12e2/href">https://medium.com/media/f7420f157f655ffcbc8ef5ef214a12e2/href</a></iframe><p>Again, you should see Kafka’s output similar to the one below. The last line shows that Kafka started successfully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*maYiiLCgVIMuPahoLg_fLw.gif" /></figure><p>Now there is the PostgreSQL database. To create the PostgreSQL database, we will use the ‘debezium/postgres:11’ image, not the classic ‘postgres’ image on DockerHub. I will explain the reason for this later in the post.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92cc243fa7fd3335520093b1880033bf/href">https://medium.com/media/92cc243fa7fd3335520093b1880033bf/href</a></iframe><p>Again, you should see PostgreSQL’s output similar to the one below. The last line shows that PostgreSQL started successfully and ready to accept connections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jt_nTydUMFyCftD_SOxJfA.gif" /></figure><p>Finally, launch the Debezium Connect application, which will establish the connection between PostgreSQL and Kafka.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cfd06219531bb1fb448e9d5519a45bfe/href">https://medium.com/media/cfd06219531bb1fb448e9d5519a45bfe/href</a></iframe><p>If you saw an output of Debezium Connect similar to the one below, your setup is complete. If you want to do the operations so far with the docker-compose file, you can download it via my GitHub <a href="https://github.com/yildirimabdullah/debezium-connect-demo/tree/master/debezium-connect-with-docker">link</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0PaDVuSQYYmSVU2Z4uOBYw.gif" /></figure><p>While running this command, I will briefly mention the parameters that I have given as an environment variable.</p><p>CONFIG_STORAGE_TOPIC: The topic to store connector and task configuration state in.<br>OFFSET_STORAGE_TOPIC: The topic to store connector offset state in.<br>STATUS_STORAGE_TOPIC: The topic to use for storing statuses.</p><p>After running the command, I check if there are new topics in Kafka running on Docker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6962015eed8e1df540cffcf4ec17e07/href">https://medium.com/media/b6962015eed8e1df540cffcf4ec17e07/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XcRL1GP9FhsTZil4DXMhYg.gif" /></figure><p>There are a few PostgreSQL details that you need to know before creating a new connector. First of all, let’s start with why I use the ‘debezium/postgres: 11’ image I mentioned above. Debezium can provide PostgreSQL connection provided that wal_level is ‘logical’. The wal_level of PostgreSQL in this image is logical.</p><h4>So what are WAL and WAL_LEVEL?</h4><p>(WAL) The mechanism called Write-Ahead Logging guarantees that the Data Page will not be written to the disk until the transaction log file is written to disk. RDBMS gives the “Successful” information to the application that initiates and commits the transaction when the Transaction Log file is written to disk. Thanks to this feature, WAL provides Atomicity and Durability, which are among the principles of ACID.</p><p>Wal_level determines how much information is written to the WAL. The default value is ‘minimal’, which writes only the information needed to recover from a crash or immediate shutdown. ‘replica’ adds logging required for WAL archiving as well as information required to run read-only queries on a standby server. Finally, ‘logical’ adds information necessary to support logical decoding. Each level includes the information logged at all lower levels. This parameter can only be set at the server start.</p><p>PostgreSQL’s logical decoding feature was first introduced in version 9.4 and is a mechanism that allows the extraction of the changes which were committed to the transaction log and the processing of these changes via the help of an output plugin. I will use pg_output as the output plugin. This plugin has been maintained by the PostgreSQL community. It can be used in PostgreSQL 10 and above versions.</p><p>Now I will create a table for the demo on the PostgreSQL database. Then, I will create a Debezium Connector to listen for transactional logs of this table.</p><p>First, I execute the PostgreSQL container on Docker. Then I access the database named ‘debezium’ which I created earlier with the following commands and here I create a table named ‘persons’.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62033ba6cb46f9059e85e9066d8a4e78/href">https://medium.com/media/62033ba6cb46f9059e85e9066d8a4e78/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N8wHrhemjnk5GBN7MdY8uw.gif" /></figure><p>After successfully creating the table, it’s time to create a new Debezium Connector. In order to create Debezium Connector, I send the following curl request to the Debezium application running on Docker.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cafc3a4441ff57bfcbd3f98b139f5b6/href">https://medium.com/media/1cafc3a4441ff57bfcbd3f98b139f5b6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j81V3m6XDfSLt7DXWbbH6g.gif" /></figure><p>Note that I have defined plugin.name as ‘pgoutput’ among the parameters. Now I check the status of the Debezium Connector that has been created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d14caf4b2cc9f8ae3097b038a9aecb32/href">https://medium.com/media/d14caf4b2cc9f8ae3097b038a9aecb32/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vw0Pftw3BXMEPn8qojJ8hQ.gif" /></figure><p>Finally, I need to add a record to the ‘persons’ table and check the CDC.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a69a519bfa22e6a62de8602ff4be125/href">https://medium.com/media/8a69a519bfa22e6a62de8602ff4be125/href</a></iframe><p>After successfully adding the record, I check if a topic has occurred in Kafka and whether the CDC event has been sent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9efee6936209ebe5f76c4c1da36b797e/href">https://medium.com/media/9efee6936209ebe5f76c4c1da36b797e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BgUA0h9kNLLMuRx1Ff6lGw.gif" /></figure><p>After consuming the topic that occurred in Kafka, I saw that the CDC was working successfully. This is simply the installation of Debezium on Docker.</p><h3>Installing Debezium on Kubernetes Cluster</h3><p>In the Trendyol Seller Core team, we deploy our applications to Kubernetes. We operate the Gitlab CI/CD during the deployment process. That’s why we created a sample deployment pipeline for Debezium deployment. You can access this pipeline via my GitHub <a href="https://github.com/yildirimabdullah/debezium-connect-demo/tree/master/debezium-connect-with-k8s">link</a>. To explain it briefly, all of the above configurations are valid for Kubernetes environments. The only difference here is that we already have a Kafka and PostgreSQL servers.</p><p>We have come to the end of this post. In my next post, I will write about what situations we encountered in the process of implementing Debezium and how we found solutions for these situations.</p><p>If you have a will and tendency towards this kind of technology, <a href="https://jobs.lever.co/trendyol">reach us</a>.</p><h3>References</h3><ul><li><a href="https://debezium.io/documentation/reference/1.0/tutorial.html">Tutorial</a></li><li><a href="https://debezium.io/documentation/reference/1.0/connectors/postgresql.html">Debezium Connector for PostgreSQL</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9ecaebf063b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/transaction-log-tailing-with-debezium-part-2-9ecaebf063b9">Transaction Log Tailing With Debezium - Part 2:</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transaction Log Tailing With Debezium — Part 1]]></title>
            <link>https://medium.com/trendyol-tech/transaction-log-tailing-with-debezium-part-1-aeb968d72220?source=rss-9a13da8bf81f------2</link>
            <guid isPermaLink="false">https://medium.com/p/aeb968d72220</guid>
            <dc:creator><![CDATA[Abdullah YILDIRIM]]></dc:creator>
            <pubDate>Tue, 03 Mar 2020 06:06:43 GMT</pubDate>
            <atom:updated>2020-03-03T08:05:27.970Z</atom:updated>
            <content:encoded><![CDATA[<h3>Transaction Log Tailing With Debezium — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T92dgs7b-ORWreMb5aXAzQ.png" /></figure><p>In this series of posts, I will explain what the Transaction Log Tailing approach and Debezium are, what problems they solve, and why we need to use it as the Trendyol Seller Core team. The other article will include the installation of Debezium in Docker and Kubernetes environments and the configurations to be made. In the last article, there will be the problems we have experienced in this process and the solutions we have applied against these problems.</p><p>Sometimes there can be more than one action we need to take in any business logic, especially when coding a business on the backend development side. There may be event-driven actions such as publishing a message to any message broker in these actions to be taken.</p><p>As an example, let’s assume that we receive the information that the customer returns are approved and processed in the database in a return process. We also need to leave a message in a message broker system to pay the money back. However, we may not have been able to do this successfully. The scariest part of this structure will be the failure to send the event. What should we do to avoid such a situation? The Transactional Outbox pattern comes into play right at this point.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q-eKa74H6Xuny_RycO8TtQ.png" /><figcaption><em>Failure while publishing an event after updating/inserting the database</em></figcaption></figure><p>This model provides an effective solution for reliably publishing events. The purpose of this approach is to have an “Outbox” table in the database of the service. When receiving the registration request, not only addition is made to the ReturnOrder table, but also a record representing the event is added to the Outbox table. Two database processes are done as part of the same transaction.</p><p>An asynchronous process monitors the Outbox table for new entries and publishes any event on the Event Bus. The model improves reliability by simply dividing two processes on different services.</p><p>More detailed information about the Transactional Outbox pattern is available on Chris Richardson’s <a href="https://microservices.io/patterns/data/transactional-outbox.html">microservices.io</a> site. There are two approaches to implement the Outbox pattern as described on the site (Polling Publisher and Transactional Log Tailing). Let’s examine these approaches.</p><h4>Polling Publisher</h4><p>The Polling Publisher approach is a structure built to publish the records in the database’s Outbox table in order. A scheduled job that runs periodically processes the records in the Outbox table, then deletes the corresponding record. This structure is easy and costless to implement in low-scale systems. However, it also has some disadvantages.</p><ul><li>If you have a multi-instance running schedule job, it will be necessary to manage ‘Optimistic Lock’ to ensure that the recordings are published only once.</li><li>Publishing events in order is difficult.</li><li>As the number of calls to the database increases as the scale increases, it will cause a performance problem after a while.</li><li>Not applicable in all NoSQL databases.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dPHr7fafMx02a64RmCzeYw.png" /><figcaption><em>Using the outbox pattern with polling publisher approach</em></figcaption></figure><h4>Transaction Log Tailing</h4><p>This is another option to access distributed transactions. The idea is to queue the database transaction log and publish each change as an event. Each committed update is displayed as an entry in the transaction log of the database. A transaction log miner can read the transaction log entry and publish each change as a message to Message Broker. Converts each relevant log entry corresponding to the added data into a message. The messages are then published in a Message Broker. The benefit of this model is that there is no need for any changes at the application level; everything happens at the database level. It is a little difficult to avoid duplicate publishing. Therefore, in order to ensure data consistency, the application that will consume the related events must be idempotent.</p><p>The benefits of applying this pattern are;</p><ul><li>There is no two-phase commit.</li><li>Transactions take place in order.</li><li>The accuracy of the data is guaranteed.</li><li>No extra calls are made to the database through an application.</li><li>It can be applied in high scale systems.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*WA9_OmN4qshS1bUxMiUgzw.png" /><figcaption><em>Using the outbox pattern with transaction log tailing approach</em></figcaption></figure><p>However, the structure attracts less attention than it deserves and is particularly useful in the microservice context. As we will see later in the article, the outbox pattern can be applied very elegantly and efficiently using Change Data Capture (CDC) and Debezium. But before we touch on the details about CDC and Debezium, let’s see why we chose to use this structure.</p><h4>Why did we use CDC and Debezium in Trendyol?</h4><p>As the Trendyol Seller Core team, we considered the constantly growing number of sellers when designing the seller’s invoice domain. We considered the parameters that can operate the system under high scales, provide reliable and consistent data, and minimize our need for re-platforming in the future.</p><p>Technically, we applied the CDC pattern to ensure that the least load is placed on the PostgreSQL database running in the background. In this way, other teams who want to be informed about the changes made in the invoice domain will not have to call any APIs, so our database request cost will decrease.</p><p>In addition, we will not have to use the polling publish method for events that need to be published in any invoice change (which can be any CRUD transaction). Thus, we will be exempt from the disadvantages I wrote above.</p><h4>So what is CDC?</h4><p>Change data capture (CDC) is a set of software design patterns used to determine the data that has changed so that action can be taken using the changed data. CDC is a great fit for capturing new entries in the outbox table and stream them to any message broker. Those entries can then be applied to another data repository or event-based system. By allowing you to detect, capture, and deliver changed data, CDC reduces the time required for and resource costs of data warehousing while enabling continuous data integration. To learn more about the advantages of the CDC, refer to this <a href="https://debezium.io/blog/2018/07/19/advantages-of-log-based-change-data-capture/">blog post</a>.</p><p>We chose to publish our events to Kafka Message Broker via Kafka Connect to implement the Transaction Log Tailing structure. Some of the connectors supported by Kafka Connect;</p><ul><li>Debezium</li><li>Active MQ Source Connector</li><li>JDBC Connector</li><li>Elastic Search Sink Connector etc.</li></ul><p>We also used Debezium through these connectors. To implement the transactional log tailing method with CDC, we need to know what Kafka Connect and Debezium are. Finally, if we talk about these technologies;</p><h4>Kafka Connect</h4><p>Kafka Connect, an open-source component of Kafka, is a framework that supported by the Confluent company for connecting Kafka with external systems such as databases, key-value stores, search indexes, and file systems. Using Kafka Connect you can use existing connector implementations for common data sources and sinks to move data into and out of Kafka.</p><p>Source Connector: A source connector ingests entire databases and streams table updates to Kafka topics. It can also collect metrics from all of your application servers into Kafka topics, making the data available for stream processing with low latency.</p><p>Sink Connector: A sink connector delivers data from Kafka topics into secondary indexes such as Elasticsearch or batch systems such as Hadoop for offline analysis.</p><p>You can find more detailed information about Kafka on the <a href="https://docs.confluent.io/current/connect/index.html">link</a>.</p><h4>Debezium</h4><p>Debezium is a distributed platform that turns your existing databases into event streams, so applications can see and respond immediately to each row-level change in the databases. Debezium is built on top of Apache Kafka and provides Kafka Connect compatible connectors that monitor specific database management systems. Debezium records the history of data changes in Kafka logs, from where your application consumes them. This makes it possible for your application to easily consume all of the events correctly and completely. Even if your application stops (or crashes), upon restart it will start consuming the events where it left off so it misses nothing. In my next article, I will discuss in more detail how Debezium handled this situation.</p><p>Unlike other approaches such as polling or dual writes, log-based CDC as implemented by Debezium:</p><ul><li>Makes sure that all data changes are captured.</li><li>Produces change events with a very low delay (e.g. ms range for MySQL or Postgres) while avoiding increased CPU usage of frequent polling.</li><li>Requires no changes to your data model (such as the “last_updated” column).</li><li>Can capture deletes.</li><li>Can capture old record state and further metadata such as transaction id and causing a query (depending on the database’s capabilities and configuration).</li></ul><p>In my next post, I will explain how we can use Debezium in local docker and Kubernetes environments and what configurations we need to add.</p><p>I would also like to thanks <a href="https://medium.com/@cemalettinkaya90">Cemalettin Kaya</a> and <a href="https://medium.com/@onurkrcli">Onur Karacalı</a> for their help in this work.</p><p>If you have a will and tendency towards this kind of technologies, <a href="https://jobs.lever.co/trendyol">reach us</a>.</p><h3>References</h3><ul><li><a href="https://microservices.io/patterns/data/transactional-outbox.html">Microservices Pattern: Pattern: Transactional outbox</a></li><li><a href="https://debezium.io/documentation/reference/1.0/">Debezium Documentation</a></li><li><a href="https://debezium.io/blog/2019/02/19/reliable-microservices-data-exchange-with-the-outbox-pattern/">Reliable Microservices Data Exchange With the Outbox Pattern</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aeb968d72220" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/transaction-log-tailing-with-debezium-part-1-aeb968d72220">Transaction Log Tailing With Debezium — Part 1</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Step by Step Integration Tests on Spring Kafka Producer]]></title>
            <link>https://medium.com/trendyol-tech/how-to-integration-test-on-spring-kafka-producer-cb9d1caf0795?source=rss-9a13da8bf81f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cb9d1caf0795</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[integration-testing]]></category>
            <dc:creator><![CDATA[Abdullah YILDIRIM]]></dc:creator>
            <pubDate>Sun, 01 Sep 2019 19:49:19 GMT</pubDate>
            <atom:updated>2021-08-02T06:50:09.971Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*fwE03sLT0s4I_v63kSgHJw.png" /></figure><p>Nowadays, one of the most important things in development is testing our code. The code tests are classified according to the purpose: unit tests, integration tests, and contract tests, etc. We pay strict attention to follow the TDD technique at Trendyol, particularly with unit tests and integration tests.</p><p>In this article, I will explain step by step how we can write an integration test if we use Kafka Streaming Platform in our applications developed with Spring Boot. <a href="https://www.softwaretestinghelp.com/what-is-integration-testing/">Integration test</a>ing is defined as a type of testing where software modules are tested as a group and integrated logically.</p><h4><strong>Spring Kafka Producer Test</strong></h4><p>Spring Kafka Test is a Java Archive File that contains some helpful utilities to test your application. This jar has some useful methods for getting results and static methods for setting up the consumer/producer. In addition to this, there is an ‘EmbeddedKafkaBroker’ in this jar. I will address what ‘EmbeddedKafkaBroker’ is in the following section.</p><p>I will use Maven, Spring Boot, and Spring Kafka in the main project, and I will depend on the ‘spring kafka test’ in the test project. Let’s see how the test application can be used with a coding example.</p><h4><strong>Test Scenario</strong></h4><p>In e-commerce applications, the Product is the main domain. Brand data is an attribute of a Product. Whenever the Brand data changes, it must trigger some actions to be taken.</p><p>In my sample test scenario, I want to be notified when any brand change occurs. So, when some brands are added to or removed from the brands in the database, I will generate an event for other applications that should be consumed by this event. For this, I must first create the event model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c5b2f53757a951107a96be563618ea9/href">https://medium.com/media/9c5b2f53757a951107a96be563618ea9/href</a></iframe><h4><strong>Kafka Configuration</strong></h4><p>To download and install Kafka, I need to add the spring-kafka dependency to my pom.xml:</p><pre>&lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.kafka&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-kafka&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><p>I need to add the KafkaAdmin Spring bean, which will add topics for all beans of type NewTopic automatically:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb586a53c145a0818f7258ed5fd19463/href">https://medium.com/media/bb586a53c145a0818f7258ed5fd19463/href</a></iframe><p>The creation of the KafkaTemplate and KafkaProducerService is handled in the Configuration class. The class is annotated with @Configuration, indicating that the class can be used by the Spring IoC container as a source of bean definitions.</p><p>To use the Spring Kafka template, I need to configure a ProducerFactory and provide it in the template’s constructor.</p><h4><strong>Spring Kafka Test and Embedded Kafka</strong></h4><p>The spring-kafka-test jar contains some useful utilities to help in testing your applications. A JUnit 4 @Rule wrapper for the EmbeddedKafkaBroker is provided to create an embedded Kafka and an embedded Zookeeper server.</p><p>The EmbeddedKafkaRule constructor function takes the following values as parameters.</p><ul><li>The number of Kafka servers to start.</li><li>Whether a controlled shutdown is required.</li><li>Issues that need to be created on the server.</li></ul><h3><strong>Producer Test</strong></h3><p>Let’s create a sample test scenario. In this scenario, I will create a topic when any brand is updated. I will send a message on this topic and then test whether the message has been sent or not. This section will consist of three different methods. Two of these methods will trigger before and after the test.</p><h4>Before:</h4><p>The generated message needs to be kept in a queue. Therefore, I use the BlockingQueue object and use the container’s setupMessageListener method to consume the message. Then, I create two properties. The first one is a container with topic information, and the second one is a consumer with a group, autoCommit, and embedded server information. I send the consumerProperties object that I have created as a parameter to the constructor method when creating the DefaultKafkaConsumerFactory object. Then, consumer and containerProperties are conveyed to the KafkaMessageListenerContainer constructor. And then, the consumerProperties and DefaultKafkaConsumerFactory objects will be conveyed to the constructor method of the KafkaMessageListenerContainer object as a parameter. Finally, I transfer the messages I have created to the Blocking queue and the listener starts with the start of the container before starting the container.</p><blockquote>To avoid sending a message before the container has required the number of assigned partitions, and I use the waitForAssignment () method on the ContainerTestUtils class.</blockquote><blockquote>For the test to work properly, I use the @DirtiesContext annotation. DirtiesContext is a test annotation that indicates that the ApplicationContext associated with a test is dirty and accordingly, it should be closed and removed from the context cache.</blockquote><h4>Test:</h4><p>I need a data transfer object called UpdatedBrandDto to be notified of the brand change during the testing phase. After creating a UpdateBrandDto in my test method, I will check whether or not the message has been received to see if I can produce this object.</p><p>I use Hamcrest Matchers and AssertJ Condition to check the value.</p><h4>After:</h4><p>Now, I can stop the Container.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0de0be12b851c87e585d0357ca9a459/href">https://medium.com/media/c0de0be12b851c87e585d0357ca9a459/href</a></iframe><h3>Kafka Producer Service</h3><p>For this example, I will use the send() method that takes as input an UpdatedBrandEvent payload that needs to be sent. For sending messages, I use the KafkaTemplate which wraps a producer to send data to Kafka topics. The template provides asynchronous send methods which return a ListenableFuture. For details on the basics of Spring support for Apache Kafka, visit this <a href="https://www.baeldung.com/spring-kafka">link</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61a8d7787aa8cd8e0e8e9f1a6b518b96/href">https://medium.com/media/61a8d7787aa8cd8e0e8e9f1a6b518b96/href</a></iframe><p>If I want to block the sending thread and get the result about the sent message, I can call the get API of the ListenableFuture object. The thread will wait for the result, but it will slow down the producer. You can visit this <a href="https://www.baeldung.com/spring-kafka">link</a> for details of Spring Kafka.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29f2ec98b5ceea4b00b52ecaac1b0209/href">https://medium.com/media/29f2ec98b5ceea4b00b52ecaac1b0209/href</a></iframe><p>The source code of this article is also available in my <a href="https://github.com/yildirimabdullah/spring-kafka-test">GitHub</a> account.</p><h4>References</h4><ul><li><a href="https://www.baeldung.com/spring-kafka">Intro to Apache Kafka with Spring | Baeldung</a></li><li><a href="https://kafka.apache.org/20/javadoc/org/apache/kafka/clients/producer/ProducerConfig.html">ProducerConfig (kafka 2.0.0 API)</a></li><li><a href="https://www.programcreek.com/java-api-examples/?code=code-not-found/spring-kafka/spring-kafka-master/spring-kafka-test-embedded/src/test/java/com/codenotfound/kafka/consumer/SpringKafkaReceiverTest.java">SpringKafkaReceiverTest Java Source Code</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cb9d1caf0795" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/how-to-integration-test-on-spring-kafka-producer-cb9d1caf0795">Step by Step Integration Tests on Spring Kafka Producer</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>