[{"data":1,"prerenderedAt":4527},["ShallowReactive",2],{"post-mastering-concurrency":3,"rel-mastering-concurrency":1336,"sib-mastering-concurrency":4455},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":12,"image":13,"optimized_image":13,"category":14,"tags":15,"author":19,"paginate":6,"body":20,"_type":1330,"_id":1331,"_source":1332,"_file":1333,"_stem":1334,"_extension":1335},"/posts/mastering-concurrency","posts",false,"","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","Exploring Java’s concurrency model with detailed insights into Threads, ExecutorService, and Virtual Threads from Project Loom.","2024-03-15T11:00:00.000Z","post","A Deep Dive into Java’s Multithreading and Parallel Computing","/assets/img/uploads/java-concurrency.png","code",[14,16,17,18],"java","concurrency","multithreading","jaimedearcos",{"type":21,"children":22,"toc":1313},"root",[23,35,42,47,103,116,122,150,432,439,472,477,483,503,509,514,815,821,868,874,892,898,903,909,942,947,1212,1218,1236,1242,1247,1252,1258,1302,1307],{"type":24,"tag":25,"props":26,"children":27},"element","blockquote",{},[28],{"type":24,"tag":29,"props":30,"children":31},"p",{},[32],{"type":33,"value":34},"text","Java's concurrency model is a critical component for building scalable and efficient applications. From classic Thread management to modern ExecutorService and the revolutionary Virtual Threads in Project Loom, mastering Java’s concurrency tools is essential for high-performance development.",{"type":24,"tag":36,"props":37,"children":39},"h2",{"id":38},"understanding-javas-concurrency-model",[40],{"type":33,"value":41},"Understanding Java’s Concurrency Model",{"type":24,"tag":29,"props":43,"children":44},{},[45],{"type":33,"value":46},"Concurrency in Java enables applications to perform multiple tasks simultaneously, improving throughput and responsiveness. Java provides several concurrency primitives:",{"type":24,"tag":48,"props":49,"children":50},"ul",{},[51,63,73,83,93],{"type":24,"tag":52,"props":53,"children":54},"li",{},[55,61],{"type":24,"tag":56,"props":57,"children":58},"strong",{},[59],{"type":33,"value":60},"Threads",{"type":33,"value":62},": The fundamental unit of execution, allowing tasks to run in parallel.",{"type":24,"tag":52,"props":64,"children":65},{},[66,71],{"type":24,"tag":56,"props":67,"children":68},{},[69],{"type":33,"value":70},"Executors",{"type":33,"value":72},": High-level abstraction for managing thread pools and task execution.",{"type":24,"tag":52,"props":74,"children":75},{},[76,81],{"type":24,"tag":56,"props":77,"children":78},{},[79],{"type":33,"value":80},"Virtual Threads",{"type":33,"value":82},": Lightweight, memory-efficient threads introduced in Java 21 for massive scalability.",{"type":24,"tag":52,"props":84,"children":85},{},[86,91],{"type":24,"tag":56,"props":87,"children":88},{},[89],{"type":33,"value":90},"Fork/Join Framework",{"type":33,"value":92},": Designed for divide-and-conquer algorithms using parallel task execution.",{"type":24,"tag":52,"props":94,"children":95},{},[96,101],{"type":24,"tag":56,"props":97,"children":98},{},[99],{"type":33,"value":100},"Reactive Programming",{"type":33,"value":102},": Asynchronous, non-blocking programming using event streams.",{"type":24,"tag":29,"props":104,"children":105},{},[106,108,114],{"type":33,"value":107},"Let's now take a deeper look at three major building blocks of concurrent programming in Java: classic threads, the ",{"type":24,"tag":14,"props":109,"children":111},{"className":110},[],[112],{"type":33,"value":113},"ExecutorService",{"type":33,"value":115},", and the new virtual threads.",{"type":24,"tag":36,"props":117,"children":119},{"id":118},"classic-approach-java-threads",[120],{"type":33,"value":121},"Classic Approach: Java Threads",{"type":24,"tag":29,"props":123,"children":124},{},[125,127,133,135,140,142,148],{"type":33,"value":126},"The most basic unit of concurrency in Java is the ",{"type":24,"tag":14,"props":128,"children":130},{"className":129},[],[131],{"type":33,"value":132},"Thread",{"type":33,"value":134},". You can extend the ",{"type":24,"tag":14,"props":136,"children":138},{"className":137},[],[139],{"type":33,"value":132},{"type":33,"value":141}," class or implement the ",{"type":24,"tag":14,"props":143,"children":145},{"className":144},[],[146],{"type":33,"value":147},"Runnable",{"type":33,"value":149}," interface to define concurrent tasks.",{"type":24,"tag":151,"props":152,"children":155},"pre",{"className":153,"code":154,"language":16,"meta":7,"style":7},"language-java shiki shiki-themes github-dark github-light","class SimpleThreadExample extends Thread {\n    public void run() {\n        System.out.println(\"Thread running: \" + Thread.currentThread().getName());\n    }\n}\n\npublic class ThreadDemo {\n    public static void main(String[] args) {\n        SimpleThreadExample thread = new SimpleThreadExample();\n        thread.start();\n    }\n}\n",[156],{"type":24,"tag":14,"props":157,"children":158},{"__ignoreMap":7},[159,193,217,272,281,290,300,323,370,398,416,424],{"type":24,"tag":160,"props":161,"children":164},"span",{"class":162,"line":163},"line",1,[165,171,177,182,187],{"type":24,"tag":160,"props":166,"children":168},{"style":167},"--shiki-default:#F97583;--shiki-light:#D73A49",[169],{"type":33,"value":170},"class",{"type":24,"tag":160,"props":172,"children":174},{"style":173},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[175],{"type":33,"value":176}," SimpleThreadExample",{"type":24,"tag":160,"props":178,"children":179},{"style":167},[180],{"type":33,"value":181}," extends",{"type":24,"tag":160,"props":183,"children":184},{"style":173},[185],{"type":33,"value":186}," Thread",{"type":24,"tag":160,"props":188,"children":190},{"style":189},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[191],{"type":33,"value":192}," {\n",{"type":24,"tag":160,"props":194,"children":196},{"class":162,"line":195},2,[197,202,207,212],{"type":24,"tag":160,"props":198,"children":199},{"style":167},[200],{"type":33,"value":201},"    public",{"type":24,"tag":160,"props":203,"children":204},{"style":167},[205],{"type":33,"value":206}," void",{"type":24,"tag":160,"props":208,"children":209},{"style":173},[210],{"type":33,"value":211}," run",{"type":24,"tag":160,"props":213,"children":214},{"style":189},[215],{"type":33,"value":216},"() {\n",{"type":24,"tag":160,"props":218,"children":220},{"class":162,"line":219},3,[221,226,231,236,242,247,252,257,262,267],{"type":24,"tag":160,"props":222,"children":223},{"style":189},[224],{"type":33,"value":225},"        System.out.",{"type":24,"tag":160,"props":227,"children":228},{"style":173},[229],{"type":33,"value":230},"println",{"type":24,"tag":160,"props":232,"children":233},{"style":189},[234],{"type":33,"value":235},"(",{"type":24,"tag":160,"props":237,"children":239},{"style":238},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[240],{"type":33,"value":241},"\"Thread running: \"",{"type":24,"tag":160,"props":243,"children":244},{"style":167},[245],{"type":33,"value":246}," +",{"type":24,"tag":160,"props":248,"children":249},{"style":189},[250],{"type":33,"value":251}," Thread.",{"type":24,"tag":160,"props":253,"children":254},{"style":173},[255],{"type":33,"value":256},"currentThread",{"type":24,"tag":160,"props":258,"children":259},{"style":189},[260],{"type":33,"value":261},"().",{"type":24,"tag":160,"props":263,"children":264},{"style":173},[265],{"type":33,"value":266},"getName",{"type":24,"tag":160,"props":268,"children":269},{"style":189},[270],{"type":33,"value":271},"());\n",{"type":24,"tag":160,"props":273,"children":275},{"class":162,"line":274},4,[276],{"type":24,"tag":160,"props":277,"children":278},{"style":189},[279],{"type":33,"value":280},"    }\n",{"type":24,"tag":160,"props":282,"children":284},{"class":162,"line":283},5,[285],{"type":24,"tag":160,"props":286,"children":287},{"style":189},[288],{"type":33,"value":289},"}\n",{"type":24,"tag":160,"props":291,"children":293},{"class":162,"line":292},6,[294],{"type":24,"tag":160,"props":295,"children":297},{"emptyLinePlaceholder":296},true,[298],{"type":33,"value":299},"\n",{"type":24,"tag":160,"props":301,"children":303},{"class":162,"line":302},7,[304,309,314,319],{"type":24,"tag":160,"props":305,"children":306},{"style":167},[307],{"type":33,"value":308},"public",{"type":24,"tag":160,"props":310,"children":311},{"style":167},[312],{"type":33,"value":313}," class",{"type":24,"tag":160,"props":315,"children":316},{"style":173},[317],{"type":33,"value":318}," ThreadDemo",{"type":24,"tag":160,"props":320,"children":321},{"style":189},[322],{"type":33,"value":192},{"type":24,"tag":160,"props":324,"children":326},{"class":162,"line":325},8,[327,331,336,340,345,349,354,359,365],{"type":24,"tag":160,"props":328,"children":329},{"style":167},[330],{"type":33,"value":201},{"type":24,"tag":160,"props":332,"children":333},{"style":167},[334],{"type":33,"value":335}," static",{"type":24,"tag":160,"props":337,"children":338},{"style":167},[339],{"type":33,"value":206},{"type":24,"tag":160,"props":341,"children":342},{"style":173},[343],{"type":33,"value":344}," main",{"type":24,"tag":160,"props":346,"children":347},{"style":189},[348],{"type":33,"value":235},{"type":24,"tag":160,"props":350,"children":351},{"style":167},[352],{"type":33,"value":353},"String",{"type":24,"tag":160,"props":355,"children":356},{"style":189},[357],{"type":33,"value":358},"[] ",{"type":24,"tag":160,"props":360,"children":362},{"style":361},"--shiki-default:#FFAB70;--shiki-light:#E36209",[363],{"type":33,"value":364},"args",{"type":24,"tag":160,"props":366,"children":367},{"style":189},[368],{"type":33,"value":369},") {\n",{"type":24,"tag":160,"props":371,"children":373},{"class":162,"line":372},9,[374,379,384,389,393],{"type":24,"tag":160,"props":375,"children":376},{"style":189},[377],{"type":33,"value":378},"        SimpleThreadExample thread ",{"type":24,"tag":160,"props":380,"children":381},{"style":167},[382],{"type":33,"value":383},"=",{"type":24,"tag":160,"props":385,"children":386},{"style":167},[387],{"type":33,"value":388}," new",{"type":24,"tag":160,"props":390,"children":391},{"style":173},[392],{"type":33,"value":176},{"type":24,"tag":160,"props":394,"children":395},{"style":189},[396],{"type":33,"value":397},"();\n",{"type":24,"tag":160,"props":399,"children":401},{"class":162,"line":400},10,[402,407,412],{"type":24,"tag":160,"props":403,"children":404},{"style":189},[405],{"type":33,"value":406},"        thread.",{"type":24,"tag":160,"props":408,"children":409},{"style":173},[410],{"type":33,"value":411},"start",{"type":24,"tag":160,"props":413,"children":414},{"style":189},[415],{"type":33,"value":397},{"type":24,"tag":160,"props":417,"children":419},{"class":162,"line":418},11,[420],{"type":24,"tag":160,"props":421,"children":422},{"style":189},[423],{"type":33,"value":280},{"type":24,"tag":160,"props":425,"children":427},{"class":162,"line":426},12,[428],{"type":24,"tag":160,"props":429,"children":430},{"style":189},[431],{"type":33,"value":289},{"type":24,"tag":433,"props":434,"children":436},"h3",{"id":435},"limitations-of-raw-threads",[437],{"type":33,"value":438},"Limitations of Raw Threads",{"type":24,"tag":48,"props":440,"children":441},{},[442,452,462],{"type":24,"tag":52,"props":443,"children":444},{},[445,450],{"type":24,"tag":56,"props":446,"children":447},{},[448],{"type":33,"value":449},"Performance Overhead",{"type":33,"value":451},": Creating and destroying threads is expensive.",{"type":24,"tag":52,"props":453,"children":454},{},[455,460],{"type":24,"tag":56,"props":456,"children":457},{},[458],{"type":33,"value":459},"No Pooling",{"type":33,"value":461},": Each thread is a one-off. No built-in reuse.",{"type":24,"tag":52,"props":463,"children":464},{},[465,470],{"type":24,"tag":56,"props":466,"children":467},{},[468],{"type":33,"value":469},"Scalability Issues",{"type":33,"value":471},": System resources limit the number of threads you can create.",{"type":24,"tag":29,"props":473,"children":474},{},[475],{"type":33,"value":476},"Using raw threads for large-scale concurrent applications quickly becomes impractical.",{"type":24,"tag":36,"props":478,"children":480},{"id":479},"modern-concurrency-executorservice-and-thread-pooling",[481],{"type":33,"value":482},"Modern Concurrency: ExecutorService and Thread Pooling",{"type":24,"tag":29,"props":484,"children":485},{},[486,488,493,495,501],{"type":33,"value":487},"To solve the limitations of raw threads, Java introduced the ",{"type":24,"tag":14,"props":489,"children":491},{"className":490},[],[492],{"type":33,"value":113},{"type":33,"value":494}," as part of ",{"type":24,"tag":14,"props":496,"children":498},{"className":497},[],[499],{"type":33,"value":500},"java.util.concurrent",{"type":33,"value":502},". It provides a high-level API to manage and reuse threads through pooling.",{"type":24,"tag":433,"props":504,"children":506},{"id":505},"how-thread-pooling-works",[507],{"type":33,"value":508},"How Thread Pooling Works",{"type":24,"tag":29,"props":510,"children":511},{},[512],{"type":33,"value":513},"Thread pooling allows you to reuse a fixed number of threads to execute multiple tasks. Instead of creating a new thread for each task, the executor assigns tasks to available worker threads in the pool.",{"type":24,"tag":151,"props":515,"children":517},{"className":153,"code":516,"language":16,"meta":7,"style":7},"import java.util.concurrent.ExecutorService;\nimport java.util.concurrent.Executors;\n\npublic class ExecutorExample {\n    public static void main(String[] args) {\n        ExecutorService executor = Executors.newFixedThreadPool(3);\n        for (int i = 0; i \u003C 5; i++) {\n            executor.submit(() -> System.out.println(\"Task executed by: \" + Thread.currentThread().getName()));\n        }\n        executor.shutdown();\n    }\n}\n",[518],{"type":24,"tag":14,"props":519,"children":520},{"__ignoreMap":7},[521,534,546,553,573,612,649,710,776,784,801,808],{"type":24,"tag":160,"props":522,"children":523},{"class":162,"line":163},[524,529],{"type":24,"tag":160,"props":525,"children":526},{"style":167},[527],{"type":33,"value":528},"import",{"type":24,"tag":160,"props":530,"children":531},{"style":189},[532],{"type":33,"value":533}," java.util.concurrent.ExecutorService;\n",{"type":24,"tag":160,"props":535,"children":536},{"class":162,"line":195},[537,541],{"type":24,"tag":160,"props":538,"children":539},{"style":167},[540],{"type":33,"value":528},{"type":24,"tag":160,"props":542,"children":543},{"style":189},[544],{"type":33,"value":545}," java.util.concurrent.Executors;\n",{"type":24,"tag":160,"props":547,"children":548},{"class":162,"line":219},[549],{"type":24,"tag":160,"props":550,"children":551},{"emptyLinePlaceholder":296},[552],{"type":33,"value":299},{"type":24,"tag":160,"props":554,"children":555},{"class":162,"line":274},[556,560,564,569],{"type":24,"tag":160,"props":557,"children":558},{"style":167},[559],{"type":33,"value":308},{"type":24,"tag":160,"props":561,"children":562},{"style":167},[563],{"type":33,"value":313},{"type":24,"tag":160,"props":565,"children":566},{"style":173},[567],{"type":33,"value":568}," ExecutorExample",{"type":24,"tag":160,"props":570,"children":571},{"style":189},[572],{"type":33,"value":192},{"type":24,"tag":160,"props":574,"children":575},{"class":162,"line":283},[576,580,584,588,592,596,600,604,608],{"type":24,"tag":160,"props":577,"children":578},{"style":167},[579],{"type":33,"value":201},{"type":24,"tag":160,"props":581,"children":582},{"style":167},[583],{"type":33,"value":335},{"type":24,"tag":160,"props":585,"children":586},{"style":167},[587],{"type":33,"value":206},{"type":24,"tag":160,"props":589,"children":590},{"style":173},[591],{"type":33,"value":344},{"type":24,"tag":160,"props":593,"children":594},{"style":189},[595],{"type":33,"value":235},{"type":24,"tag":160,"props":597,"children":598},{"style":167},[599],{"type":33,"value":353},{"type":24,"tag":160,"props":601,"children":602},{"style":189},[603],{"type":33,"value":358},{"type":24,"tag":160,"props":605,"children":606},{"style":361},[607],{"type":33,"value":364},{"type":24,"tag":160,"props":609,"children":610},{"style":189},[611],{"type":33,"value":369},{"type":24,"tag":160,"props":613,"children":614},{"class":162,"line":292},[615,620,624,629,634,638,644],{"type":24,"tag":160,"props":616,"children":617},{"style":189},[618],{"type":33,"value":619},"        ExecutorService executor ",{"type":24,"tag":160,"props":621,"children":622},{"style":167},[623],{"type":33,"value":383},{"type":24,"tag":160,"props":625,"children":626},{"style":189},[627],{"type":33,"value":628}," Executors.",{"type":24,"tag":160,"props":630,"children":631},{"style":173},[632],{"type":33,"value":633},"newFixedThreadPool",{"type":24,"tag":160,"props":635,"children":636},{"style":189},[637],{"type":33,"value":235},{"type":24,"tag":160,"props":639,"children":641},{"style":640},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[642],{"type":33,"value":643},"3",{"type":24,"tag":160,"props":645,"children":646},{"style":189},[647],{"type":33,"value":648},");\n",{"type":24,"tag":160,"props":650,"children":651},{"class":162,"line":302},[652,657,662,667,672,676,681,686,691,696,701,706],{"type":24,"tag":160,"props":653,"children":654},{"style":167},[655],{"type":33,"value":656},"        for",{"type":24,"tag":160,"props":658,"children":659},{"style":189},[660],{"type":33,"value":661}," (",{"type":24,"tag":160,"props":663,"children":664},{"style":167},[665],{"type":33,"value":666},"int",{"type":24,"tag":160,"props":668,"children":669},{"style":189},[670],{"type":33,"value":671}," i ",{"type":24,"tag":160,"props":673,"children":674},{"style":167},[675],{"type":33,"value":383},{"type":24,"tag":160,"props":677,"children":678},{"style":640},[679],{"type":33,"value":680}," 0",{"type":24,"tag":160,"props":682,"children":683},{"style":189},[684],{"type":33,"value":685},"; i ",{"type":24,"tag":160,"props":687,"children":688},{"style":167},[689],{"type":33,"value":690},"\u003C",{"type":24,"tag":160,"props":692,"children":693},{"style":640},[694],{"type":33,"value":695}," 5",{"type":24,"tag":160,"props":697,"children":698},{"style":189},[699],{"type":33,"value":700},"; i",{"type":24,"tag":160,"props":702,"children":703},{"style":167},[704],{"type":33,"value":705},"++",{"type":24,"tag":160,"props":707,"children":708},{"style":189},[709],{"type":33,"value":369},{"type":24,"tag":160,"props":711,"children":712},{"class":162,"line":325},[713,718,723,728,733,738,742,746,751,755,759,763,767,771],{"type":24,"tag":160,"props":714,"children":715},{"style":189},[716],{"type":33,"value":717},"            executor.",{"type":24,"tag":160,"props":719,"children":720},{"style":173},[721],{"type":33,"value":722},"submit",{"type":24,"tag":160,"props":724,"children":725},{"style":189},[726],{"type":33,"value":727},"(() ",{"type":24,"tag":160,"props":729,"children":730},{"style":167},[731],{"type":33,"value":732},"->",{"type":24,"tag":160,"props":734,"children":735},{"style":189},[736],{"type":33,"value":737}," System.out.",{"type":24,"tag":160,"props":739,"children":740},{"style":173},[741],{"type":33,"value":230},{"type":24,"tag":160,"props":743,"children":744},{"style":189},[745],{"type":33,"value":235},{"type":24,"tag":160,"props":747,"children":748},{"style":238},[749],{"type":33,"value":750},"\"Task executed by: \"",{"type":24,"tag":160,"props":752,"children":753},{"style":167},[754],{"type":33,"value":246},{"type":24,"tag":160,"props":756,"children":757},{"style":189},[758],{"type":33,"value":251},{"type":24,"tag":160,"props":760,"children":761},{"style":173},[762],{"type":33,"value":256},{"type":24,"tag":160,"props":764,"children":765},{"style":189},[766],{"type":33,"value":261},{"type":24,"tag":160,"props":768,"children":769},{"style":173},[770],{"type":33,"value":266},{"type":24,"tag":160,"props":772,"children":773},{"style":189},[774],{"type":33,"value":775},"()));\n",{"type":24,"tag":160,"props":777,"children":778},{"class":162,"line":372},[779],{"type":24,"tag":160,"props":780,"children":781},{"style":189},[782],{"type":33,"value":783},"        }\n",{"type":24,"tag":160,"props":785,"children":786},{"class":162,"line":400},[787,792,797],{"type":24,"tag":160,"props":788,"children":789},{"style":189},[790],{"type":33,"value":791},"        executor.",{"type":24,"tag":160,"props":793,"children":794},{"style":173},[795],{"type":33,"value":796},"shutdown",{"type":24,"tag":160,"props":798,"children":799},{"style":189},[800],{"type":33,"value":397},{"type":24,"tag":160,"props":802,"children":803},{"class":162,"line":418},[804],{"type":24,"tag":160,"props":805,"children":806},{"style":189},[807],{"type":33,"value":280},{"type":24,"tag":160,"props":809,"children":810},{"class":162,"line":426},[811],{"type":24,"tag":160,"props":812,"children":813},{"style":189},[814],{"type":33,"value":289},{"type":24,"tag":433,"props":816,"children":818},{"id":817},"common-executor-types",[819],{"type":33,"value":820},"Common Executor Types:",{"type":24,"tag":48,"props":822,"children":823},{},[824,835,846,857],{"type":24,"tag":52,"props":825,"children":826},{},[827,833],{"type":24,"tag":14,"props":828,"children":830},{"className":829},[],[831],{"type":33,"value":832},"newFixedThreadPool(n)",{"type":33,"value":834},": Fixed number of threads.",{"type":24,"tag":52,"props":836,"children":837},{},[838,844],{"type":24,"tag":14,"props":839,"children":841},{"className":840},[],[842],{"type":33,"value":843},"newCachedThreadPool()",{"type":33,"value":845},": Dynamically creates threads as needed.",{"type":24,"tag":52,"props":847,"children":848},{},[849,855],{"type":24,"tag":14,"props":850,"children":852},{"className":851},[],[853],{"type":33,"value":854},"newSingleThreadExecutor()",{"type":33,"value":856},": One thread for all tasks.",{"type":24,"tag":52,"props":858,"children":859},{},[860,866],{"type":24,"tag":14,"props":861,"children":863},{"className":862},[],[864],{"type":33,"value":865},"newScheduledThreadPool(n)",{"type":33,"value":867},": Supports delayed and periodic tasks.",{"type":24,"tag":433,"props":869,"children":871},{"id":870},"advantages",[872],{"type":33,"value":873},"Advantages:",{"type":24,"tag":48,"props":875,"children":876},{},[877,882,887],{"type":24,"tag":52,"props":878,"children":879},{},[880],{"type":33,"value":881},"Efficient resource use through reuse.",{"type":24,"tag":52,"props":883,"children":884},{},[885],{"type":33,"value":886},"Better scalability and responsiveness.",{"type":24,"tag":52,"props":888,"children":889},{},[890],{"type":33,"value":891},"Built-in management of task queues and error handling.",{"type":24,"tag":36,"props":893,"children":895},{"id":894},"the-future-virtual-threads-and-project-loom",[896],{"type":33,"value":897},"The Future: Virtual Threads and Project Loom",{"type":24,"tag":29,"props":899,"children":900},{},[901],{"type":33,"value":902},"Java 21 introduces Virtual Threads, a lightweight and scalable alternative to platform threads. A virtual thread is managed by the Java runtime rather than the operating system, allowing millions of threads to coexist.",{"type":24,"tag":433,"props":904,"children":906},{"id":905},"key-features",[907],{"type":33,"value":908},"Key Features:",{"type":24,"tag":48,"props":910,"children":911},{},[912,922,932],{"type":24,"tag":52,"props":913,"children":914},{},[915,920],{"type":24,"tag":56,"props":916,"children":917},{},[918],{"type":33,"value":919},"Minimal Memory Footprint",{"type":33,"value":921},": Much lighter than traditional threads.",{"type":24,"tag":52,"props":923,"children":924},{},[925,930],{"type":24,"tag":56,"props":926,"children":927},{},[928],{"type":33,"value":929},"Unblocked I/O",{"type":33,"value":931},": I/O operations don’t block the underlying platform thread.",{"type":24,"tag":52,"props":933,"children":934},{},[935,940],{"type":24,"tag":56,"props":936,"children":937},{},[938],{"type":33,"value":939},"Massive Scalability",{"type":33,"value":941},": Suitable for applications requiring thousands or millions of concurrent tasks (e.g., web servers).",{"type":24,"tag":29,"props":943,"children":944},{},[945],{"type":33,"value":946},"Example:",{"type":24,"tag":151,"props":948,"children":950},{"className":153,"code":949,"language":16,"meta":7,"style":7},"public class VirtualThreadExample {\n    public static void main(String[] args) {\n        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {\n            for (int i = 0; i \u003C 10; i++) {\n                executor.submit(() -> {\n                    System.out.println(\"Running in: \" + Thread.currentThread().getName());\n                });\n            }\n        }\n    }\n}\n",[951],{"type":24,"tag":14,"props":952,"children":953},{"__ignoreMap":7},[954,974,1013,1053,1106,1130,1175,1183,1191,1198,1205],{"type":24,"tag":160,"props":955,"children":956},{"class":162,"line":163},[957,961,965,970],{"type":24,"tag":160,"props":958,"children":959},{"style":167},[960],{"type":33,"value":308},{"type":24,"tag":160,"props":962,"children":963},{"style":167},[964],{"type":33,"value":313},{"type":24,"tag":160,"props":966,"children":967},{"style":173},[968],{"type":33,"value":969}," VirtualThreadExample",{"type":24,"tag":160,"props":971,"children":972},{"style":189},[973],{"type":33,"value":192},{"type":24,"tag":160,"props":975,"children":976},{"class":162,"line":195},[977,981,985,989,993,997,1001,1005,1009],{"type":24,"tag":160,"props":978,"children":979},{"style":167},[980],{"type":33,"value":201},{"type":24,"tag":160,"props":982,"children":983},{"style":167},[984],{"type":33,"value":335},{"type":24,"tag":160,"props":986,"children":987},{"style":167},[988],{"type":33,"value":206},{"type":24,"tag":160,"props":990,"children":991},{"style":173},[992],{"type":33,"value":344},{"type":24,"tag":160,"props":994,"children":995},{"style":189},[996],{"type":33,"value":235},{"type":24,"tag":160,"props":998,"children":999},{"style":167},[1000],{"type":33,"value":353},{"type":24,"tag":160,"props":1002,"children":1003},{"style":189},[1004],{"type":33,"value":358},{"type":24,"tag":160,"props":1006,"children":1007},{"style":361},[1008],{"type":33,"value":364},{"type":24,"tag":160,"props":1010,"children":1011},{"style":189},[1012],{"type":33,"value":369},{"type":24,"tag":160,"props":1014,"children":1015},{"class":162,"line":219},[1016,1021,1025,1030,1035,1039,1043,1048],{"type":24,"tag":160,"props":1017,"children":1018},{"style":167},[1019],{"type":33,"value":1020},"        try",{"type":24,"tag":160,"props":1022,"children":1023},{"style":189},[1024],{"type":33,"value":661},{"type":24,"tag":160,"props":1026,"children":1027},{"style":167},[1028],{"type":33,"value":1029},"var",{"type":24,"tag":160,"props":1031,"children":1032},{"style":189},[1033],{"type":33,"value":1034}," executor ",{"type":24,"tag":160,"props":1036,"children":1037},{"style":167},[1038],{"type":33,"value":383},{"type":24,"tag":160,"props":1040,"children":1041},{"style":189},[1042],{"type":33,"value":628},{"type":24,"tag":160,"props":1044,"children":1045},{"style":173},[1046],{"type":33,"value":1047},"newVirtualThreadPerTaskExecutor",{"type":24,"tag":160,"props":1049,"children":1050},{"style":189},[1051],{"type":33,"value":1052},"()) {\n",{"type":24,"tag":160,"props":1054,"children":1055},{"class":162,"line":274},[1056,1061,1065,1069,1073,1077,1081,1085,1089,1094,1098,1102],{"type":24,"tag":160,"props":1057,"children":1058},{"style":167},[1059],{"type":33,"value":1060},"            for",{"type":24,"tag":160,"props":1062,"children":1063},{"style":189},[1064],{"type":33,"value":661},{"type":24,"tag":160,"props":1066,"children":1067},{"style":167},[1068],{"type":33,"value":666},{"type":24,"tag":160,"props":1070,"children":1071},{"style":189},[1072],{"type":33,"value":671},{"type":24,"tag":160,"props":1074,"children":1075},{"style":167},[1076],{"type":33,"value":383},{"type":24,"tag":160,"props":1078,"children":1079},{"style":640},[1080],{"type":33,"value":680},{"type":24,"tag":160,"props":1082,"children":1083},{"style":189},[1084],{"type":33,"value":685},{"type":24,"tag":160,"props":1086,"children":1087},{"style":167},[1088],{"type":33,"value":690},{"type":24,"tag":160,"props":1090,"children":1091},{"style":640},[1092],{"type":33,"value":1093}," 10",{"type":24,"tag":160,"props":1095,"children":1096},{"style":189},[1097],{"type":33,"value":700},{"type":24,"tag":160,"props":1099,"children":1100},{"style":167},[1101],{"type":33,"value":705},{"type":24,"tag":160,"props":1103,"children":1104},{"style":189},[1105],{"type":33,"value":369},{"type":24,"tag":160,"props":1107,"children":1108},{"class":162,"line":283},[1109,1114,1118,1122,1126],{"type":24,"tag":160,"props":1110,"children":1111},{"style":189},[1112],{"type":33,"value":1113},"                executor.",{"type":24,"tag":160,"props":1115,"children":1116},{"style":173},[1117],{"type":33,"value":722},{"type":24,"tag":160,"props":1119,"children":1120},{"style":189},[1121],{"type":33,"value":727},{"type":24,"tag":160,"props":1123,"children":1124},{"style":167},[1125],{"type":33,"value":732},{"type":24,"tag":160,"props":1127,"children":1128},{"style":189},[1129],{"type":33,"value":192},{"type":24,"tag":160,"props":1131,"children":1132},{"class":162,"line":292},[1133,1138,1142,1146,1151,1155,1159,1163,1167,1171],{"type":24,"tag":160,"props":1134,"children":1135},{"style":189},[1136],{"type":33,"value":1137},"                    System.out.",{"type":24,"tag":160,"props":1139,"children":1140},{"style":173},[1141],{"type":33,"value":230},{"type":24,"tag":160,"props":1143,"children":1144},{"style":189},[1145],{"type":33,"value":235},{"type":24,"tag":160,"props":1147,"children":1148},{"style":238},[1149],{"type":33,"value":1150},"\"Running in: \"",{"type":24,"tag":160,"props":1152,"children":1153},{"style":167},[1154],{"type":33,"value":246},{"type":24,"tag":160,"props":1156,"children":1157},{"style":189},[1158],{"type":33,"value":251},{"type":24,"tag":160,"props":1160,"children":1161},{"style":173},[1162],{"type":33,"value":256},{"type":24,"tag":160,"props":1164,"children":1165},{"style":189},[1166],{"type":33,"value":261},{"type":24,"tag":160,"props":1168,"children":1169},{"style":173},[1170],{"type":33,"value":266},{"type":24,"tag":160,"props":1172,"children":1173},{"style":189},[1174],{"type":33,"value":271},{"type":24,"tag":160,"props":1176,"children":1177},{"class":162,"line":302},[1178],{"type":24,"tag":160,"props":1179,"children":1180},{"style":189},[1181],{"type":33,"value":1182},"                });\n",{"type":24,"tag":160,"props":1184,"children":1185},{"class":162,"line":325},[1186],{"type":24,"tag":160,"props":1187,"children":1188},{"style":189},[1189],{"type":33,"value":1190},"            }\n",{"type":24,"tag":160,"props":1192,"children":1193},{"class":162,"line":372},[1194],{"type":24,"tag":160,"props":1195,"children":1196},{"style":189},[1197],{"type":33,"value":783},{"type":24,"tag":160,"props":1199,"children":1200},{"class":162,"line":400},[1201],{"type":24,"tag":160,"props":1202,"children":1203},{"style":189},[1204],{"type":33,"value":280},{"type":24,"tag":160,"props":1206,"children":1207},{"class":162,"line":418},[1208],{"type":24,"tag":160,"props":1209,"children":1210},{"style":189},[1211],{"type":33,"value":289},{"type":24,"tag":433,"props":1213,"children":1215},{"id":1214},"when-to-use-virtual-threads",[1216],{"type":33,"value":1217},"When to Use Virtual Threads:",{"type":24,"tag":48,"props":1219,"children":1220},{},[1221,1226,1231],{"type":24,"tag":52,"props":1222,"children":1223},{},[1224],{"type":33,"value":1225},"I/O-bound applications (e.g., database access, HTTP clients).",{"type":24,"tag":52,"props":1227,"children":1228},{},[1229],{"type":33,"value":1230},"High-concurrency workloads (e.g., messaging systems).",{"type":24,"tag":52,"props":1232,"children":1233},{},[1234],{"type":33,"value":1235},"When simplifying thread management is a priority.",{"type":24,"tag":36,"props":1237,"children":1239},{"id":1238},"conclusion",[1240],{"type":33,"value":1241},"Conclusion",{"type":24,"tag":29,"props":1243,"children":1244},{},[1245],{"type":33,"value":1246},"Java offers a flexible and powerful concurrency model. Classic Threads provide a foundational understanding, ExecutorService enables efficient thread reuse and task management, and Virtual Threads open the door to a new era of scalable concurrency with minimal overhead.",{"type":24,"tag":29,"props":1248,"children":1249},{},[1250],{"type":33,"value":1251},"Each model has its place depending on the problem at hand. Mastering them gives you the toolkit needed to build high-performance, concurrent applications.",{"type":24,"tag":36,"props":1253,"children":1255},{"id":1254},"further-reading",[1256],{"type":33,"value":1257},"Further Reading",{"type":24,"tag":48,"props":1259,"children":1260},{},[1261,1273,1283,1292],{"type":24,"tag":52,"props":1262,"children":1263},{},[1264],{"type":24,"tag":1265,"props":1266,"children":1270},"a",{"href":1267,"rel":1268},"https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/package-summary.html",[1269],"nofollow",[1271],{"type":33,"value":1272},"Java Concurrency API",{"type":24,"tag":52,"props":1274,"children":1275},{},[1276],{"type":24,"tag":1265,"props":1277,"children":1280},{"href":1278,"rel":1279},"https://openjdk.org/projects/loom/",[1269],[1281],{"type":33,"value":1282},"Project Loom (Virtual Threads)",{"type":24,"tag":52,"props":1284,"children":1285},{},[1286],{"type":24,"tag":1265,"props":1287,"children":1290},{"href":1288,"rel":1289},"https://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html",[1269],[1291],{"type":33,"value":90},{"type":24,"tag":52,"props":1293,"children":1294},{},[1295],{"type":24,"tag":1265,"props":1296,"children":1299},{"href":1297,"rel":1298},"https://www.oreilly.com/library/view/effective-java-3rd/9780134686097/",[1269],[1300],{"type":33,"value":1301},"Effective Java - Joshua Bloch",{"type":24,"tag":29,"props":1303,"children":1304},{},[1305],{"type":33,"value":1306},"Deep understanding of Java's concurrency tools can significantly improve your system’s performance, scalability, and maintainability.",{"type":24,"tag":1308,"props":1309,"children":1310},"style",{},[1311],{"type":33,"value":1312},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":195,"depth":195,"links":1314},[1315,1316,1319,1324,1328,1329],{"id":38,"depth":195,"text":41},{"id":118,"depth":195,"text":121,"children":1317},[1318],{"id":435,"depth":219,"text":438},{"id":479,"depth":195,"text":482,"children":1320},[1321,1322,1323],{"id":505,"depth":219,"text":508},{"id":817,"depth":219,"text":820},{"id":870,"depth":219,"text":873},{"id":894,"depth":195,"text":897,"children":1325},[1326,1327],{"id":905,"depth":219,"text":908},{"id":1214,"depth":219,"text":1217},{"id":1238,"depth":195,"text":1241},{"id":1254,"depth":195,"text":1257},"markdown","content:posts:mastering-concurrency.md","content","posts/mastering-concurrency.md","posts/mastering-concurrency","md",[1337,2745,3643],{"_path":1338,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":1339,"description":1340,"date":1341,"layout":11,"subtitle":1342,"image":1343,"optimized_image":1343,"category":14,"tags":1344,"author":19,"paginate":6,"body":1350,"_type":1330,"_id":2742,"_source":1332,"_file":2743,"_stem":2744,"_extension":1335},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png",[14,1345,1346,1347,1348,1349],"gpt","openai","sql","ai","nlp",{"type":21,"children":1351,"toc":2731},[1352,1360,1366,1384,1390,1395,1437,1450,1466,1472,1477,1685,1691,1696,2029,2035,2040,2391,2397,2402,2461,2467,2472,2652,2658,2676,2682,2687,2694,2727],{"type":24,"tag":25,"props":1353,"children":1354},{},[1355],{"type":24,"tag":29,"props":1356,"children":1357},{},[1358],{"type":33,"value":1359},"Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":24,"tag":36,"props":1361,"children":1363},{"id":1362},"requisitos-previos",[1364],{"type":33,"value":1365},"Requisitos Previos",{"type":24,"tag":48,"props":1367,"children":1368},{},[1369,1374,1379],{"type":24,"tag":52,"props":1370,"children":1371},{},[1372],{"type":33,"value":1373},"Cuenta y clave API de OpenAI",{"type":24,"tag":52,"props":1375,"children":1376},{},[1377],{"type":33,"value":1378},"Python instalado en tu entorno",{"type":24,"tag":52,"props":1380,"children":1381},{},[1382],{"type":33,"value":1383},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":24,"tag":36,"props":1385,"children":1387},{"id":1386},"paso-1-configuración-del-entorno",[1388],{"type":33,"value":1389},"Paso 1: Configuración del entorno",{"type":24,"tag":29,"props":1391,"children":1392},{},[1393],{"type":33,"value":1394},"Primero, instala las dependencias necesarias:",{"type":24,"tag":151,"props":1396,"children":1400},{"className":1397,"code":1398,"language":1399,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","pip install openai python-dotenv sqlalchemy psycopg2-binary\n","bash",[1401],{"type":24,"tag":14,"props":1402,"children":1403},{"__ignoreMap":7},[1404],{"type":24,"tag":160,"props":1405,"children":1406},{"class":162,"line":163},[1407,1412,1417,1422,1427,1432],{"type":24,"tag":160,"props":1408,"children":1409},{"style":173},[1410],{"type":33,"value":1411},"pip",{"type":24,"tag":160,"props":1413,"children":1414},{"style":238},[1415],{"type":33,"value":1416}," install",{"type":24,"tag":160,"props":1418,"children":1419},{"style":238},[1420],{"type":33,"value":1421}," openai",{"type":24,"tag":160,"props":1423,"children":1424},{"style":238},[1425],{"type":33,"value":1426}," python-dotenv",{"type":24,"tag":160,"props":1428,"children":1429},{"style":238},[1430],{"type":33,"value":1431}," sqlalchemy",{"type":24,"tag":160,"props":1433,"children":1434},{"style":238},[1435],{"type":33,"value":1436}," psycopg2-binary\n",{"type":24,"tag":29,"props":1438,"children":1439},{},[1440,1442,1448],{"type":33,"value":1441},"Guarda tu clave API en un archivo ",{"type":24,"tag":14,"props":1443,"children":1445},{"className":1444},[],[1446],{"type":33,"value":1447},".env",{"type":33,"value":1449},":",{"type":24,"tag":151,"props":1451,"children":1455},{"className":1452,"code":1453,"language":1454,"meta":7,"style":7},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[1456],{"type":24,"tag":14,"props":1457,"children":1458},{"__ignoreMap":7},[1459],{"type":24,"tag":160,"props":1460,"children":1461},{"class":162,"line":163},[1462],{"type":24,"tag":160,"props":1463,"children":1464},{},[1465],{"type":33,"value":1453},{"type":24,"tag":36,"props":1467,"children":1469},{"id":1468},"paso-2-conexión-a-la-base-de-datos",[1470],{"type":33,"value":1471},"Paso 2: Conexión a la base de datos",{"type":24,"tag":29,"props":1473,"children":1474},{},[1475],{"type":33,"value":1476},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":24,"tag":151,"props":1478,"children":1482},{"className":1479,"code":1480,"language":1481,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n","python",[1483],{"type":24,"tag":14,"props":1484,"children":1485},{"__ignoreMap":7},[1486,1508,1529,1541,1562,1569,1577,1584,1612,1637,1668],{"type":24,"tag":160,"props":1487,"children":1488},{"class":162,"line":163},[1489,1494,1499,1503],{"type":24,"tag":160,"props":1490,"children":1491},{"style":167},[1492],{"type":33,"value":1493},"from",{"type":24,"tag":160,"props":1495,"children":1496},{"style":189},[1497],{"type":33,"value":1498}," sqlalchemy ",{"type":24,"tag":160,"props":1500,"children":1501},{"style":167},[1502],{"type":33,"value":528},{"type":24,"tag":160,"props":1504,"children":1505},{"style":189},[1506],{"type":33,"value":1507}," create_engine\n",{"type":24,"tag":160,"props":1509,"children":1510},{"class":162,"line":195},[1511,1515,1520,1524],{"type":24,"tag":160,"props":1512,"children":1513},{"style":167},[1514],{"type":33,"value":1493},{"type":24,"tag":160,"props":1516,"children":1517},{"style":189},[1518],{"type":33,"value":1519}," sqlalchemy.orm ",{"type":24,"tag":160,"props":1521,"children":1522},{"style":167},[1523],{"type":33,"value":528},{"type":24,"tag":160,"props":1525,"children":1526},{"style":189},[1527],{"type":33,"value":1528}," sessionmaker\n",{"type":24,"tag":160,"props":1530,"children":1531},{"class":162,"line":219},[1532,1536],{"type":24,"tag":160,"props":1533,"children":1534},{"style":167},[1535],{"type":33,"value":528},{"type":24,"tag":160,"props":1537,"children":1538},{"style":189},[1539],{"type":33,"value":1540}," os\n",{"type":24,"tag":160,"props":1542,"children":1543},{"class":162,"line":274},[1544,1548,1553,1557],{"type":24,"tag":160,"props":1545,"children":1546},{"style":167},[1547],{"type":33,"value":1493},{"type":24,"tag":160,"props":1549,"children":1550},{"style":189},[1551],{"type":33,"value":1552}," dotenv ",{"type":24,"tag":160,"props":1554,"children":1555},{"style":167},[1556],{"type":33,"value":528},{"type":24,"tag":160,"props":1558,"children":1559},{"style":189},[1560],{"type":33,"value":1561}," load_dotenv\n",{"type":24,"tag":160,"props":1563,"children":1564},{"class":162,"line":283},[1565],{"type":24,"tag":160,"props":1566,"children":1567},{"emptyLinePlaceholder":296},[1568],{"type":33,"value":299},{"type":24,"tag":160,"props":1570,"children":1571},{"class":162,"line":292},[1572],{"type":24,"tag":160,"props":1573,"children":1574},{"style":189},[1575],{"type":33,"value":1576},"load_dotenv()\n",{"type":24,"tag":160,"props":1578,"children":1579},{"class":162,"line":302},[1580],{"type":24,"tag":160,"props":1581,"children":1582},{"emptyLinePlaceholder":296},[1583],{"type":33,"value":299},{"type":24,"tag":160,"props":1585,"children":1586},{"class":162,"line":325},[1587,1592,1597,1602,1607],{"type":24,"tag":160,"props":1588,"children":1589},{"style":640},[1590],{"type":33,"value":1591},"DATABASE_URL",{"type":24,"tag":160,"props":1593,"children":1594},{"style":167},[1595],{"type":33,"value":1596}," =",{"type":24,"tag":160,"props":1598,"children":1599},{"style":189},[1600],{"type":33,"value":1601}," os.getenv(",{"type":24,"tag":160,"props":1603,"children":1604},{"style":238},[1605],{"type":33,"value":1606},"\"DATABASE_URL\"",{"type":24,"tag":160,"props":1608,"children":1609},{"style":189},[1610],{"type":33,"value":1611},")\n",{"type":24,"tag":160,"props":1613,"children":1614},{"class":162,"line":372},[1615,1620,1624,1629,1633],{"type":24,"tag":160,"props":1616,"children":1617},{"style":189},[1618],{"type":33,"value":1619},"engine ",{"type":24,"tag":160,"props":1621,"children":1622},{"style":167},[1623],{"type":33,"value":383},{"type":24,"tag":160,"props":1625,"children":1626},{"style":189},[1627],{"type":33,"value":1628}," create_engine(",{"type":24,"tag":160,"props":1630,"children":1631},{"style":640},[1632],{"type":33,"value":1591},{"type":24,"tag":160,"props":1634,"children":1635},{"style":189},[1636],{"type":33,"value":1611},{"type":24,"tag":160,"props":1638,"children":1639},{"class":162,"line":400},[1640,1645,1649,1654,1659,1663],{"type":24,"tag":160,"props":1641,"children":1642},{"style":189},[1643],{"type":33,"value":1644},"Session ",{"type":24,"tag":160,"props":1646,"children":1647},{"style":167},[1648],{"type":33,"value":383},{"type":24,"tag":160,"props":1650,"children":1651},{"style":189},[1652],{"type":33,"value":1653}," sessionmaker(",{"type":24,"tag":160,"props":1655,"children":1656},{"style":361},[1657],{"type":33,"value":1658},"bind",{"type":24,"tag":160,"props":1660,"children":1661},{"style":167},[1662],{"type":33,"value":383},{"type":24,"tag":160,"props":1664,"children":1665},{"style":189},[1666],{"type":33,"value":1667},"engine)\n",{"type":24,"tag":160,"props":1669,"children":1670},{"class":162,"line":418},[1671,1676,1680],{"type":24,"tag":160,"props":1672,"children":1673},{"style":189},[1674],{"type":33,"value":1675},"session ",{"type":24,"tag":160,"props":1677,"children":1678},{"style":167},[1679],{"type":33,"value":383},{"type":24,"tag":160,"props":1681,"children":1682},{"style":189},[1683],{"type":33,"value":1684}," Session()\n",{"type":24,"tag":36,"props":1686,"children":1688},{"id":1687},"paso-3-obtener-esquema-de-la-base-de-datos",[1689],{"type":33,"value":1690},"Paso 3: Obtener esquema de la base de datos",{"type":24,"tag":29,"props":1692,"children":1693},{},[1694],{"type":33,"value":1695},"Genera una descripción del esquema que será utilizada por GPT:",{"type":24,"tag":151,"props":1697,"children":1699},{"className":1479,"code":1698,"language":1481,"meta":7,"style":7},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[1700],{"type":24,"tag":14,"props":1701,"children":1702},{"__ignoreMap":7},[1703,1723,1730,1748,1765,1782,1805,1848,1869,1953,1977,1990,1997,2015],{"type":24,"tag":160,"props":1704,"children":1705},{"class":162,"line":163},[1706,1710,1714,1718],{"type":24,"tag":160,"props":1707,"children":1708},{"style":167},[1709],{"type":33,"value":1493},{"type":24,"tag":160,"props":1711,"children":1712},{"style":189},[1713],{"type":33,"value":1498},{"type":24,"tag":160,"props":1715,"children":1716},{"style":167},[1717],{"type":33,"value":528},{"type":24,"tag":160,"props":1719,"children":1720},{"style":189},[1721],{"type":33,"value":1722}," inspect\n",{"type":24,"tag":160,"props":1724,"children":1725},{"class":162,"line":195},[1726],{"type":24,"tag":160,"props":1727,"children":1728},{"emptyLinePlaceholder":296},[1729],{"type":33,"value":299},{"type":24,"tag":160,"props":1731,"children":1732},{"class":162,"line":219},[1733,1738,1743],{"type":24,"tag":160,"props":1734,"children":1735},{"style":167},[1736],{"type":33,"value":1737},"def",{"type":24,"tag":160,"props":1739,"children":1740},{"style":173},[1741],{"type":33,"value":1742}," get_database_schema",{"type":24,"tag":160,"props":1744,"children":1745},{"style":189},[1746],{"type":33,"value":1747},"(engine):\n",{"type":24,"tag":160,"props":1749,"children":1750},{"class":162,"line":274},[1751,1756,1760],{"type":24,"tag":160,"props":1752,"children":1753},{"style":189},[1754],{"type":33,"value":1755},"    inspector ",{"type":24,"tag":160,"props":1757,"children":1758},{"style":167},[1759],{"type":33,"value":383},{"type":24,"tag":160,"props":1761,"children":1762},{"style":189},[1763],{"type":33,"value":1764}," inspect(engine)\n",{"type":24,"tag":160,"props":1766,"children":1767},{"class":162,"line":283},[1768,1773,1777],{"type":24,"tag":160,"props":1769,"children":1770},{"style":189},[1771],{"type":33,"value":1772},"    schema_description ",{"type":24,"tag":160,"props":1774,"children":1775},{"style":167},[1776],{"type":33,"value":383},{"type":24,"tag":160,"props":1778,"children":1779},{"style":238},[1780],{"type":33,"value":1781}," \"\"\n",{"type":24,"tag":160,"props":1783,"children":1784},{"class":162,"line":292},[1785,1790,1795,1800],{"type":24,"tag":160,"props":1786,"children":1787},{"style":167},[1788],{"type":33,"value":1789},"    for",{"type":24,"tag":160,"props":1791,"children":1792},{"style":189},[1793],{"type":33,"value":1794}," table_name ",{"type":24,"tag":160,"props":1796,"children":1797},{"style":167},[1798],{"type":33,"value":1799},"in",{"type":24,"tag":160,"props":1801,"children":1802},{"style":189},[1803],{"type":33,"value":1804}," inspector.get_table_names():\n",{"type":24,"tag":160,"props":1806,"children":1807},{"class":162,"line":302},[1808,1813,1818,1823,1828,1833,1838,1843],{"type":24,"tag":160,"props":1809,"children":1810},{"style":189},[1811],{"type":33,"value":1812},"        schema_description ",{"type":24,"tag":160,"props":1814,"children":1815},{"style":167},[1816],{"type":33,"value":1817},"+=",{"type":24,"tag":160,"props":1819,"children":1820},{"style":167},[1821],{"type":33,"value":1822}," f",{"type":24,"tag":160,"props":1824,"children":1825},{"style":238},[1826],{"type":33,"value":1827},"\"Table: ",{"type":24,"tag":160,"props":1829,"children":1830},{"style":640},[1831],{"type":33,"value":1832},"{",{"type":24,"tag":160,"props":1834,"children":1835},{"style":189},[1836],{"type":33,"value":1837},"table_name",{"type":24,"tag":160,"props":1839,"children":1840},{"style":640},[1841],{"type":33,"value":1842},"}\\n",{"type":24,"tag":160,"props":1844,"children":1845},{"style":238},[1846],{"type":33,"value":1847},"\"\n",{"type":24,"tag":160,"props":1849,"children":1850},{"class":162,"line":325},[1851,1855,1860,1864],{"type":24,"tag":160,"props":1852,"children":1853},{"style":167},[1854],{"type":33,"value":656},{"type":24,"tag":160,"props":1856,"children":1857},{"style":189},[1858],{"type":33,"value":1859}," column ",{"type":24,"tag":160,"props":1861,"children":1862},{"style":167},[1863],{"type":33,"value":1799},{"type":24,"tag":160,"props":1865,"children":1866},{"style":189},[1867],{"type":33,"value":1868}," inspector.get_columns(table_name):\n",{"type":24,"tag":160,"props":1870,"children":1871},{"class":162,"line":372},[1872,1877,1881,1885,1890,1894,1899,1904,1909,1914,1918,1922,1926,1931,1935,1939,1944,1949],{"type":24,"tag":160,"props":1873,"children":1874},{"style":189},[1875],{"type":33,"value":1876},"            schema_description ",{"type":24,"tag":160,"props":1878,"children":1879},{"style":167},[1880],{"type":33,"value":1817},{"type":24,"tag":160,"props":1882,"children":1883},{"style":167},[1884],{"type":33,"value":1822},{"type":24,"tag":160,"props":1886,"children":1887},{"style":238},[1888],{"type":33,"value":1889},"\"- ",{"type":24,"tag":160,"props":1891,"children":1892},{"style":640},[1893],{"type":33,"value":1832},{"type":24,"tag":160,"props":1895,"children":1896},{"style":189},[1897],{"type":33,"value":1898},"column[",{"type":24,"tag":160,"props":1900,"children":1901},{"style":238},[1902],{"type":33,"value":1903},"'name'",{"type":24,"tag":160,"props":1905,"children":1906},{"style":189},[1907],{"type":33,"value":1908},"]",{"type":24,"tag":160,"props":1910,"children":1911},{"style":640},[1912],{"type":33,"value":1913},"}",{"type":24,"tag":160,"props":1915,"children":1916},{"style":238},[1917],{"type":33,"value":661},{"type":24,"tag":160,"props":1919,"children":1920},{"style":640},[1921],{"type":33,"value":1832},{"type":24,"tag":160,"props":1923,"children":1924},{"style":189},[1925],{"type":33,"value":1898},{"type":24,"tag":160,"props":1927,"children":1928},{"style":238},[1929],{"type":33,"value":1930},"'type'",{"type":24,"tag":160,"props":1932,"children":1933},{"style":189},[1934],{"type":33,"value":1908},{"type":24,"tag":160,"props":1936,"children":1937},{"style":640},[1938],{"type":33,"value":1913},{"type":24,"tag":160,"props":1940,"children":1941},{"style":238},[1942],{"type":33,"value":1943},")",{"type":24,"tag":160,"props":1945,"children":1946},{"style":640},[1947],{"type":33,"value":1948},"\\n",{"type":24,"tag":160,"props":1950,"children":1951},{"style":238},[1952],{"type":33,"value":1847},{"type":24,"tag":160,"props":1954,"children":1955},{"class":162,"line":400},[1956,1960,1964,1969,1973],{"type":24,"tag":160,"props":1957,"children":1958},{"style":189},[1959],{"type":33,"value":1812},{"type":24,"tag":160,"props":1961,"children":1962},{"style":167},[1963],{"type":33,"value":1817},{"type":24,"tag":160,"props":1965,"children":1966},{"style":238},[1967],{"type":33,"value":1968}," \"",{"type":24,"tag":160,"props":1970,"children":1971},{"style":640},[1972],{"type":33,"value":1948},{"type":24,"tag":160,"props":1974,"children":1975},{"style":238},[1976],{"type":33,"value":1847},{"type":24,"tag":160,"props":1978,"children":1979},{"class":162,"line":418},[1980,1985],{"type":24,"tag":160,"props":1981,"children":1982},{"style":167},[1983],{"type":33,"value":1984},"    return",{"type":24,"tag":160,"props":1986,"children":1987},{"style":189},[1988],{"type":33,"value":1989}," schema_description\n",{"type":24,"tag":160,"props":1991,"children":1992},{"class":162,"line":426},[1993],{"type":24,"tag":160,"props":1994,"children":1995},{"emptyLinePlaceholder":296},[1996],{"type":33,"value":299},{"type":24,"tag":160,"props":1998,"children":2000},{"class":162,"line":1999},13,[2001,2006,2010],{"type":24,"tag":160,"props":2002,"children":2003},{"style":189},[2004],{"type":33,"value":2005},"schema_description ",{"type":24,"tag":160,"props":2007,"children":2008},{"style":167},[2009],{"type":33,"value":383},{"type":24,"tag":160,"props":2011,"children":2012},{"style":189},[2013],{"type":33,"value":2014}," get_database_schema(engine)\n",{"type":24,"tag":160,"props":2016,"children":2018},{"class":162,"line":2017},14,[2019,2024],{"type":24,"tag":160,"props":2020,"children":2021},{"style":640},[2022],{"type":33,"value":2023},"print",{"type":24,"tag":160,"props":2025,"children":2026},{"style":189},[2027],{"type":33,"value":2028},"(schema_description)\n",{"type":24,"tag":36,"props":2030,"children":2032},{"id":2031},"paso-4-preparar-la-consulta-con-gpt",[2033],{"type":33,"value":2034},"Paso 4: Preparar la consulta con GPT",{"type":24,"tag":29,"props":2036,"children":2037},{},[2038],{"type":33,"value":2039},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":24,"tag":151,"props":2041,"children":2043},{"className":1479,"code":2042,"language":1481,"meta":7,"style":7},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[2044],{"type":24,"tag":14,"props":2045,"children":2046},{"__ignoreMap":7},[2047,2059,2070,2077,2102,2109,2126,2143,2165,2182,2253,2286,2294,2302,2329,2342,2350,2378],{"type":24,"tag":160,"props":2048,"children":2049},{"class":162,"line":163},[2050,2054],{"type":24,"tag":160,"props":2051,"children":2052},{"style":167},[2053],{"type":33,"value":528},{"type":24,"tag":160,"props":2055,"children":2056},{"style":189},[2057],{"type":33,"value":2058}," openai\n",{"type":24,"tag":160,"props":2060,"children":2061},{"class":162,"line":195},[2062,2066],{"type":24,"tag":160,"props":2063,"children":2064},{"style":167},[2065],{"type":33,"value":528},{"type":24,"tag":160,"props":2067,"children":2068},{"style":189},[2069],{"type":33,"value":1540},{"type":24,"tag":160,"props":2071,"children":2072},{"class":162,"line":219},[2073],{"type":24,"tag":160,"props":2074,"children":2075},{"emptyLinePlaceholder":296},[2076],{"type":33,"value":299},{"type":24,"tag":160,"props":2078,"children":2079},{"class":162,"line":274},[2080,2085,2089,2093,2098],{"type":24,"tag":160,"props":2081,"children":2082},{"style":189},[2083],{"type":33,"value":2084},"openai.api_key ",{"type":24,"tag":160,"props":2086,"children":2087},{"style":167},[2088],{"type":33,"value":383},{"type":24,"tag":160,"props":2090,"children":2091},{"style":189},[2092],{"type":33,"value":1601},{"type":24,"tag":160,"props":2094,"children":2095},{"style":238},[2096],{"type":33,"value":2097},"\"OPENAI_API_KEY\"",{"type":24,"tag":160,"props":2099,"children":2100},{"style":189},[2101],{"type":33,"value":1611},{"type":24,"tag":160,"props":2103,"children":2104},{"class":162,"line":283},[2105],{"type":24,"tag":160,"props":2106,"children":2107},{"emptyLinePlaceholder":296},[2108],{"type":33,"value":299},{"type":24,"tag":160,"props":2110,"children":2111},{"class":162,"line":292},[2112,2116,2121],{"type":24,"tag":160,"props":2113,"children":2114},{"style":167},[2115],{"type":33,"value":1737},{"type":24,"tag":160,"props":2117,"children":2118},{"style":173},[2119],{"type":33,"value":2120}," generate_sql",{"type":24,"tag":160,"props":2122,"children":2123},{"style":189},[2124],{"type":33,"value":2125},"(question, schema):\n",{"type":24,"tag":160,"props":2127,"children":2128},{"class":162,"line":302},[2129,2134,2138],{"type":24,"tag":160,"props":2130,"children":2131},{"style":189},[2132],{"type":33,"value":2133},"    response ",{"type":24,"tag":160,"props":2135,"children":2136},{"style":167},[2137],{"type":33,"value":383},{"type":24,"tag":160,"props":2139,"children":2140},{"style":189},[2141],{"type":33,"value":2142}," openai.ChatCompletion.create(\n",{"type":24,"tag":160,"props":2144,"children":2145},{"class":162,"line":325},[2146,2151,2155,2160],{"type":24,"tag":160,"props":2147,"children":2148},{"style":361},[2149],{"type":33,"value":2150},"        model",{"type":24,"tag":160,"props":2152,"children":2153},{"style":167},[2154],{"type":33,"value":383},{"type":24,"tag":160,"props":2156,"children":2157},{"style":238},[2158],{"type":33,"value":2159},"\"gpt-4\"",{"type":24,"tag":160,"props":2161,"children":2162},{"style":189},[2163],{"type":33,"value":2164},",\n",{"type":24,"tag":160,"props":2166,"children":2167},{"class":162,"line":372},[2168,2173,2177],{"type":24,"tag":160,"props":2169,"children":2170},{"style":361},[2171],{"type":33,"value":2172},"        messages",{"type":24,"tag":160,"props":2174,"children":2175},{"style":167},[2176],{"type":33,"value":383},{"type":24,"tag":160,"props":2178,"children":2179},{"style":189},[2180],{"type":33,"value":2181},"[\n",{"type":24,"tag":160,"props":2183,"children":2184},{"class":162,"line":400},[2185,2190,2195,2200,2205,2210,2215,2219,2224,2229,2234,2239,2243,2248],{"type":24,"tag":160,"props":2186,"children":2187},{"style":189},[2188],{"type":33,"value":2189},"            {",{"type":24,"tag":160,"props":2191,"children":2192},{"style":238},[2193],{"type":33,"value":2194},"\"role\"",{"type":24,"tag":160,"props":2196,"children":2197},{"style":189},[2198],{"type":33,"value":2199},": ",{"type":24,"tag":160,"props":2201,"children":2202},{"style":238},[2203],{"type":33,"value":2204},"\"system\"",{"type":24,"tag":160,"props":2206,"children":2207},{"style":189},[2208],{"type":33,"value":2209},", ",{"type":24,"tag":160,"props":2211,"children":2212},{"style":238},[2213],{"type":33,"value":2214},"\"content\"",{"type":24,"tag":160,"props":2216,"children":2217},{"style":189},[2218],{"type":33,"value":2199},{"type":24,"tag":160,"props":2220,"children":2221},{"style":167},[2222],{"type":33,"value":2223},"f",{"type":24,"tag":160,"props":2225,"children":2226},{"style":238},[2227],{"type":33,"value":2228},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":24,"tag":160,"props":2230,"children":2231},{"style":640},[2232],{"type":33,"value":2233},"\\n{",{"type":24,"tag":160,"props":2235,"children":2236},{"style":189},[2237],{"type":33,"value":2238},"schema",{"type":24,"tag":160,"props":2240,"children":2241},{"style":640},[2242],{"type":33,"value":1913},{"type":24,"tag":160,"props":2244,"children":2245},{"style":238},[2246],{"type":33,"value":2247},"\"",{"type":24,"tag":160,"props":2249,"children":2250},{"style":189},[2251],{"type":33,"value":2252},"},\n",{"type":24,"tag":160,"props":2254,"children":2255},{"class":162,"line":418},[2256,2260,2264,2268,2273,2277,2281],{"type":24,"tag":160,"props":2257,"children":2258},{"style":189},[2259],{"type":33,"value":2189},{"type":24,"tag":160,"props":2261,"children":2262},{"style":238},[2263],{"type":33,"value":2194},{"type":24,"tag":160,"props":2265,"children":2266},{"style":189},[2267],{"type":33,"value":2199},{"type":24,"tag":160,"props":2269,"children":2270},{"style":238},[2271],{"type":33,"value":2272},"\"user\"",{"type":24,"tag":160,"props":2274,"children":2275},{"style":189},[2276],{"type":33,"value":2209},{"type":24,"tag":160,"props":2278,"children":2279},{"style":238},[2280],{"type":33,"value":2214},{"type":24,"tag":160,"props":2282,"children":2283},{"style":189},[2284],{"type":33,"value":2285},": question}\n",{"type":24,"tag":160,"props":2287,"children":2288},{"class":162,"line":426},[2289],{"type":24,"tag":160,"props":2290,"children":2291},{"style":189},[2292],{"type":33,"value":2293},"        ]\n",{"type":24,"tag":160,"props":2295,"children":2296},{"class":162,"line":1999},[2297],{"type":24,"tag":160,"props":2298,"children":2299},{"style":189},[2300],{"type":33,"value":2301},"    )\n",{"type":24,"tag":160,"props":2303,"children":2304},{"class":162,"line":2017},[2305,2310,2314,2319,2324],{"type":24,"tag":160,"props":2306,"children":2307},{"style":189},[2308],{"type":33,"value":2309},"    sql_query ",{"type":24,"tag":160,"props":2311,"children":2312},{"style":167},[2313],{"type":33,"value":383},{"type":24,"tag":160,"props":2315,"children":2316},{"style":189},[2317],{"type":33,"value":2318}," response.choices[",{"type":24,"tag":160,"props":2320,"children":2321},{"style":640},[2322],{"type":33,"value":2323},"0",{"type":24,"tag":160,"props":2325,"children":2326},{"style":189},[2327],{"type":33,"value":2328},"].message.content.strip()\n",{"type":24,"tag":160,"props":2330,"children":2332},{"class":162,"line":2331},15,[2333,2337],{"type":24,"tag":160,"props":2334,"children":2335},{"style":167},[2336],{"type":33,"value":1984},{"type":24,"tag":160,"props":2338,"children":2339},{"style":189},[2340],{"type":33,"value":2341}," sql_query\n",{"type":24,"tag":160,"props":2343,"children":2345},{"class":162,"line":2344},16,[2346],{"type":24,"tag":160,"props":2347,"children":2348},{"emptyLinePlaceholder":296},[2349],{"type":33,"value":299},{"type":24,"tag":160,"props":2351,"children":2353},{"class":162,"line":2352},17,[2354,2359,2363,2368,2373],{"type":24,"tag":160,"props":2355,"children":2356},{"style":189},[2357],{"type":33,"value":2358},"query ",{"type":24,"tag":160,"props":2360,"children":2361},{"style":167},[2362],{"type":33,"value":383},{"type":24,"tag":160,"props":2364,"children":2365},{"style":189},[2366],{"type":33,"value":2367}," generate_sql(",{"type":24,"tag":160,"props":2369,"children":2370},{"style":238},[2371],{"type":33,"value":2372},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":24,"tag":160,"props":2374,"children":2375},{"style":189},[2376],{"type":33,"value":2377},", schema_description)\n",{"type":24,"tag":160,"props":2379,"children":2381},{"class":162,"line":2380},18,[2382,2386],{"type":24,"tag":160,"props":2383,"children":2384},{"style":640},[2385],{"type":33,"value":2023},{"type":24,"tag":160,"props":2387,"children":2388},{"style":189},[2389],{"type":33,"value":2390},"(query)\n",{"type":24,"tag":36,"props":2392,"children":2394},{"id":2393},"paso-5-ejecutar-consultas-generadas",[2395],{"type":33,"value":2396},"Paso 5: Ejecutar consultas generadas",{"type":24,"tag":29,"props":2398,"children":2399},{},[2400],{"type":33,"value":2401},"Ejecuta la consulta generada y obtiene resultados:",{"type":24,"tag":151,"props":2403,"children":2405},{"className":1479,"code":2404,"language":1481,"meta":7,"style":7},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[2406],{"type":24,"tag":14,"props":2407,"children":2408},{"__ignoreMap":7},[2409,2426,2448],{"type":24,"tag":160,"props":2410,"children":2411},{"class":162,"line":163},[2412,2417,2421],{"type":24,"tag":160,"props":2413,"children":2414},{"style":189},[2415],{"type":33,"value":2416},"result ",{"type":24,"tag":160,"props":2418,"children":2419},{"style":167},[2420],{"type":33,"value":383},{"type":24,"tag":160,"props":2422,"children":2423},{"style":189},[2424],{"type":33,"value":2425}," session.execute(query)\n",{"type":24,"tag":160,"props":2427,"children":2428},{"class":162,"line":195},[2429,2434,2439,2443],{"type":24,"tag":160,"props":2430,"children":2431},{"style":167},[2432],{"type":33,"value":2433},"for",{"type":24,"tag":160,"props":2435,"children":2436},{"style":189},[2437],{"type":33,"value":2438}," row ",{"type":24,"tag":160,"props":2440,"children":2441},{"style":167},[2442],{"type":33,"value":1799},{"type":24,"tag":160,"props":2444,"children":2445},{"style":189},[2446],{"type":33,"value":2447}," result:\n",{"type":24,"tag":160,"props":2449,"children":2450},{"class":162,"line":219},[2451,2456],{"type":24,"tag":160,"props":2452,"children":2453},{"style":640},[2454],{"type":33,"value":2455},"    print",{"type":24,"tag":160,"props":2457,"children":2458},{"style":189},[2459],{"type":33,"value":2460},"(row)\n",{"type":24,"tag":36,"props":2462,"children":2464},{"id":2463},"paso-6-manejo-de-errores-y-seguridad",[2465],{"type":33,"value":2466},"Paso 6: Manejo de errores y seguridad",{"type":24,"tag":29,"props":2468,"children":2469},{},[2470],{"type":33,"value":2471},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":24,"tag":151,"props":2473,"children":2475},{"className":1479,"code":2474,"language":1481,"meta":7,"style":7},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[2476],{"type":24,"tag":14,"props":2477,"children":2478},{"__ignoreMap":7},[2479,2496,2509,2526,2539,2562,2604,2616,2623,2640],{"type":24,"tag":160,"props":2480,"children":2481},{"class":162,"line":163},[2482,2486,2491],{"type":24,"tag":160,"props":2483,"children":2484},{"style":167},[2485],{"type":33,"value":1737},{"type":24,"tag":160,"props":2487,"children":2488},{"style":173},[2489],{"type":33,"value":2490}," execute_safe_query",{"type":24,"tag":160,"props":2492,"children":2493},{"style":189},[2494],{"type":33,"value":2495},"(sql_query):\n",{"type":24,"tag":160,"props":2497,"children":2498},{"class":162,"line":195},[2499,2504],{"type":24,"tag":160,"props":2500,"children":2501},{"style":167},[2502],{"type":33,"value":2503},"    try",{"type":24,"tag":160,"props":2505,"children":2506},{"style":189},[2507],{"type":33,"value":2508},":\n",{"type":24,"tag":160,"props":2510,"children":2511},{"class":162,"line":219},[2512,2517,2521],{"type":24,"tag":160,"props":2513,"children":2514},{"style":189},[2515],{"type":33,"value":2516},"        result ",{"type":24,"tag":160,"props":2518,"children":2519},{"style":167},[2520],{"type":33,"value":383},{"type":24,"tag":160,"props":2522,"children":2523},{"style":189},[2524],{"type":33,"value":2525}," session.execute(sql_query)\n",{"type":24,"tag":160,"props":2527,"children":2528},{"class":162,"line":274},[2529,2534],{"type":24,"tag":160,"props":2530,"children":2531},{"style":167},[2532],{"type":33,"value":2533},"        return",{"type":24,"tag":160,"props":2535,"children":2536},{"style":189},[2537],{"type":33,"value":2538}," result.fetchall()\n",{"type":24,"tag":160,"props":2540,"children":2541},{"class":162,"line":283},[2542,2547,2552,2557],{"type":24,"tag":160,"props":2543,"children":2544},{"style":167},[2545],{"type":33,"value":2546},"    except",{"type":24,"tag":160,"props":2548,"children":2549},{"style":640},[2550],{"type":33,"value":2551}," Exception",{"type":24,"tag":160,"props":2553,"children":2554},{"style":167},[2555],{"type":33,"value":2556}," as",{"type":24,"tag":160,"props":2558,"children":2559},{"style":189},[2560],{"type":33,"value":2561}," e:\n",{"type":24,"tag":160,"props":2563,"children":2564},{"class":162,"line":292},[2565,2570,2574,2578,2583,2587,2592,2596,2600],{"type":24,"tag":160,"props":2566,"children":2567},{"style":640},[2568],{"type":33,"value":2569},"        print",{"type":24,"tag":160,"props":2571,"children":2572},{"style":189},[2573],{"type":33,"value":235},{"type":24,"tag":160,"props":2575,"children":2576},{"style":167},[2577],{"type":33,"value":2223},{"type":24,"tag":160,"props":2579,"children":2580},{"style":238},[2581],{"type":33,"value":2582},"\"Error al ejecutar consulta: ",{"type":24,"tag":160,"props":2584,"children":2585},{"style":640},[2586],{"type":33,"value":1832},{"type":24,"tag":160,"props":2588,"children":2589},{"style":189},[2590],{"type":33,"value":2591},"e",{"type":24,"tag":160,"props":2593,"children":2594},{"style":640},[2595],{"type":33,"value":1913},{"type":24,"tag":160,"props":2597,"children":2598},{"style":238},[2599],{"type":33,"value":2247},{"type":24,"tag":160,"props":2601,"children":2602},{"style":189},[2603],{"type":33,"value":1611},{"type":24,"tag":160,"props":2605,"children":2606},{"class":162,"line":302},[2607,2611],{"type":24,"tag":160,"props":2608,"children":2609},{"style":167},[2610],{"type":33,"value":2533},{"type":24,"tag":160,"props":2612,"children":2613},{"style":640},[2614],{"type":33,"value":2615}," None\n",{"type":24,"tag":160,"props":2617,"children":2618},{"class":162,"line":325},[2619],{"type":24,"tag":160,"props":2620,"children":2621},{"emptyLinePlaceholder":296},[2622],{"type":33,"value":299},{"type":24,"tag":160,"props":2624,"children":2625},{"class":162,"line":372},[2626,2631,2635],{"type":24,"tag":160,"props":2627,"children":2628},{"style":189},[2629],{"type":33,"value":2630},"results ",{"type":24,"tag":160,"props":2632,"children":2633},{"style":167},[2634],{"type":33,"value":383},{"type":24,"tag":160,"props":2636,"children":2637},{"style":189},[2638],{"type":33,"value":2639}," execute_safe_query(query)\n",{"type":24,"tag":160,"props":2641,"children":2642},{"class":162,"line":400},[2643,2647],{"type":24,"tag":160,"props":2644,"children":2645},{"style":640},[2646],{"type":33,"value":2023},{"type":24,"tag":160,"props":2648,"children":2649},{"style":189},[2650],{"type":33,"value":2651},"(results)\n",{"type":24,"tag":36,"props":2653,"children":2655},{"id":2654},"buenas-prácticas",[2656],{"type":33,"value":2657},"Buenas prácticas",{"type":24,"tag":48,"props":2659,"children":2660},{},[2661,2666,2671],{"type":24,"tag":52,"props":2662,"children":2663},{},[2664],{"type":33,"value":2665},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":24,"tag":52,"props":2667,"children":2668},{},[2669],{"type":33,"value":2670},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":24,"tag":52,"props":2672,"children":2673},{},[2674],{"type":33,"value":2675},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":24,"tag":36,"props":2677,"children":2679},{"id":2678},"conclusión",[2680],{"type":33,"value":2681},"Conclusión",{"type":24,"tag":29,"props":2683,"children":2684},{},[2685],{"type":33,"value":2686},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":24,"tag":2688,"props":2689,"children":2691},"h1",{"id":2690},"recursos-adicionales",[2692],{"type":33,"value":2693},"Recursos adicionales",{"type":24,"tag":48,"props":2695,"children":2696},{},[2697,2707,2717],{"type":24,"tag":52,"props":2698,"children":2699},{},[2700],{"type":24,"tag":1265,"props":2701,"children":2704},{"href":2702,"rel":2703},"https://platform.openai.com/docs/api-reference",[1269],[2705],{"type":33,"value":2706},"Documentación API OpenAI",{"type":24,"tag":52,"props":2708,"children":2709},{},[2710],{"type":24,"tag":1265,"props":2711,"children":2714},{"href":2712,"rel":2713},"https://docs.sqlalchemy.org/en/20/",[1269],[2715],{"type":33,"value":2716},"SQLAlchemy ORM Documentation",{"type":24,"tag":52,"props":2718,"children":2719},{},[2720],{"type":24,"tag":1265,"props":2721,"children":2724},{"href":2722,"rel":2723},"https://pypi.org/project/python-dotenv/",[1269],[2725],{"type":33,"value":2726},"Python Dotenv",{"type":24,"tag":1308,"props":2728,"children":2729},{},[2730],{"type":33,"value":1312},{"title":7,"searchDepth":195,"depth":195,"links":2732},[2733,2734,2735,2736,2737,2738,2739,2740,2741],{"id":1362,"depth":195,"text":1365},{"id":1386,"depth":195,"text":1389},{"id":1468,"depth":195,"text":1471},{"id":1687,"depth":195,"text":1690},{"id":2031,"depth":195,"text":2034},{"id":2393,"depth":195,"text":2396},{"id":2463,"depth":195,"text":2466},{"id":2654,"depth":195,"text":2657},{"id":2678,"depth":195,"text":2681},"content:posts:gpt-sql-integration.md","posts/gpt-sql-integration.md","posts/gpt-sql-integration",{"_path":2746,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":2747,"description":2748,"date":2749,"layout":11,"subtitle":2750,"image":2751,"optimized_image":2751,"category":14,"tags":2752,"author":19,"paginate":6,"body":2756,"_type":1330,"_id":3640,"_source":1332,"_file":3641,"_stem":3642,"_extension":1335},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","Explore essential security strategies for Java applications, addressing common vulnerabilities identified by OWASP Top 10 with practical examples.","2024-08-15T11:00:00.000Z","Practical Strategies to Safeguard Your Java Applications","/assets/img/uploads/java-security-best-practices.png",[14,16,2753,2754,2755],"security","owasp","cybersecurity",{"type":21,"children":2757,"toc":3628},[2758,2766,2772,2777,2831,2836,2842,2847,2967,2972,2978,2983,3084,3090,3095,3218,3223,3229,3234,3259,3264,3270,3275,3291,3296,3302,3307,3389,3394,3400,3405,3524,3529,3535,3540,3573,3578,3582,3587,3591,3624],{"type":24,"tag":25,"props":2759,"children":2760},{},[2761],{"type":24,"tag":29,"props":2762,"children":2763},{},[2764],{"type":33,"value":2765},"Security is critical in software development. Java applications, like all software, face numerous potential vulnerabilities. In this article, we will explore essential security best practices, referencing the OWASP Top 10, and providing practical Java examples to help you protect your applications effectively.",{"type":24,"tag":36,"props":2767,"children":2769},{"id":2768},"understanding-owasp-top-10",[2770],{"type":33,"value":2771},"Understanding OWASP Top 10",{"type":24,"tag":29,"props":2773,"children":2774},{},[2775],{"type":33,"value":2776},"The OWASP Top 10 outlines the most critical web application security risks:",{"type":24,"tag":2778,"props":2779,"children":2780},"ol",{},[2781,2786,2791,2796,2801,2806,2811,2816,2821,2826],{"type":24,"tag":52,"props":2782,"children":2783},{},[2784],{"type":33,"value":2785},"Injection",{"type":24,"tag":52,"props":2787,"children":2788},{},[2789],{"type":33,"value":2790},"Broken Authentication",{"type":24,"tag":52,"props":2792,"children":2793},{},[2794],{"type":33,"value":2795},"Sensitive Data Exposure",{"type":24,"tag":52,"props":2797,"children":2798},{},[2799],{"type":33,"value":2800},"XML External Entities (XXE)",{"type":24,"tag":52,"props":2802,"children":2803},{},[2804],{"type":33,"value":2805},"Broken Access Control",{"type":24,"tag":52,"props":2807,"children":2808},{},[2809],{"type":33,"value":2810},"Security Misconfiguration",{"type":24,"tag":52,"props":2812,"children":2813},{},[2814],{"type":33,"value":2815},"Cross-Site Scripting (XSS)",{"type":24,"tag":52,"props":2817,"children":2818},{},[2819],{"type":33,"value":2820},"Insecure Deserialization",{"type":24,"tag":52,"props":2822,"children":2823},{},[2824],{"type":33,"value":2825},"Using Components with Known Vulnerabilities",{"type":24,"tag":52,"props":2827,"children":2828},{},[2829],{"type":33,"value":2830},"Insufficient Logging and Monitoring",{"type":24,"tag":29,"props":2832,"children":2833},{},[2834],{"type":33,"value":2835},"Let's delve into practical strategies and Java implementations for some of these threats.",{"type":24,"tag":36,"props":2837,"children":2839},{"id":2838},"_1-injection-prevention",[2840],{"type":33,"value":2841},"1. Injection Prevention",{"type":24,"tag":29,"props":2843,"children":2844},{},[2845],{"type":33,"value":2846},"Use Prepared Statements and parameterized queries:",{"type":24,"tag":151,"props":2848,"children":2850},{"className":153,"code":2849,"language":16,"meta":7,"style":7},"PreparedStatement stmt = connection.prepareStatement(\"SELECT * FROM users WHERE username=? AND password=?\");\nstmt.setString(1, username);\nstmt.setString(2, password);\nResultSet rs = stmt.executeQuery();\n",[2851],{"type":24,"tag":14,"props":2852,"children":2853},{"__ignoreMap":7},[2854,2889,2916,2941],{"type":24,"tag":160,"props":2855,"children":2856},{"class":162,"line":163},[2857,2862,2866,2871,2876,2880,2885],{"type":24,"tag":160,"props":2858,"children":2859},{"style":189},[2860],{"type":33,"value":2861},"PreparedStatement stmt ",{"type":24,"tag":160,"props":2863,"children":2864},{"style":167},[2865],{"type":33,"value":383},{"type":24,"tag":160,"props":2867,"children":2868},{"style":189},[2869],{"type":33,"value":2870}," connection.",{"type":24,"tag":160,"props":2872,"children":2873},{"style":173},[2874],{"type":33,"value":2875},"prepareStatement",{"type":24,"tag":160,"props":2877,"children":2878},{"style":189},[2879],{"type":33,"value":235},{"type":24,"tag":160,"props":2881,"children":2882},{"style":238},[2883],{"type":33,"value":2884},"\"SELECT * FROM users WHERE username=? AND password=?\"",{"type":24,"tag":160,"props":2886,"children":2887},{"style":189},[2888],{"type":33,"value":648},{"type":24,"tag":160,"props":2890,"children":2891},{"class":162,"line":195},[2892,2897,2902,2906,2911],{"type":24,"tag":160,"props":2893,"children":2894},{"style":189},[2895],{"type":33,"value":2896},"stmt.",{"type":24,"tag":160,"props":2898,"children":2899},{"style":173},[2900],{"type":33,"value":2901},"setString",{"type":24,"tag":160,"props":2903,"children":2904},{"style":189},[2905],{"type":33,"value":235},{"type":24,"tag":160,"props":2907,"children":2908},{"style":640},[2909],{"type":33,"value":2910},"1",{"type":24,"tag":160,"props":2912,"children":2913},{"style":189},[2914],{"type":33,"value":2915},", username);\n",{"type":24,"tag":160,"props":2917,"children":2918},{"class":162,"line":219},[2919,2923,2927,2931,2936],{"type":24,"tag":160,"props":2920,"children":2921},{"style":189},[2922],{"type":33,"value":2896},{"type":24,"tag":160,"props":2924,"children":2925},{"style":173},[2926],{"type":33,"value":2901},{"type":24,"tag":160,"props":2928,"children":2929},{"style":189},[2930],{"type":33,"value":235},{"type":24,"tag":160,"props":2932,"children":2933},{"style":640},[2934],{"type":33,"value":2935},"2",{"type":24,"tag":160,"props":2937,"children":2938},{"style":189},[2939],{"type":33,"value":2940},", password);\n",{"type":24,"tag":160,"props":2942,"children":2943},{"class":162,"line":274},[2944,2949,2953,2958,2963],{"type":24,"tag":160,"props":2945,"children":2946},{"style":189},[2947],{"type":33,"value":2948},"ResultSet rs ",{"type":24,"tag":160,"props":2950,"children":2951},{"style":167},[2952],{"type":33,"value":383},{"type":24,"tag":160,"props":2954,"children":2955},{"style":189},[2956],{"type":33,"value":2957}," stmt.",{"type":24,"tag":160,"props":2959,"children":2960},{"style":173},[2961],{"type":33,"value":2962},"executeQuery",{"type":24,"tag":160,"props":2964,"children":2965},{"style":189},[2966],{"type":33,"value":397},{"type":24,"tag":29,"props":2968,"children":2969},{},[2970],{"type":33,"value":2971},"This approach prevents SQL injection by ensuring that input data is treated as data, not executable code.",{"type":24,"tag":36,"props":2973,"children":2975},{"id":2974},"_2-secure-authentication",[2976],{"type":33,"value":2977},"2. Secure Authentication",{"type":24,"tag":29,"props":2979,"children":2980},{},[2981],{"type":33,"value":2982},"Implement robust authentication using secure password storage (bcrypt or Argon2):",{"type":24,"tag":151,"props":2984,"children":2986},{"className":153,"code":2985,"language":16,"meta":7,"style":7},"import org.springframework.security.crypto.bcrypt.BCrypt;\n\nString hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());\nif (BCrypt.checkpw(password, hashedPassword)) {\n    // authentication successful\n}\n",[2987],{"type":24,"tag":14,"props":2988,"children":2989},{"__ignoreMap":7},[2990,3002,3009,3045,3068,3077],{"type":24,"tag":160,"props":2991,"children":2992},{"class":162,"line":163},[2993,2997],{"type":24,"tag":160,"props":2994,"children":2995},{"style":167},[2996],{"type":33,"value":528},{"type":24,"tag":160,"props":2998,"children":2999},{"style":189},[3000],{"type":33,"value":3001}," org.springframework.security.crypto.bcrypt.BCrypt;\n",{"type":24,"tag":160,"props":3003,"children":3004},{"class":162,"line":195},[3005],{"type":24,"tag":160,"props":3006,"children":3007},{"emptyLinePlaceholder":296},[3008],{"type":33,"value":299},{"type":24,"tag":160,"props":3010,"children":3011},{"class":162,"line":219},[3012,3017,3021,3026,3031,3036,3041],{"type":24,"tag":160,"props":3013,"children":3014},{"style":189},[3015],{"type":33,"value":3016},"String hashedPassword ",{"type":24,"tag":160,"props":3018,"children":3019},{"style":167},[3020],{"type":33,"value":383},{"type":24,"tag":160,"props":3022,"children":3023},{"style":189},[3024],{"type":33,"value":3025}," BCrypt.",{"type":24,"tag":160,"props":3027,"children":3028},{"style":173},[3029],{"type":33,"value":3030},"hashpw",{"type":24,"tag":160,"props":3032,"children":3033},{"style":189},[3034],{"type":33,"value":3035},"(password, BCrypt.",{"type":24,"tag":160,"props":3037,"children":3038},{"style":173},[3039],{"type":33,"value":3040},"gensalt",{"type":24,"tag":160,"props":3042,"children":3043},{"style":189},[3044],{"type":33,"value":271},{"type":24,"tag":160,"props":3046,"children":3047},{"class":162,"line":274},[3048,3053,3058,3063],{"type":24,"tag":160,"props":3049,"children":3050},{"style":167},[3051],{"type":33,"value":3052},"if",{"type":24,"tag":160,"props":3054,"children":3055},{"style":189},[3056],{"type":33,"value":3057}," (BCrypt.",{"type":24,"tag":160,"props":3059,"children":3060},{"style":173},[3061],{"type":33,"value":3062},"checkpw",{"type":24,"tag":160,"props":3064,"children":3065},{"style":189},[3066],{"type":33,"value":3067},"(password, hashedPassword)) {\n",{"type":24,"tag":160,"props":3069,"children":3070},{"class":162,"line":283},[3071],{"type":24,"tag":160,"props":3072,"children":3074},{"style":3073},"--shiki-default:#6A737D;--shiki-light:#6A737D",[3075],{"type":33,"value":3076},"    // authentication successful\n",{"type":24,"tag":160,"props":3078,"children":3079},{"class":162,"line":292},[3080],{"type":24,"tag":160,"props":3081,"children":3082},{"style":189},[3083],{"type":33,"value":289},{"type":24,"tag":36,"props":3085,"children":3087},{"id":3086},"_3-sensitive-data-exposure",[3088],{"type":33,"value":3089},"3. Sensitive Data Exposure",{"type":24,"tag":29,"props":3091,"children":3092},{},[3093],{"type":33,"value":3094},"Use encryption for sensitive data storage:",{"type":24,"tag":151,"props":3096,"children":3098},{"className":153,"code":3097,"language":16,"meta":7,"style":7},"Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nSecretKey key = generateSecretKey();\ncipher.init(Cipher.ENCRYPT_MODE, key);\nbyte[] encryptedData = cipher.doFinal(plainData.getBytes(StandardCharsets.UTF_8));\n",[3099],{"type":24,"tag":14,"props":3100,"children":3101},{"__ignoreMap":7},[3102,3137,3158,3176],{"type":24,"tag":160,"props":3103,"children":3104},{"class":162,"line":163},[3105,3110,3114,3119,3124,3128,3133],{"type":24,"tag":160,"props":3106,"children":3107},{"style":189},[3108],{"type":33,"value":3109},"Cipher cipher ",{"type":24,"tag":160,"props":3111,"children":3112},{"style":167},[3113],{"type":33,"value":383},{"type":24,"tag":160,"props":3115,"children":3116},{"style":189},[3117],{"type":33,"value":3118}," Cipher.",{"type":24,"tag":160,"props":3120,"children":3121},{"style":173},[3122],{"type":33,"value":3123},"getInstance",{"type":24,"tag":160,"props":3125,"children":3126},{"style":189},[3127],{"type":33,"value":235},{"type":24,"tag":160,"props":3129,"children":3130},{"style":238},[3131],{"type":33,"value":3132},"\"AES/GCM/NoPadding\"",{"type":24,"tag":160,"props":3134,"children":3135},{"style":189},[3136],{"type":33,"value":648},{"type":24,"tag":160,"props":3138,"children":3139},{"class":162,"line":195},[3140,3145,3149,3154],{"type":24,"tag":160,"props":3141,"children":3142},{"style":189},[3143],{"type":33,"value":3144},"SecretKey key ",{"type":24,"tag":160,"props":3146,"children":3147},{"style":167},[3148],{"type":33,"value":383},{"type":24,"tag":160,"props":3150,"children":3151},{"style":173},[3152],{"type":33,"value":3153}," generateSecretKey",{"type":24,"tag":160,"props":3155,"children":3156},{"style":189},[3157],{"type":33,"value":397},{"type":24,"tag":160,"props":3159,"children":3160},{"class":162,"line":219},[3161,3166,3171],{"type":24,"tag":160,"props":3162,"children":3163},{"style":189},[3164],{"type":33,"value":3165},"cipher.",{"type":24,"tag":160,"props":3167,"children":3168},{"style":173},[3169],{"type":33,"value":3170},"init",{"type":24,"tag":160,"props":3172,"children":3173},{"style":189},[3174],{"type":33,"value":3175},"(Cipher.ENCRYPT_MODE, key);\n",{"type":24,"tag":160,"props":3177,"children":3178},{"class":162,"line":274},[3179,3184,3189,3193,3198,3203,3208,3213],{"type":24,"tag":160,"props":3180,"children":3181},{"style":167},[3182],{"type":33,"value":3183},"byte",{"type":24,"tag":160,"props":3185,"children":3186},{"style":189},[3187],{"type":33,"value":3188},"[] encryptedData ",{"type":24,"tag":160,"props":3190,"children":3191},{"style":167},[3192],{"type":33,"value":383},{"type":24,"tag":160,"props":3194,"children":3195},{"style":189},[3196],{"type":33,"value":3197}," cipher.",{"type":24,"tag":160,"props":3199,"children":3200},{"style":173},[3201],{"type":33,"value":3202},"doFinal",{"type":24,"tag":160,"props":3204,"children":3205},{"style":189},[3206],{"type":33,"value":3207},"(plainData.",{"type":24,"tag":160,"props":3209,"children":3210},{"style":173},[3211],{"type":33,"value":3212},"getBytes",{"type":24,"tag":160,"props":3214,"children":3215},{"style":189},[3216],{"type":33,"value":3217},"(StandardCharsets.UTF_8));\n",{"type":24,"tag":29,"props":3219,"children":3220},{},[3221],{"type":33,"value":3222},"Always encrypt data at rest and in transit (use HTTPS).",{"type":24,"tag":36,"props":3224,"children":3226},{"id":3225},"_4-security-misconfiguration",[3227],{"type":33,"value":3228},"4. Security Misconfiguration",{"type":24,"tag":29,"props":3230,"children":3231},{},[3232],{"type":33,"value":3233},"Adopt secure defaults, disable debugging, and avoid detailed error messages:",{"type":24,"tag":151,"props":3235,"children":3239},{"className":3236,"code":3237,"language":3238,"meta":7,"style":7},"language-properties shiki shiki-themes github-dark github-light","server.error.include-stacktrace=never\nserver.error.include-message=never\n","properties",[3240],{"type":24,"tag":14,"props":3241,"children":3242},{"__ignoreMap":7},[3243,3251],{"type":24,"tag":160,"props":3244,"children":3245},{"class":162,"line":163},[3246],{"type":24,"tag":160,"props":3247,"children":3248},{},[3249],{"type":33,"value":3250},"server.error.include-stacktrace=never\n",{"type":24,"tag":160,"props":3252,"children":3253},{"class":162,"line":195},[3254],{"type":24,"tag":160,"props":3255,"children":3256},{},[3257],{"type":33,"value":3258},"server.error.include-message=never\n",{"type":24,"tag":29,"props":3260,"children":3261},{},[3262],{"type":33,"value":3263},"Review and secure your Java application configuration regularly.",{"type":24,"tag":36,"props":3265,"children":3267},{"id":3266},"_5-cross-site-scripting-xss",[3268],{"type":33,"value":3269},"5. Cross-Site Scripting (XSS)",{"type":24,"tag":29,"props":3271,"children":3272},{},[3273],{"type":33,"value":3274},"Utilize proper encoding or frameworks that prevent XSS by default, such as Thymeleaf:",{"type":24,"tag":151,"props":3276,"children":3280},{"className":3277,"code":3278,"language":3279,"meta":7,"style":7},"language-html shiki shiki-themes github-dark github-light","\u003Cp th:text=\"${userInput}\">\u003C/p>\n","html",[3281],{"type":24,"tag":14,"props":3282,"children":3283},{"__ignoreMap":7},[3284],{"type":24,"tag":160,"props":3285,"children":3286},{"class":162,"line":163},[3287],{"type":24,"tag":160,"props":3288,"children":3289},{},[3290],{"type":33,"value":3278},{"type":24,"tag":29,"props":3292,"children":3293},{},[3294],{"type":33,"value":3295},"Avoid directly inserting user inputs into HTML without proper sanitization or encoding.",{"type":24,"tag":36,"props":3297,"children":3299},{"id":3298},"_6-insecure-deserialization",[3300],{"type":33,"value":3301},"6. Insecure Deserialization",{"type":24,"tag":29,"props":3303,"children":3304},{},[3305],{"type":33,"value":3306},"Limit deserialization to trusted sources only:",{"type":24,"tag":151,"props":3308,"children":3310},{"className":153,"code":3309,"language":16,"meta":7,"style":7},"ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"trusted_data.ser\"));\nMyObject obj = (MyObject) ois.readObject();\n",[3311],{"type":24,"tag":14,"props":3312,"children":3313},{"__ignoreMap":7},[3314,3363],{"type":24,"tag":160,"props":3315,"children":3316},{"class":162,"line":163},[3317,3322,3326,3330,3335,3339,3344,3349,3353,3358],{"type":24,"tag":160,"props":3318,"children":3319},{"style":189},[3320],{"type":33,"value":3321},"ObjectInputStream ois ",{"type":24,"tag":160,"props":3323,"children":3324},{"style":167},[3325],{"type":33,"value":383},{"type":24,"tag":160,"props":3327,"children":3328},{"style":167},[3329],{"type":33,"value":388},{"type":24,"tag":160,"props":3331,"children":3332},{"style":173},[3333],{"type":33,"value":3334}," ObjectInputStream",{"type":24,"tag":160,"props":3336,"children":3337},{"style":189},[3338],{"type":33,"value":235},{"type":24,"tag":160,"props":3340,"children":3341},{"style":167},[3342],{"type":33,"value":3343},"new",{"type":24,"tag":160,"props":3345,"children":3346},{"style":173},[3347],{"type":33,"value":3348}," FileInputStream",{"type":24,"tag":160,"props":3350,"children":3351},{"style":189},[3352],{"type":33,"value":235},{"type":24,"tag":160,"props":3354,"children":3355},{"style":238},[3356],{"type":33,"value":3357},"\"trusted_data.ser\"",{"type":24,"tag":160,"props":3359,"children":3360},{"style":189},[3361],{"type":33,"value":3362},"));\n",{"type":24,"tag":160,"props":3364,"children":3365},{"class":162,"line":195},[3366,3371,3375,3380,3385],{"type":24,"tag":160,"props":3367,"children":3368},{"style":189},[3369],{"type":33,"value":3370},"MyObject obj ",{"type":24,"tag":160,"props":3372,"children":3373},{"style":167},[3374],{"type":33,"value":383},{"type":24,"tag":160,"props":3376,"children":3377},{"style":189},[3378],{"type":33,"value":3379}," (MyObject) ois.",{"type":24,"tag":160,"props":3381,"children":3382},{"style":173},[3383],{"type":33,"value":3384},"readObject",{"type":24,"tag":160,"props":3386,"children":3387},{"style":189},[3388],{"type":33,"value":397},{"type":24,"tag":29,"props":3390,"children":3391},{},[3392],{"type":33,"value":3393},"Always validate deserialized data.",{"type":24,"tag":36,"props":3395,"children":3397},{"id":3396},"_7-component-vulnerabilities",[3398],{"type":33,"value":3399},"7. Component Vulnerabilities",{"type":24,"tag":29,"props":3401,"children":3402},{},[3403],{"type":33,"value":3404},"Regularly update libraries and dependencies, and use tools like OWASP Dependency-Check to identify vulnerabilities:",{"type":24,"tag":151,"props":3406,"children":3410},{"className":3407,"code":3408,"language":3409,"meta":7,"style":7},"language-xml shiki shiki-themes github-dark github-light","\u003Cplugin>\n    \u003CgroupId>org.owasp\u003C/groupId>\n    \u003CartifactId>dependency-check-maven\u003C/artifactId>\n    \u003Cversion>9.0.9\u003C/version>\n\u003C/plugin>\n","xml",[3411],{"type":24,"tag":14,"props":3412,"children":3413},{"__ignoreMap":7},[3414,3432,3458,3483,3508],{"type":24,"tag":160,"props":3415,"children":3416},{"class":162,"line":163},[3417,3421,3427],{"type":24,"tag":160,"props":3418,"children":3419},{"style":189},[3420],{"type":33,"value":690},{"type":24,"tag":160,"props":3422,"children":3424},{"style":3423},"--shiki-default:#85E89D;--shiki-light:#22863A",[3425],{"type":33,"value":3426},"plugin",{"type":24,"tag":160,"props":3428,"children":3429},{"style":189},[3430],{"type":33,"value":3431},">\n",{"type":24,"tag":160,"props":3433,"children":3434},{"class":162,"line":195},[3435,3440,3445,3450,3454],{"type":24,"tag":160,"props":3436,"children":3437},{"style":189},[3438],{"type":33,"value":3439},"    \u003C",{"type":24,"tag":160,"props":3441,"children":3442},{"style":3423},[3443],{"type":33,"value":3444},"groupId",{"type":24,"tag":160,"props":3446,"children":3447},{"style":189},[3448],{"type":33,"value":3449},">org.owasp\u003C/",{"type":24,"tag":160,"props":3451,"children":3452},{"style":3423},[3453],{"type":33,"value":3444},{"type":24,"tag":160,"props":3455,"children":3456},{"style":189},[3457],{"type":33,"value":3431},{"type":24,"tag":160,"props":3459,"children":3460},{"class":162,"line":219},[3461,3465,3470,3475,3479],{"type":24,"tag":160,"props":3462,"children":3463},{"style":189},[3464],{"type":33,"value":3439},{"type":24,"tag":160,"props":3466,"children":3467},{"style":3423},[3468],{"type":33,"value":3469},"artifactId",{"type":24,"tag":160,"props":3471,"children":3472},{"style":189},[3473],{"type":33,"value":3474},">dependency-check-maven\u003C/",{"type":24,"tag":160,"props":3476,"children":3477},{"style":3423},[3478],{"type":33,"value":3469},{"type":24,"tag":160,"props":3480,"children":3481},{"style":189},[3482],{"type":33,"value":3431},{"type":24,"tag":160,"props":3484,"children":3485},{"class":162,"line":274},[3486,3490,3495,3500,3504],{"type":24,"tag":160,"props":3487,"children":3488},{"style":189},[3489],{"type":33,"value":3439},{"type":24,"tag":160,"props":3491,"children":3492},{"style":3423},[3493],{"type":33,"value":3494},"version",{"type":24,"tag":160,"props":3496,"children":3497},{"style":189},[3498],{"type":33,"value":3499},">9.0.9\u003C/",{"type":24,"tag":160,"props":3501,"children":3502},{"style":3423},[3503],{"type":33,"value":3494},{"type":24,"tag":160,"props":3505,"children":3506},{"style":189},[3507],{"type":33,"value":3431},{"type":24,"tag":160,"props":3509,"children":3510},{"class":162,"line":283},[3511,3516,3520],{"type":24,"tag":160,"props":3512,"children":3513},{"style":189},[3514],{"type":33,"value":3515},"\u003C/",{"type":24,"tag":160,"props":3517,"children":3518},{"style":3423},[3519],{"type":33,"value":3426},{"type":24,"tag":160,"props":3521,"children":3522},{"style":189},[3523],{"type":33,"value":3431},{"type":24,"tag":29,"props":3525,"children":3526},{},[3527],{"type":33,"value":3528},"Execute dependency checks regularly in your CI/CD pipeline.",{"type":24,"tag":36,"props":3530,"children":3532},{"id":3531},"_8-logging-and-monitoring",[3533],{"type":33,"value":3534},"8. Logging and Monitoring",{"type":24,"tag":29,"props":3536,"children":3537},{},[3538],{"type":33,"value":3539},"Implement comprehensive logging and monitoring to detect and respond to security incidents:",{"type":24,"tag":151,"props":3541,"children":3543},{"className":153,"code":3542,"language":16,"meta":7,"style":7},"logger.warn(\"Failed login attempt for user: {}\", username);\n",[3544],{"type":24,"tag":14,"props":3545,"children":3546},{"__ignoreMap":7},[3547],{"type":24,"tag":160,"props":3548,"children":3549},{"class":162,"line":163},[3550,3555,3560,3564,3569],{"type":24,"tag":160,"props":3551,"children":3552},{"style":189},[3553],{"type":33,"value":3554},"logger.",{"type":24,"tag":160,"props":3556,"children":3557},{"style":173},[3558],{"type":33,"value":3559},"warn",{"type":24,"tag":160,"props":3561,"children":3562},{"style":189},[3563],{"type":33,"value":235},{"type":24,"tag":160,"props":3565,"children":3566},{"style":238},[3567],{"type":33,"value":3568},"\"Failed login attempt for user: {}\"",{"type":24,"tag":160,"props":3570,"children":3571},{"style":189},[3572],{"type":33,"value":2915},{"type":24,"tag":29,"props":3574,"children":3575},{},[3576],{"type":33,"value":3577},"Aggregate logs centrally, and set up alerts for suspicious activities.",{"type":24,"tag":36,"props":3579,"children":3580},{"id":1238},[3581],{"type":33,"value":1241},{"type":24,"tag":29,"props":3583,"children":3584},{},[3585],{"type":33,"value":3586},"Adopting these best practices can greatly enhance the security posture of your Java applications. Stay proactive by regularly reviewing security measures, updating dependencies, and monitoring your systems. Security is a continuous process, not a one-time setup.",{"type":24,"tag":2688,"props":3588,"children":3589},{"id":1254},[3590],{"type":33,"value":1257},{"type":24,"tag":48,"props":3592,"children":3593},{},[3594,3604,3614],{"type":24,"tag":52,"props":3595,"children":3596},{},[3597],{"type":24,"tag":1265,"props":3598,"children":3601},{"href":3599,"rel":3600},"https://owasp.org/www-project-top-ten/",[1269],[3602],{"type":33,"value":3603},"OWASP Top 10",{"type":24,"tag":52,"props":3605,"children":3606},{},[3607],{"type":24,"tag":1265,"props":3608,"children":3611},{"href":3609,"rel":3610},"https://spring.io/projects/spring-security",[1269],[3612],{"type":33,"value":3613},"Spring Security",{"type":24,"tag":52,"props":3615,"children":3616},{},[3617],{"type":24,"tag":1265,"props":3618,"children":3621},{"href":3619,"rel":3620},"https://owasp.org/www-project-dependency-check/",[1269],[3622],{"type":33,"value":3623},"OWASP Dependency Check",{"type":24,"tag":1308,"props":3625,"children":3626},{},[3627],{"type":33,"value":1312},{"title":7,"searchDepth":195,"depth":195,"links":3629},[3630,3631,3632,3633,3634,3635,3636,3637,3638,3639],{"id":2768,"depth":195,"text":2771},{"id":2838,"depth":195,"text":2841},{"id":2974,"depth":195,"text":2977},{"id":3086,"depth":195,"text":3089},{"id":3225,"depth":195,"text":3228},{"id":3266,"depth":195,"text":3269},{"id":3298,"depth":195,"text":3301},{"id":3396,"depth":195,"text":3399},{"id":3531,"depth":195,"text":3534},{"id":1238,"depth":195,"text":1241},"content:posts:java-security-best-practices.md","posts/java-security-best-practices.md","posts/java-security-best-practices",{"_path":3644,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":3645,"description":3646,"date":3647,"layout":11,"subtitle":3648,"image":3649,"optimized_image":3649,"category":14,"tags":3650,"author":19,"paginate":6,"body":3654,"_type":1330,"_id":4452,"_source":1332,"_file":4453,"_stem":4454,"_extension":1335},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","An expert guide to understanding and implementing circuit breakers in distributed systems, with practical Java examples using Resilience4j and best practices.","2024-07-15T11:00:00.000Z","Enhancing Resilience with Circuit Breaker Patterns","/assets/img/uploads/circuit-breaking-resilience.png",[14,16,3651,3652,3653],"resilience","circuit-breaker","distributed-systems",{"type":21,"children":3655,"toc":4440},[3656,3664,3670,3675,3708,3713,3719,3724,3742,3748,3754,3759,3869,3875,4079,4085,4090,4252,4258,4263,4356,4361,4367,4390,4394,4399,4403,4436],{"type":24,"tag":25,"props":3657,"children":3658},{},[3659],{"type":24,"tag":29,"props":3660,"children":3661},{},[3662],{"type":33,"value":3663},"In modern distributed systems, failures are inevitable. The circuit breaker pattern helps maintain system stability by preventing failures from cascading across service boundaries. In this post, we'll dive deep into circuit breakers, explore their importance, and demonstrate practical implementation using Resilience4j in Java.",{"type":24,"tag":36,"props":3665,"children":3667},{"id":3666},"understanding-the-circuit-breaker-pattern",[3668],{"type":33,"value":3669},"Understanding the Circuit Breaker Pattern",{"type":24,"tag":29,"props":3671,"children":3672},{},[3673],{"type":33,"value":3674},"The circuit breaker pattern prevents a service from making repeated calls to an external service that is experiencing issues. It has three main states:",{"type":24,"tag":48,"props":3676,"children":3677},{},[3678,3688,3698],{"type":24,"tag":52,"props":3679,"children":3680},{},[3681,3686],{"type":24,"tag":56,"props":3682,"children":3683},{},[3684],{"type":33,"value":3685},"Closed:",{"type":33,"value":3687}," All requests pass through normally.",{"type":24,"tag":52,"props":3689,"children":3690},{},[3691,3696],{"type":24,"tag":56,"props":3692,"children":3693},{},[3694],{"type":33,"value":3695},"Open:",{"type":33,"value":3697}," Requests fail fast, bypassing the problematic service.",{"type":24,"tag":52,"props":3699,"children":3700},{},[3701,3706],{"type":24,"tag":56,"props":3702,"children":3703},{},[3704],{"type":33,"value":3705},"Half-Open:",{"type":33,"value":3707}," A limited number of requests test if the external service has recovered.",{"type":24,"tag":29,"props":3709,"children":3710},{},[3711],{"type":33,"value":3712},"This mechanism helps improve the resilience and responsiveness of your systems.",{"type":24,"tag":36,"props":3714,"children":3716},{"id":3715},"when-to-use-circuit-breakers",[3717],{"type":33,"value":3718},"When to Use Circuit Breakers",{"type":24,"tag":29,"props":3720,"children":3721},{},[3722],{"type":33,"value":3723},"Circuit breakers are essential when:",{"type":24,"tag":48,"props":3725,"children":3726},{},[3727,3732,3737],{"type":24,"tag":52,"props":3728,"children":3729},{},[3730],{"type":33,"value":3731},"Your system relies on remote calls (microservices architecture, external APIs).",{"type":24,"tag":52,"props":3733,"children":3734},{},[3735],{"type":33,"value":3736},"You need to prevent failure propagation.",{"type":24,"tag":52,"props":3738,"children":3739},{},[3740],{"type":33,"value":3741},"You aim to improve user experience by quickly responding to failures.",{"type":24,"tag":36,"props":3743,"children":3745},{"id":3744},"implementing-circuit-breakers-with-resilience4j",[3746],{"type":33,"value":3747},"Implementing Circuit Breakers with Resilience4j",{"type":24,"tag":433,"props":3749,"children":3751},{"id":3750},"step-1-adding-dependencies",[3752],{"type":33,"value":3753},"Step 1: Adding Dependencies",{"type":24,"tag":29,"props":3755,"children":3756},{},[3757],{"type":33,"value":3758},"Add Resilience4j dependencies to your project (Maven example):",{"type":24,"tag":151,"props":3760,"children":3762},{"className":3407,"code":3761,"language":3409,"meta":7,"style":7},"\u003Cdependency>\n    \u003CgroupId>io.github.resilience4j\u003C/groupId>\n    \u003CartifactId>resilience4j-circuitbreaker\u003C/artifactId>\n    \u003Cversion>2.0.2\u003C/version>\n\u003C/dependency>\n",[3763],{"type":24,"tag":14,"props":3764,"children":3765},{"__ignoreMap":7},[3766,3782,3806,3830,3854],{"type":24,"tag":160,"props":3767,"children":3768},{"class":162,"line":163},[3769,3773,3778],{"type":24,"tag":160,"props":3770,"children":3771},{"style":189},[3772],{"type":33,"value":690},{"type":24,"tag":160,"props":3774,"children":3775},{"style":3423},[3776],{"type":33,"value":3777},"dependency",{"type":24,"tag":160,"props":3779,"children":3780},{"style":189},[3781],{"type":33,"value":3431},{"type":24,"tag":160,"props":3783,"children":3784},{"class":162,"line":195},[3785,3789,3793,3798,3802],{"type":24,"tag":160,"props":3786,"children":3787},{"style":189},[3788],{"type":33,"value":3439},{"type":24,"tag":160,"props":3790,"children":3791},{"style":3423},[3792],{"type":33,"value":3444},{"type":24,"tag":160,"props":3794,"children":3795},{"style":189},[3796],{"type":33,"value":3797},">io.github.resilience4j\u003C/",{"type":24,"tag":160,"props":3799,"children":3800},{"style":3423},[3801],{"type":33,"value":3444},{"type":24,"tag":160,"props":3803,"children":3804},{"style":189},[3805],{"type":33,"value":3431},{"type":24,"tag":160,"props":3807,"children":3808},{"class":162,"line":219},[3809,3813,3817,3822,3826],{"type":24,"tag":160,"props":3810,"children":3811},{"style":189},[3812],{"type":33,"value":3439},{"type":24,"tag":160,"props":3814,"children":3815},{"style":3423},[3816],{"type":33,"value":3469},{"type":24,"tag":160,"props":3818,"children":3819},{"style":189},[3820],{"type":33,"value":3821},">resilience4j-circuitbreaker\u003C/",{"type":24,"tag":160,"props":3823,"children":3824},{"style":3423},[3825],{"type":33,"value":3469},{"type":24,"tag":160,"props":3827,"children":3828},{"style":189},[3829],{"type":33,"value":3431},{"type":24,"tag":160,"props":3831,"children":3832},{"class":162,"line":274},[3833,3837,3841,3846,3850],{"type":24,"tag":160,"props":3834,"children":3835},{"style":189},[3836],{"type":33,"value":3439},{"type":24,"tag":160,"props":3838,"children":3839},{"style":3423},[3840],{"type":33,"value":3494},{"type":24,"tag":160,"props":3842,"children":3843},{"style":189},[3844],{"type":33,"value":3845},">2.0.2\u003C/",{"type":24,"tag":160,"props":3847,"children":3848},{"style":3423},[3849],{"type":33,"value":3494},{"type":24,"tag":160,"props":3851,"children":3852},{"style":189},[3853],{"type":33,"value":3431},{"type":24,"tag":160,"props":3855,"children":3856},{"class":162,"line":283},[3857,3861,3865],{"type":24,"tag":160,"props":3858,"children":3859},{"style":189},[3860],{"type":33,"value":3515},{"type":24,"tag":160,"props":3862,"children":3863},{"style":3423},[3864],{"type":33,"value":3777},{"type":24,"tag":160,"props":3866,"children":3867},{"style":189},[3868],{"type":33,"value":3431},{"type":24,"tag":433,"props":3870,"children":3872},{"id":3871},"step-2-configuring-circuit-breaker",[3873],{"type":33,"value":3874},"Step 2: Configuring Circuit Breaker",{"type":24,"tag":151,"props":3876,"children":3878},{"className":153,"code":3877,"language":16,"meta":7,"style":7},"CircuitBreakerConfig config = CircuitBreakerConfig.custom()\n    .failureRateThreshold(50)\n    .waitDurationInOpenState(Duration.ofMillis(1000))\n    .permittedNumberOfCallsInHalfOpenState(2)\n    .minimumNumberOfCalls(5)\n    .build();\n\nCircuitBreaker circuitBreaker = CircuitBreaker.of(\"myService\", config);\n",[3879],{"type":24,"tag":14,"props":3880,"children":3881},{"__ignoreMap":7},[3882,3909,3935,3971,3995,4020,4036,4043],{"type":24,"tag":160,"props":3883,"children":3884},{"class":162,"line":163},[3885,3890,3894,3899,3904],{"type":24,"tag":160,"props":3886,"children":3887},{"style":189},[3888],{"type":33,"value":3889},"CircuitBreakerConfig config ",{"type":24,"tag":160,"props":3891,"children":3892},{"style":167},[3893],{"type":33,"value":383},{"type":24,"tag":160,"props":3895,"children":3896},{"style":189},[3897],{"type":33,"value":3898}," CircuitBreakerConfig.",{"type":24,"tag":160,"props":3900,"children":3901},{"style":173},[3902],{"type":33,"value":3903},"custom",{"type":24,"tag":160,"props":3905,"children":3906},{"style":189},[3907],{"type":33,"value":3908},"()\n",{"type":24,"tag":160,"props":3910,"children":3911},{"class":162,"line":195},[3912,3917,3922,3926,3931],{"type":24,"tag":160,"props":3913,"children":3914},{"style":189},[3915],{"type":33,"value":3916},"    .",{"type":24,"tag":160,"props":3918,"children":3919},{"style":173},[3920],{"type":33,"value":3921},"failureRateThreshold",{"type":24,"tag":160,"props":3923,"children":3924},{"style":189},[3925],{"type":33,"value":235},{"type":24,"tag":160,"props":3927,"children":3928},{"style":640},[3929],{"type":33,"value":3930},"50",{"type":24,"tag":160,"props":3932,"children":3933},{"style":189},[3934],{"type":33,"value":1611},{"type":24,"tag":160,"props":3936,"children":3937},{"class":162,"line":219},[3938,3942,3947,3952,3957,3961,3966],{"type":24,"tag":160,"props":3939,"children":3940},{"style":189},[3941],{"type":33,"value":3916},{"type":24,"tag":160,"props":3943,"children":3944},{"style":173},[3945],{"type":33,"value":3946},"waitDurationInOpenState",{"type":24,"tag":160,"props":3948,"children":3949},{"style":189},[3950],{"type":33,"value":3951},"(Duration.",{"type":24,"tag":160,"props":3953,"children":3954},{"style":173},[3955],{"type":33,"value":3956},"ofMillis",{"type":24,"tag":160,"props":3958,"children":3959},{"style":189},[3960],{"type":33,"value":235},{"type":24,"tag":160,"props":3962,"children":3963},{"style":640},[3964],{"type":33,"value":3965},"1000",{"type":24,"tag":160,"props":3967,"children":3968},{"style":189},[3969],{"type":33,"value":3970},"))\n",{"type":24,"tag":160,"props":3972,"children":3973},{"class":162,"line":274},[3974,3978,3983,3987,3991],{"type":24,"tag":160,"props":3975,"children":3976},{"style":189},[3977],{"type":33,"value":3916},{"type":24,"tag":160,"props":3979,"children":3980},{"style":173},[3981],{"type":33,"value":3982},"permittedNumberOfCallsInHalfOpenState",{"type":24,"tag":160,"props":3984,"children":3985},{"style":189},[3986],{"type":33,"value":235},{"type":24,"tag":160,"props":3988,"children":3989},{"style":640},[3990],{"type":33,"value":2935},{"type":24,"tag":160,"props":3992,"children":3993},{"style":189},[3994],{"type":33,"value":1611},{"type":24,"tag":160,"props":3996,"children":3997},{"class":162,"line":283},[3998,4002,4007,4011,4016],{"type":24,"tag":160,"props":3999,"children":4000},{"style":189},[4001],{"type":33,"value":3916},{"type":24,"tag":160,"props":4003,"children":4004},{"style":173},[4005],{"type":33,"value":4006},"minimumNumberOfCalls",{"type":24,"tag":160,"props":4008,"children":4009},{"style":189},[4010],{"type":33,"value":235},{"type":24,"tag":160,"props":4012,"children":4013},{"style":640},[4014],{"type":33,"value":4015},"5",{"type":24,"tag":160,"props":4017,"children":4018},{"style":189},[4019],{"type":33,"value":1611},{"type":24,"tag":160,"props":4021,"children":4022},{"class":162,"line":292},[4023,4027,4032],{"type":24,"tag":160,"props":4024,"children":4025},{"style":189},[4026],{"type":33,"value":3916},{"type":24,"tag":160,"props":4028,"children":4029},{"style":173},[4030],{"type":33,"value":4031},"build",{"type":24,"tag":160,"props":4033,"children":4034},{"style":189},[4035],{"type":33,"value":397},{"type":24,"tag":160,"props":4037,"children":4038},{"class":162,"line":302},[4039],{"type":24,"tag":160,"props":4040,"children":4041},{"emptyLinePlaceholder":296},[4042],{"type":33,"value":299},{"type":24,"tag":160,"props":4044,"children":4045},{"class":162,"line":325},[4046,4051,4055,4060,4065,4069,4074],{"type":24,"tag":160,"props":4047,"children":4048},{"style":189},[4049],{"type":33,"value":4050},"CircuitBreaker circuitBreaker ",{"type":24,"tag":160,"props":4052,"children":4053},{"style":167},[4054],{"type":33,"value":383},{"type":24,"tag":160,"props":4056,"children":4057},{"style":189},[4058],{"type":33,"value":4059}," CircuitBreaker.",{"type":24,"tag":160,"props":4061,"children":4062},{"style":173},[4063],{"type":33,"value":4064},"of",{"type":24,"tag":160,"props":4066,"children":4067},{"style":189},[4068],{"type":33,"value":235},{"type":24,"tag":160,"props":4070,"children":4071},{"style":238},[4072],{"type":33,"value":4073},"\"myService\"",{"type":24,"tag":160,"props":4075,"children":4076},{"style":189},[4077],{"type":33,"value":4078},", config);\n",{"type":24,"tag":433,"props":4080,"children":4082},{"id":4081},"step-3-using-circuit-breaker",[4083],{"type":33,"value":4084},"Step 3: Using Circuit Breaker",{"type":24,"tag":29,"props":4086,"children":4087},{},[4088],{"type":33,"value":4089},"Wrap service calls using the circuit breaker:",{"type":24,"tag":151,"props":4091,"children":4093},{"className":153,"code":4092,"language":16,"meta":7,"style":7},"Supplier\u003CString> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> myService.remoteCall());\n\nTry.ofSupplier(decoratedSupplier)\n    .recover(throwable -> \"Fallback response\")\n    .onSuccess(result -> System.out.println(\"Response: \" + result));\n",[4094],{"type":24,"tag":14,"props":4095,"children":4096},{"__ignoreMap":7},[4097,4150,4157,4175,4205],{"type":24,"tag":160,"props":4098,"children":4099},{"class":162,"line":163},[4100,4105,4109,4114,4118,4122,4127,4132,4136,4141,4146],{"type":24,"tag":160,"props":4101,"children":4102},{"style":189},[4103],{"type":33,"value":4104},"Supplier\u003C",{"type":24,"tag":160,"props":4106,"children":4107},{"style":167},[4108],{"type":33,"value":353},{"type":24,"tag":160,"props":4110,"children":4111},{"style":189},[4112],{"type":33,"value":4113},"> decoratedSupplier ",{"type":24,"tag":160,"props":4115,"children":4116},{"style":167},[4117],{"type":33,"value":383},{"type":24,"tag":160,"props":4119,"children":4120},{"style":189},[4121],{"type":33,"value":4059},{"type":24,"tag":160,"props":4123,"children":4124},{"style":173},[4125],{"type":33,"value":4126},"decorateSupplier",{"type":24,"tag":160,"props":4128,"children":4129},{"style":189},[4130],{"type":33,"value":4131},"(circuitBreaker, () ",{"type":24,"tag":160,"props":4133,"children":4134},{"style":167},[4135],{"type":33,"value":732},{"type":24,"tag":160,"props":4137,"children":4138},{"style":189},[4139],{"type":33,"value":4140}," myService.",{"type":24,"tag":160,"props":4142,"children":4143},{"style":173},[4144],{"type":33,"value":4145},"remoteCall",{"type":24,"tag":160,"props":4147,"children":4148},{"style":189},[4149],{"type":33,"value":271},{"type":24,"tag":160,"props":4151,"children":4152},{"class":162,"line":195},[4153],{"type":24,"tag":160,"props":4154,"children":4155},{"emptyLinePlaceholder":296},[4156],{"type":33,"value":299},{"type":24,"tag":160,"props":4158,"children":4159},{"class":162,"line":219},[4160,4165,4170],{"type":24,"tag":160,"props":4161,"children":4162},{"style":189},[4163],{"type":33,"value":4164},"Try.",{"type":24,"tag":160,"props":4166,"children":4167},{"style":173},[4168],{"type":33,"value":4169},"ofSupplier",{"type":24,"tag":160,"props":4171,"children":4172},{"style":189},[4173],{"type":33,"value":4174},"(decoratedSupplier)\n",{"type":24,"tag":160,"props":4176,"children":4177},{"class":162,"line":274},[4178,4182,4187,4192,4196,4201],{"type":24,"tag":160,"props":4179,"children":4180},{"style":189},[4181],{"type":33,"value":3916},{"type":24,"tag":160,"props":4183,"children":4184},{"style":173},[4185],{"type":33,"value":4186},"recover",{"type":24,"tag":160,"props":4188,"children":4189},{"style":189},[4190],{"type":33,"value":4191},"(throwable ",{"type":24,"tag":160,"props":4193,"children":4194},{"style":167},[4195],{"type":33,"value":732},{"type":24,"tag":160,"props":4197,"children":4198},{"style":238},[4199],{"type":33,"value":4200}," \"Fallback response\"",{"type":24,"tag":160,"props":4202,"children":4203},{"style":189},[4204],{"type":33,"value":1611},{"type":24,"tag":160,"props":4206,"children":4207},{"class":162,"line":283},[4208,4212,4217,4222,4226,4230,4234,4238,4243,4247],{"type":24,"tag":160,"props":4209,"children":4210},{"style":189},[4211],{"type":33,"value":3916},{"type":24,"tag":160,"props":4213,"children":4214},{"style":173},[4215],{"type":33,"value":4216},"onSuccess",{"type":24,"tag":160,"props":4218,"children":4219},{"style":189},[4220],{"type":33,"value":4221},"(result ",{"type":24,"tag":160,"props":4223,"children":4224},{"style":167},[4225],{"type":33,"value":732},{"type":24,"tag":160,"props":4227,"children":4228},{"style":189},[4229],{"type":33,"value":737},{"type":24,"tag":160,"props":4231,"children":4232},{"style":173},[4233],{"type":33,"value":230},{"type":24,"tag":160,"props":4235,"children":4236},{"style":189},[4237],{"type":33,"value":235},{"type":24,"tag":160,"props":4239,"children":4240},{"style":238},[4241],{"type":33,"value":4242},"\"Response: \"",{"type":24,"tag":160,"props":4244,"children":4245},{"style":167},[4246],{"type":33,"value":246},{"type":24,"tag":160,"props":4248,"children":4249},{"style":189},[4250],{"type":33,"value":4251}," result));\n",{"type":24,"tag":36,"props":4253,"children":4255},{"id":4254},"monitoring-and-metrics",[4256],{"type":33,"value":4257},"Monitoring and Metrics",{"type":24,"tag":29,"props":4259,"children":4260},{},[4261],{"type":33,"value":4262},"Resilience4j provides built-in support for metrics and monitoring:",{"type":24,"tag":151,"props":4264,"children":4266},{"className":153,"code":4265,"language":16,"meta":7,"style":7},"CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();\nregistry.getEventPublisher()\n    .onStateTransition(event -> System.out.println(event.getStateTransition()));\n",[4267],{"type":24,"tag":14,"props":4268,"children":4269},{"__ignoreMap":7},[4270,4296,4313],{"type":24,"tag":160,"props":4271,"children":4272},{"class":162,"line":163},[4273,4278,4282,4287,4292],{"type":24,"tag":160,"props":4274,"children":4275},{"style":189},[4276],{"type":33,"value":4277},"CircuitBreakerRegistry registry ",{"type":24,"tag":160,"props":4279,"children":4280},{"style":167},[4281],{"type":33,"value":383},{"type":24,"tag":160,"props":4283,"children":4284},{"style":189},[4285],{"type":33,"value":4286}," CircuitBreakerRegistry.",{"type":24,"tag":160,"props":4288,"children":4289},{"style":173},[4290],{"type":33,"value":4291},"ofDefaults",{"type":24,"tag":160,"props":4293,"children":4294},{"style":189},[4295],{"type":33,"value":397},{"type":24,"tag":160,"props":4297,"children":4298},{"class":162,"line":195},[4299,4304,4309],{"type":24,"tag":160,"props":4300,"children":4301},{"style":189},[4302],{"type":33,"value":4303},"registry.",{"type":24,"tag":160,"props":4305,"children":4306},{"style":173},[4307],{"type":33,"value":4308},"getEventPublisher",{"type":24,"tag":160,"props":4310,"children":4311},{"style":189},[4312],{"type":33,"value":3908},{"type":24,"tag":160,"props":4314,"children":4315},{"class":162,"line":219},[4316,4320,4325,4330,4334,4338,4342,4347,4352],{"type":24,"tag":160,"props":4317,"children":4318},{"style":189},[4319],{"type":33,"value":3916},{"type":24,"tag":160,"props":4321,"children":4322},{"style":173},[4323],{"type":33,"value":4324},"onStateTransition",{"type":24,"tag":160,"props":4326,"children":4327},{"style":189},[4328],{"type":33,"value":4329},"(event ",{"type":24,"tag":160,"props":4331,"children":4332},{"style":167},[4333],{"type":33,"value":732},{"type":24,"tag":160,"props":4335,"children":4336},{"style":189},[4337],{"type":33,"value":737},{"type":24,"tag":160,"props":4339,"children":4340},{"style":173},[4341],{"type":33,"value":230},{"type":24,"tag":160,"props":4343,"children":4344},{"style":189},[4345],{"type":33,"value":4346},"(event.",{"type":24,"tag":160,"props":4348,"children":4349},{"style":173},[4350],{"type":33,"value":4351},"getStateTransition",{"type":24,"tag":160,"props":4353,"children":4354},{"style":189},[4355],{"type":33,"value":775},{"type":24,"tag":29,"props":4357,"children":4358},{},[4359],{"type":33,"value":4360},"Integrate with Prometheus, Grafana, or other observability tools for comprehensive monitoring.",{"type":24,"tag":36,"props":4362,"children":4364},{"id":4363},"best-practices-for-circuit-breaking",[4365],{"type":33,"value":4366},"Best Practices for Circuit Breaking",{"type":24,"tag":48,"props":4368,"children":4369},{},[4370,4375,4380,4385],{"type":24,"tag":52,"props":4371,"children":4372},{},[4373],{"type":33,"value":4374},"Configure sensible thresholds to avoid premature circuit opening.",{"type":24,"tag":52,"props":4376,"children":4377},{},[4378],{"type":33,"value":4379},"Always define fallback mechanisms.",{"type":24,"tag":52,"props":4381,"children":4382},{},[4383],{"type":33,"value":4384},"Regularly monitor circuit breaker states and metrics.",{"type":24,"tag":52,"props":4386,"children":4387},{},[4388],{"type":33,"value":4389},"Test thoroughly to ensure reliability under failure conditions.",{"type":24,"tag":36,"props":4391,"children":4392},{"id":1238},[4393],{"type":33,"value":1241},{"type":24,"tag":29,"props":4395,"children":4396},{},[4397],{"type":33,"value":4398},"Implementing circuit breakers using Resilience4j significantly enhances the resilience of your Java-based distributed systems. This proactive approach ensures your systems gracefully handle failures, maintaining service availability and user satisfaction.",{"type":24,"tag":2688,"props":4400,"children":4401},{"id":1254},[4402],{"type":33,"value":1257},{"type":24,"tag":48,"props":4404,"children":4405},{},[4406,4416,4426],{"type":24,"tag":52,"props":4407,"children":4408},{},[4409],{"type":24,"tag":1265,"props":4410,"children":4413},{"href":4411,"rel":4412},"https://resilience4j.readme.io/docs",[1269],[4414],{"type":33,"value":4415},"Resilience4j Official Documentation",{"type":24,"tag":52,"props":4417,"children":4418},{},[4419],{"type":24,"tag":1265,"props":4420,"children":4423},{"href":4421,"rel":4422},"https://martinfowler.com/bliki/CircuitBreaker.html",[1269],[4424],{"type":33,"value":4425},"Circuit Breaker Pattern by Martin Fowler",{"type":24,"tag":52,"props":4427,"children":4428},{},[4429],{"type":24,"tag":1265,"props":4430,"children":4433},{"href":4431,"rel":4432},"https://microservices.io/patterns/reliability/circuit-breaker.html",[1269],[4434],{"type":33,"value":4435},"Microservices Patterns by Chris Richardson",{"type":24,"tag":1308,"props":4437,"children":4438},{},[4439],{"type":33,"value":1312},{"title":7,"searchDepth":195,"depth":195,"links":4441},[4442,4443,4444,4449,4450,4451],{"id":3666,"depth":195,"text":3669},{"id":3715,"depth":195,"text":3718},{"id":3744,"depth":195,"text":3747,"children":4445},[4446,4447,4448],{"id":3750,"depth":219,"text":3753},{"id":3871,"depth":219,"text":3874},{"id":4081,"depth":219,"text":4084},{"id":4254,"depth":195,"text":4257},{"id":4363,"depth":195,"text":4366},{"id":1238,"depth":195,"text":1241},"content:posts:circuit-breaking-resilience.md","posts/circuit-breaking-resilience.md","posts/circuit-breaking-resilience",[4456,4457,4458,4459,4463,4467,4471,4472,4476,4480,4484,4488,4492,4496,4500,4504,4508,4512,4515,4519,4523],{"_path":1338,"title":1339,"date":1341},{"_path":2746,"title":2747,"date":2749},{"_path":3644,"title":3645,"date":3647},{"_path":4460,"title":4461,"date":4462},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":4464,"title":4465,"date":4466},"/posts/pattern-matching-sealed-classes-java21","A Guide to Pattern Matching and Sealed Classes in Java 21","2024-05-15T11:00:00.000Z",{"_path":4468,"title":4469,"date":4470},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":4,"title":8,"date":10},{"_path":4473,"title":4474,"date":4475},"/posts/microservices-mistakes","Common Mistakes When Developing Microservices","2024-02-15T11:00:00.000Z",{"_path":4477,"title":4478,"date":4479},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":4481,"title":4482,"date":4483},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":4485,"title":4486,"date":4487},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":4489,"title":4490,"date":4491},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":4493,"title":4494,"date":4495},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":4497,"title":4498,"date":4499},"/posts/python-virtual-environments","Python Virtual Environments (venv)","2023-09-17 22:00:00",{"_path":4501,"title":4502,"date":4503},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":4505,"title":4506,"date":4507},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":4509,"title":4510,"date":4511},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","2020-12-06T11:00:00.000Z",{"_path":4513,"title":4514,"date":4511},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot",{"_path":4516,"title":4517,"date":4518},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":4520,"title":4521,"date":4522},"/posts/algorithms-working-with-trees","Algorithms - Binary tree traversals in kotlin","2020-11-24T11:00:00.000Z",{"_path":4524,"title":4525,"date":4526},"/posts/playing-with-ocr-libraries","Playing with OCR libraries","2020-11-22T11:00:00.000Z",1790510065749]