{"id":90,"date":"2023-12-02T14:47:01","date_gmt":"2023-12-02T19:47:01","guid":{"rendered":"https:\/\/coding101.xyz\/?p=90"},"modified":"2023-12-02T19:57:08","modified_gmt":"2023-12-03T00:57:08","slug":"kafka-spring","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=90","title":{"rendered":"Kafka Spring"},"content":{"rendered":"\n<p>This shows how to create Kafka with Spring integration<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>First, the full maven configuration file: <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #557799\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<\/span>\n<span style=\"color: #007700\">&lt;project<\/span> <span style=\"color: #0000CC\">xmlns=<\/span><span style=\"background-color: #fff0f0\">&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot;<\/span> <span style=\"color: #0000CC\">xmlns:xsi=<\/span><span style=\"background-color: #fff0f0\">&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;<\/span>\n<span style=\"color: #0000CC\">xsi:schemaLocation=<\/span><span style=\"background-color: #fff0f0\">&quot;http:\/\/maven.apache.org\/POM\/4.0.0 https:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;<\/span><span style=\"color: #007700\">&gt;<\/span>\n<span style=\"color: #007700\">&lt;modelVersion&gt;<\/span>4.0.0<span style=\"color: #007700\">&lt;\/modelVersion&gt;<\/span>\n<span style=\"color: #007700\">&lt;parent&gt;<\/span>\n<span style=\"color: #007700\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n<span style=\"color: #007700\">&lt;artifactId&gt;<\/span>spring-boot-starter-parent<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700\">&lt;version&gt;<\/span>3.2.0<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n<span style=\"color: #007700\">&lt;relativePath\/&gt;<\/span> <span style=\"color: #888888\">&lt;!-- lookup parent from repository --&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/parent&gt;<\/span>\n<span style=\"color: #007700\">&lt;groupId&gt;<\/span>com.prod1<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n<span style=\"color: #007700\">&lt;artifactId&gt;<\/span>appl<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700\">&lt;version&gt;<\/span>0.0.1-SNAPSHOT<span style=\"color: #007700\">&lt;\/version&gt;<\/span>\n<span style=\"color: #007700\">&lt;name&gt;<\/span>appl<span style=\"color: #007700\">&lt;\/name&gt;<\/span>\n<span style=\"color: #007700\">&lt;description&gt;<\/span>Demo project for Spring Boot<span style=\"color: #007700\">&lt;\/description&gt;<\/span>\n<span style=\"color: #007700\">&lt;properties&gt;<\/span>\n<span style=\"color: #007700\">&lt;java.version&gt;<\/span>17<span style=\"color: #007700\">&lt;\/java.version&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/properties&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependencies&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n<span style=\"color: #007700\">&lt;artifactId&gt;<\/span>spring-boot-starter-web<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;dependency&gt;<\/span>\n<span style=\"color: #007700\">&lt;groupId&gt;<\/span>org.springframework.kafka<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n<span style=\"color: #007700\">&lt;artifactId&gt;<\/span>spring-kafka<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/dependency&gt;<\/span>\n\n<span style=\"color: #007700\">&lt;\/dependencies&gt;<\/span>\n\n<span style=\"color: #007700\">&lt;build&gt;<\/span>\n<span style=\"color: #007700\">&lt;plugins&gt;<\/span>\n<span style=\"color: #007700\">&lt;plugin&gt;<\/span>\n<span style=\"color: #007700\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700\">&lt;\/groupId&gt;<\/span>\n<span style=\"color: #007700\">&lt;artifactId&gt;<\/span>spring-boot-maven-plugin<span style=\"color: #007700\">&lt;\/artifactId&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/plugin&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/plugins&gt;<\/span>\n<span style=\"color: #007700\">&lt;\/build&gt;<\/span>\n\n<span style=\"color: #007700\">&lt;\/project&gt;<\/span>\n<\/pre><\/td><\/tr><\/table><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Configuration<\/h2>\n\n\n\n<p>The code configures some beans that allow creation of a KafkaTemplate for sending message and gives an example of how a listener is created with a simple annotation<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><table><tr><td><pre style=\"margin: 0; line-height: 125%\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39<\/pre><\/td><td><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">package<\/span> com<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">prod1<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">appl<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.apache.kafka.clients.producer.ProducerConfig<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.apache.kafka.common.serialization.StringSerializer<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.context.annotation.Bean<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.context.annotation.Configuration<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.kafka.annotation.KafkaListener<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.kafka.core.DefaultKafkaProducerFactory<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.kafka.core.KafkaTemplate<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.kafka.core.ProducerFactory<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">java.util.HashMap<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">java.util.Map<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #555555; font-weight: bold\">@Configuration<\/span>\n<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">KafkaConfig<\/span> <span style=\"color: #333333\">{<\/span>\n    <span style=\"color: #555555; font-weight: bold\">@Bean<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> KafkaTemplate<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> kafkaTemplate<span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">{<\/span>\n        <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> <span style=\"color: #0066BB; font-weight: bold\">KafkaTemplate<\/span><span style=\"color: #333333\">(<\/span>prodFactory<span style=\"color: #333333\">());<\/span>\n    <span style=\"color: #333333\">}<\/span>\n\n    <span style=\"color: #555555; font-weight: bold\">@Bean<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> ProducerFactory<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> prodFactory<span style=\"color: #333333\">()<\/span> <span style=\"color: #333333\">{<\/span>\n        Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> Object<span style=\"color: #333333\">&gt;<\/span> props <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HashMap <span style=\"color: #333333\">&lt;&gt;();<\/span>\n        props<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span>ProducerConfig<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">BOOTSTRAP_SERVERS_CONFIG<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;localhost:9092&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        props<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span>ProducerConfig<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">KEY_SERIALIZER_CLASS_CONFIG<\/span><span style=\"color: #333333\">,<\/span> StringSerializer<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">class<\/span><span style=\"color: #333333\">);<\/span>\n        props<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span>ProducerConfig<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">VALUE_SERIALIZER_CLASS_CONFIG<\/span><span style=\"color: #333333\">,<\/span> StringSerializer<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">class<\/span><span style=\"color: #333333\">);<\/span>\n\n        DefaultKafkaProducerFactory<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> prodFactory <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> DefaultKafkaProducerFactory<span style=\"color: #333333\">&lt;&gt;(<\/span>props<span style=\"color: #333333\">);<\/span>\n\n        <span style=\"color: #008800; font-weight: bold\">return<\/span> prodFactory<span style=\"color: #333333\">;<\/span>\n    <span style=\"color: #333333\">}<\/span>\n\n    <span style=\"color: #555555; font-weight: bold\">@KafkaListener<\/span><span style=\"color: #333333\">(<\/span>topics <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;tester1&quot;<\/span><span style=\"color: #333333\">,<\/span> groupId <span style=\"color: #333333\">=<\/span> <span style=\"background-color: #fff0f0\">&quot;tester&quot;<\/span><span style=\"color: #333333\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #333399; font-weight: bold\">void<\/span> <span style=\"color: #0066BB; font-weight: bold\">listen<\/span><span style=\"color: #333333\">(<\/span>String message<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n        System<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">out<\/span><span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">println<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;msg: &quot;<\/span> <span style=\"color: #333333\">+<\/span> message<span style=\"color: #333333\">);<\/span>\n    <span style=\"color: #333333\">}<\/span>\n\n<span style=\"color: #333333\">}<\/span>\n<\/pre><\/td><\/tr><\/table><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This shows how to create Kafka with Spring integration<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=90"}],"version-history":[{"count":2,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}