<?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 Akhil Bojedla on Medium]]></title>
        <description><![CDATA[Stories by Akhil Bojedla on Medium]]></description>
        <link>https://medium.com/@akhil.bojedla?source=rss-2c41445d423f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dElpGNrRANp-5SPDSXlxNQ@2x.jpeg</url>
            <title>Stories by Akhil Bojedla on Medium</title>
            <link>https://medium.com/@akhil.bojedla?source=rss-2c41445d423f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 07:55:04 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akhil.bojedla/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[Start, Stop, Pause and Resume Spring Kafka consumer at runtime]]></title>
            <link>https://medium.com/@akhil.bojedla/start-stop-pause-and-resume-spring-kafka-consumer-at-runtime-45b44b9be44b?source=rss-2c41445d423f------2</link>
            <guid isPermaLink="false">https://medium.com/p/45b44b9be44b</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[spring-kafka]]></category>
            <dc:creator><![CDATA[Akhil Bojedla]]></dc:creator>
            <pubDate>Sat, 22 Oct 2022 20:53:32 GMT</pubDate>
            <atom:updated>2022-10-25T13:26:34.111Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*oK9FH_z1q_jgEJgD8dqHRw.png" /></figure><h3>Introduction</h3><p>Kafka is one of the widely adapted tools across companies as a real-time data streaming platform. Naturally, Spring-boot has a easy to use Kafka integration abstracting away a lot of complexity. By default, Spring Kafka consumer runs the consumer.poll()<em> </em>in a continous loop polling records from the Kafka topic.</p><p>In this article, I will attempt to explain how we can start/stop/pause/resume spring-kafka consumer. This is especially useful when we wish to control the Kafka consumer at runtime. Few use cases include stopping/pausing consumers during maintenance, stopping consumer to reset Kafka consumer offsets etc.</p><h3>Dependencies</h3><p>This article assumes that the service is already setup with an active Kafka consumer. If not, you may follow <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/messaging.html#messaging.kafka">this article</a> to setup a new Kafka consumer in your Spring-boot service.</p><h4>Additional dependencies</h4><p>In addition, this demo uses Spring web MVC to create a REST API which we will use to notify the consumer to start/stop/pause/resume, Spring data JPA to store and retrieve the current desired state of consumer &amp; Spring’s @Scheduled annotation. Needless to say, you may switch these additional dependencies with any comparable alternatives.</p><h3>Implementation</h3><h4>Kafka consumer</h4><p>When setting up the consumer we need to set some additional parameters in @KafkaListener annotation namely id and autoStartup . The id must be a unique identifier for this particular consumer (We will later use this to control the lifecycle of this consumer). Setting autoStartup as false ensures that the Kafka consumer is not started automatically when the applications starts.</p><blockquote>Note: I will be using Kotlin as language of choice throughout this article. I will only be including code snippets that are absolutely necessary to explain the flow. For the complete code please refer to the repository <a href="https://github.com/bytecodehq/kafka-consumer-toggle-demo">here</a>.</blockquote><pre>@Component<br>class SampleConsumer {<br><br>    companion object {<br>        const val SAMPLE_CONSUMER_ID = &quot;sample-consumer&quot;<br>        private val logger = LoggerFactory.getLogger(SampleConsumer::class.java)<br>    }<br><br>    @KafkaListener(<br>        id = SAMPLE_CONSUMER_ID,<br>        topics = [&quot;sample-topic&quot;],<br>        groupId = &quot;bytecodehq&quot;,<br><br>        // To avoid starting the consumer during application startup.<br>        // We must start the consumer depending on the current desired state.<br>        autoStartup = &quot;false&quot;<br>    )<br>    fun onMessage(<br>        record: ConsumerRecord&lt;String, String&gt;<br>    ) {<br>       logger.info(&quot;Consumed message -&gt; Key=${record.key()} Value=${record.value()}&quot;)<br>    }<br>}</pre><h4>API to update consumer state</h4><p>Now, let’s create the REST API that lets user take control of the lifecycle of the consumer.</p><blockquote>Note that we are not updating the consumer state directly but rather saving the desired consumer state to a database table. This state will be eventually applied to consumer using a scheduler task (will be discussed in later section of this article). This is to make sure that we don’t lose the desired state of the consumer during application scaling/restarts.</blockquote><pre>@RestController<br>@RequestMapping(&quot;/kafkaConsumer/update&quot;)<br>class UpdateKafkaConsumerStateController(<br>    private val kafkaConsumerStateRepository: KafkaConsumerStateRepository<br>) {<br><br>    @PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE])<br>    fun toggleKafkaConsumer(<br>        @RequestBody updateKafkaConsumerStateRequest: UpdateKafkaConsumerStateRequest<br>    ): ResponseEntity&lt;Void&gt; {<br>        kafkaConsumerStateRepository.saveConsumerState(<br>            consumerId = updateKafkaConsumerStateRequest.consumerId,<br>            state = updateKafkaConsumerStateRequest.desiredState<br>        )<br>        return ResponseEntity.ok().build()<br>    }<br>}</pre><h4>Eventually applying consumer state</h4><p>The next step is to create a periodically running task that checks the current desired state of consumer and apply it to the consumer instance. This is the part where the actual magic happens. Depending on the last set desired state of the consumer the task would apply the state to consumer.</p><pre>@Component<br>@EnableScheduling<br>class KafkaConsumerStateScheduler(<br>    private val listenerEndpointRegistry: KafkaListenerEndpointRegistry,<br>    private val kafkaConsumerStateRepository: KafkaConsumerStateRepository<br>) {<br><br>    companion object {<br>        private val logger = LoggerFactory.getLogger(KafkaConsumerStateScheduler::class.java)<br>    }<br><br>    @Scheduled(<br>        initialDelay = 0,<br>        fixedDelayString = &quot;1000&quot;<br>    )<br>    fun toggleSampleConsumer() {<br>        val containerId = SampleConsumer.SAMPLE_CONSUMER_ID<br>        val container = listenerEndpointRegistry.getListenerContainer(containerId)<br>            ?: throw IllegalStateException(&quot;Kafka consumer with id $containerId not found&quot;)<br><br>        val desiredConsumerState = kafkaConsumerStateRepository.getConsumerState(containerId) ?: KafkaConsumerState.START<br><br>        when (desiredConsumerState) {<br>            KafkaConsumerState.START -&gt; container.start()<br>            KafkaConsumerState.STOP -&gt; container.stop()<br>            KafkaConsumerState.PAUSE -&gt; container.pause()<br>            KafkaConsumerState.RESUME -&gt; container.resume()<br>        }<br><br>        logger.info(&quot;Updated consumer $containerId state to $desiredConsumerState&quot;)<br>    }<br>}</pre><p>By default, the consumer state is set to START . So when the application starts the consumer is started on the first run of the KafkaConsumerStateScheduler task.</p><pre>INFO 53585 --- [   scheduling-1] o.a.k.clients.consumer.KafkaConsumer     : [Consumer clientId=consumer-bytecodehq-1, groupId=bytecodehq] Subscribed to topic(s): sample-topic<br>INFO 53585 --- [   scheduling-1] c.b.d.s.KafkaConsumerStateScheduler      : Updated consumer sample-consumer state to START</pre><p>We can stop the consumer by making an API call to the above created API with respective consumer id and desired state. To stop the consumer we make the following API request:</p><pre>curl -X POST --location &quot;<a href="http://localhost:8080/kafkaConsumer/update">http://localhost:8080/kafkaConsumer/update</a>&quot; \<br>    -H &quot;Content-Type: application/json&quot; \<br>    -d &quot;{<br>          \&quot;consumerId\&quot;: \&quot;sample-consumer\&quot;,<br>          \&quot;desiredState\&quot;: \&quot;STOP\&quot;<br>        }&quot;</pre><p>This updates the desired state of the consumer in the database table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/326/1*7Z5GigCFH_kSWaISunWAog.png" /><figcaption>Updated desired state in database table</figcaption></figure><p>This state is then picked up on the next run of the KafkaConsumerStateScheduler task and applies it to the running consumer there by effectively stopping the consumer.</p><pre>INFO 53585 --- [onsumerId-0-C-1] o.a.kafka.common.utils.AppInfoParser     : App info kafka.consumer for consumer-bytecodehq-1 unregistered<br>INFO 53585 --- [onsumerId-0-C-1] o.s.k.l.KafkaMessageListenerContainer    : bytecodehq: Consumer stopped<br>INFO 53585 --- [   scheduling-1] c.b.d.s.KafkaConsumerStateScheduler      : Updated consumer sample-consumer state to STOP</pre><h3>Conclusion</h3><p>This is just one of the many possible ways by which we can reliably control the Kafka consumer state. Obviously, some of the components can be replaced with other alternatives of your choice. For example, The REST API we created is just an interface for us to be able to notify the consumer of desired state change &amp; the database to store the this state.</p><p>You can find the complete code for this demo <a href="https://github.com/bytecodehq/kafka-consumer-toggle-demo">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45b44b9be44b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exception handling — Spring WebFlux]]></title>
            <link>https://medium.com/@akhil.bojedla/exception-handling-spring-webflux-b11647d8608?source=rss-2c41445d423f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b11647d8608</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-framework]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring-webflux]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Akhil Bojedla]]></dc:creator>
            <pubDate>Wed, 09 Oct 2019 22:42:13 GMT</pubDate>
            <atom:updated>2019-10-12T00:03:43.969Z</atom:updated>
            <content:encoded><![CDATA[<h3>Exception handling — Spring WebFlux</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*nhxekvtqvKZENIOH" /></figure><p>Exception handling is a mechanism to handle run-time errors. The core advantage of exception handling is to maintain the normal flow of the application. When developing web applications handling exceptions is important especially when you want to control what you want to show the user.</p><p>In this article, we will be looking at various strategies that one can adapt to handle exceptions when using Spring WebFlux. I will also try to drop hints on which strategy might fit better in certain well known scenarios.</p><h3>Inline Error handling</h3><p>There are two operators built into the Publisher&lt;&gt;API to handle errors.</p><h4>onErrorResume</h4><p>Use onErrorResumewhen you would like to subscribe to a fallback publisher on error matching the given conditions. There are various overloaded operators of onErrorResumethat we will be discussing below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c599ef9dd19e3dadd86371dea1daabc6/href">https://medium.com/media/c599ef9dd19e3dadd86371dea1daabc6/href</a></iframe><h4>onErrorReturn</h4><p>Use onErrorReturn to emit a static value when an error is observed.</p><p>onErrorReturn has similar overloaded methods as onErrorResume .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd4770bda10258c1a1819d7797def957/href">https://medium.com/media/dd4770bda10258c1a1819d7797def957/href</a></iframe><h3>Global exception handling</h3><p>You can also write a global exception handler to handle all the unhandled exceptions from all the routes. The following block of code handles all unhandled exceptions coming from your routes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81bc8473d0a02476cbf1c88d9d93cbae/href">https://medium.com/media/81bc8473d0a02476cbf1c88d9d93cbae/href</a></iframe><p>In the above example, it is to be observed that we are handling IOException and putting a custom message as JSON and with 400 status code in response. For other exceptions, we are putting a plain text message Unknown error with a 500 response code.</p><p>It is also important to notice that we put @Order(-2) to make our error handler take precedence over Spring boot’s DefaultErrorWebExceptionHandler which is registered at @Order(-1) by ErrorWebFluxAutoConfiguration. Alternatively, you can choose to avoid the @Order annotation by excluding this auto-configuration in Spring-boot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d2913f7d6caa78d0ca7879a4a7b6685/href">https://medium.com/media/6d2913f7d6caa78d0ca7879a4a7b6685/href</a></iframe><p>Writing a global exception handler is a good idea especially when you have a strong structured contract with your clients.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b11647d8608" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>