<?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 Ekarin Prasoppol on Medium]]></title>
        <description><![CDATA[Stories by Ekarin Prasoppol on Medium]]></description>
        <link>https://medium.com/@ekarin.pra?source=rss-4632e3f196dd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*oqtiaJhjJ90URSTa.jpg</url>
            <title>Stories by Ekarin Prasoppol on Medium</title>
            <link>https://medium.com/@ekarin.pra?source=rss-4632e3f196dd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:31:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ekarin.pra/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[Debug Reactive Streams ใน Spring WebFlux ด้วย Reactor Debug Agent]]></title>
            <link>https://medium.com/ascend-developers/debug-reactive-streams-%E0%B9%83%E0%B8%99-spring-webflux-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-reactor-debug-agent-6de6373954fa?source=rss-4632e3f196dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/6de6373954fa</guid>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Ekarin Prasoppol]]></dc:creator>
            <pubDate>Fri, 26 Dec 2025 08:27:56 GMT</pubDate>
            <atom:updated>2025-12-26T08:27:56.133Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OwxKh_yqj26oITdt1BN6vA.png" /></figure><p>Reactive Programming ด้วย <strong>Spring WebFlux</strong> และ <strong>Project Reactor</strong> ช่วยให้เราสร้างระบบที่รองรับ concurrent workload ได้ดี ใช้ resource คุ้ม และ scale ได้ง่าย แต่ที่หลายคนบ่นเหมือนกันคือ</p><blockquote>“ตอน Error มัน Debug ยากมาก”</blockquote><p>Stacktrace ของ reactive pipeline มักไม่บอกเราตรง ๆ ว่า</p><ul><li>error เกิดจาก business logic จุดไหน</li><li>operator ไหนเป็นต้นเหตุ</li><li>หรือ reactive chain วิ่งมาถึงตรงนี้ได้อย่างไร</li></ul><p>ผลลัพธ์คือ developer ต้องไล่เดา flow จาก .map() .flatMap() ที่ต่อกันยาว ๆ ซึ่งเสียเวลาและทำให้การแก้ปัญหาช้าลงอย่างมาก</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tRcZKX22A8WHFx6pc78aZg.png" /></figure><p>บทความนี้จะพาไปทำความรู้จัก <strong>Reactor Debug Agent</strong> พร้อมเทคนิคสำคัญอย่าง .checkpoint() และ Hooks.onOperatorDebug()ผ่านตัวอย่างโค้ด และการอธิบายทีละบรรทัด เพื่อให้คุณสามารถ</p><ul><li>อ่าน stacktrace ของ reactive ได้เหมือน synchronous code</li><li>pinpoint ตำแหน่ง error ได้เร็วขึ้น</li><li>และ debug WebFlux application ได้อย่างมั่นใจมากขึ้น</li></ul><blockquote><em>เป้าหมายไม่ใช่แค่ “รู้ว่า error คืออะไร”<br> แต่คือ “รู้ว่ามันเกิดขึ้นได้อย่างไร และควรแก้ตรงไหน”</em></blockquote><h3>Step 1: เตรียมโปรเจกต์</h3><h3>เพิ่ม dependency ใน pom.xml</h3><pre>&lt;dependency&gt;<br>  &lt;groupId&gt;io.projectreactor.tools&lt;/groupId&gt;<br>  &lt;artifactId&gt;reactor-tools&lt;/artifactId&gt;<br>  &lt;version&gt;3.5.6&lt;/version&gt;<br>&lt;/dependency&gt;</pre><h3>Step 2: เปิด Debug Mode ตั้งแต่แอปเริ่มรัน</h3><pre>@SpringBootApplication<br>public class DemoApplication {<br>public static void main(String[] args) {<br>      Hooks.onOperatorDebug();<br>      ReactorDebugAgent.init();<br>      ReactorDebugAgent.processExistingClasses();</pre><pre>      SpringApplication.run(DemoApplication.class, args);<br>  }<br>}</pre><ul><li>เรียกใช้ Hooks.onOperatorDebug() เพื่อเปิดโหมด debug ของ reactive operator ทุกตัวในแอปฯ สำหรับเก็บข้อมูล stacktrace</li><li>เริ่มต้น Reactor Debug Agent ด้วย ReactorDebugAgent.init() เพื่อช่วยเพิ่มข้อมูล stacktrace ที่ชัดเจนและเชื่อมโยงข้อผิดพลาดกลับไปยังโค้ดต้นเหตุ</li><li>สั่งให้ agent สแกน class ที่โหลดแล้วด้วย ReactorDebugAgent.processExistingClasses() เพื่อ instrument operator ที่ถูกโหลดก่อนหน้านี้ (ถ้าไม่เรียก อาจมี operator บางตัวไม่ถูก instrument)</li></ul><h3>Step 3: ตัวอย่าง Reactive Flow ที่จะเกิด Exception</h3><pre>@GetMapping(&quot;/calculate&quot;)<br>public Mono&lt;String&gt; calculate() {<br>    return Mono.just(10)<br>               .map(i -&gt; i - 10)<br>               .map(i -&gt; 10 / i)<br>               .checkpoint(&quot;Divide calculation&quot;)<br>               .map(String::valueOf);<br>}</pre><h4>อธิบายทีล่ะบรรทัด</h4><pre>Mono.just(10)</pre><p>สร้าง Mono ที่ปล่อยค่าเดียวคือ 10</p><pre>.map(i -&gt; i - 10)</pre><p>แปลงค่า 10 → 0<br> ตอนนี้ยังไม่มี error</p><pre>.map(i -&gt; 10 / i)</pre><p>เกิดการคำนวณ 10 / 0<br> 💥 เกิด ArithmeticException ที่บรรทัดนี้</p><pre>.checkpoint(&quot;Divide calculation&quot;)</pre><p>ใส่ป้ายชื่อให้ pipeline<br> ถ้าเกิด error Stacktrace จะบอกว่า<br> 👉 “พังที่ checkpoint: Divide calculation”</p><pre>.map(String::valueOf)</pre><p>บรรทัดนี้ <strong>จะไม่ถูกเรียก</strong> เพราะ error เกิดก่อนหน้าแล้ว</p><h3>Step 4: ดู Stacktrace แบบ “อ่านรู้เรื่อง”</h3><h3>❌ ถ้าไม่ใช้ Debug Agent</h3><pre>java.lang.ArithmeticException: / by zero<br>    at reactor.core.publisher.FluxMap$MapSubscriber.onNext<br>    ...</pre><ul><li>ไม่รู้ว่า error มาจาก map ไหน</li><li>ไม่รู้ logic ธุรกิจ</li><li>แก้ยากมาก</li></ul><h3>✅ เมื่อใช้ Debug Agent + checkpoint</h3><pre>java.lang.ArithmeticException: / by zero<br>	at com.example.DemoController.lambda$calculate$1(DemoController.java:18)</pre><pre>Reactor operator stacktrace:<br>--- Operator checkpoint: Divide calculation</pre><p>🎯 <strong>รู้ทันที</strong></p><ul><li>พังที่ method ไหน</li><li>พังใน business logic จุดไหน</li><li>ไม่ต้องไล่เดา reactive chain</li></ul><h3>Step 6: ตัวอย่าง เพิ่มอีกหนึ่งเคส (DB / Service)</h3><pre>public Mono&lt;User&gt; findUser(String id) {<br>    return userRepository.findById(id)<br>        .checkpoint(&quot;DB: findUserById&quot;)<br>        .switchIfEmpty(Mono.error(new RuntimeException(&quot;User not found&quot;)));<br>}</pre><h3>ถ้าเกิด error จะเห็นว่า</h3><pre>--- Operator checkpoint: DB: findUserById</pre><p>📍 แปลว่า error นี้มาจาก database layer<br> ไม่ใช่ controller<br> ไม่ใช่ downstream service</p><h3>ปิดท้าย</h3><p>Reactive Programming ไม่ได้ทำให้การ debug เป็นไปไม่ได้ แต่ต้องใช้วิธีคิดและเครื่องมือที่เหมาะสม</p><p>ปัญหาที่หลายทีมเจอไม่ใช่ Reactive เอง แต่คือ <strong>stacktrace ที่ขาด context</strong> ทำให้ไม่รู้ว่า error เกิดจาก business logic ตรงไหนของ pipeline</p><p><strong>Reactor Debug Agent</strong> ช่วยเติมช่องว่างนี้ ด้วยการเชื่อม reactive operator กลับไปยังโค้ดจริงที่เขียนไว้<br> เมื่อใช้งานร่วมกับ .checkpoint() และ Hooks.onOperatorDebug() เราจะสามารถ</p><ul><li>pinpoint ตำแหน่งที่ error เกิดขึ้นได้เร็วขึ้น</li><li>อ่าน stacktrace ของ reactive code ได้เหมือน synchronous code มากขึ้น</li><li>ลดเวลาในการไล่ debug และการเดา flow ของระบบ</li></ul><p>อย่างไรก็ตาม เครื่องมือเหล่านี้มี overhead และควรใช้งานเฉพาะใน <strong>development หรือ troubleshooting environment</strong><br>การเลือกใช้ให้เหมาะสมในแต่ละสถานการณ์ จะช่วยให้ทีมได้ประโยชน์จาก Reactive Programming โดยไม่เพิ่มความซับซ้อนเกินจำเป็น</p><blockquote><em>Reactive ไม่ได้ยากขึ้น<br> แค่ต้อง debug ให้ถูกวิธี</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6de6373954fa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ascend-developers/debug-reactive-streams-%E0%B9%83%E0%B8%99-spring-webflux-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-reactor-debug-agent-6de6373954fa">Debug Reactive Streams ใน Spring WebFlux ด้วย Reactor Debug Agent</a> was originally published in <a href="https://medium.com/ascend-developers">Ascend Developers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>