{"id":63098,"date":"2022-03-22T09:00:27","date_gmt":"2022-03-22T09:00:27","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=63098"},"modified":"2022-03-22T09:00:27","modified_gmt":"2022-03-22T09:00:27","slug":"get-started-with-reactive-programming-with-kotlin-on-quarkus","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=63098","title":{"rendered":"Get started with reactive programming with Kotlin on Quarkus"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Get started with reactive programming with Kotlin on Quarkus<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/daniel-oh\" class=\"username\">Daniel Oh<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Tue, 03\/22\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-69696\" class=\"rate-widget-thumbs-up\">\n<div class=\"rate-thumbs-up-btn-up rate-thumbs-up-btn-up vote-pending\">Up<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to like.<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Moving to the cloud with event-driven architecture raises big concerns for enterprises using multiple programming languages such as <a href=\"https:\/\/opensource.com\/resources\/java\" title=\"https:\/\/opensource.com\/resources\/java\">Java<\/a>, C#, <a href=\"https:\/\/opensource.com\/article\/17\/6\/functional-javascript\" title=\"https:\/\/opensource.com\/article\/17\/6\/functional-javascript\">JavaScript<\/a>, Scala, and <a href=\"https:\/\/opensource.com\/article\/20\/12\/groovy\" title=\"https:\/\/opensource.com\/article\/20\/12\/groovy\">Groovy<\/a> to implement business requirements. Because enterprises need to redesign multiple architectures for container deployment separately and put more effort into optimizing production on the cloud, developers often must learn a new programming language in line with the production environment. For example, Java developers have to switch their skill sets to Node.Js to develop lightweight event-front applications.<\/p>\n<p><a href=\"https:\/\/kotlinlang.org\/\" title=\"https:\/\/kotlinlang.org\/\">Kotlin<\/a> addresses these issues and targets various developers who deploy business applications with multiple programming languages on top of Java Virtual Machine (JVM). Kotlin handles these issues with both imperative and reactive approaches. However, there&#8217;s still a hustle to catch up on Kotlin&#8217;s new syntax and APIs, especially for Java developers. Luckily, the <a href=\"https:\/\/opensource.com\/article\/21\/8\/java-quarkus-ebook\" title=\"https:\/\/opensource.com\/article\/21\/8\/java-quarkus-ebook\">Quarkus<\/a> Kotlin extension makes it easier for developers to implement Kotlin applications.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">Explore the open source cloud<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/do092-developing-cloud-native-applications-microservices-architectures?intcmp=7013a000002gp8aAAA\">Free online course: Developing cloud-native applications with microservices arc\u2026<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/engage\/managed-cloud-services-s-202202210207?intcmp=7013a000002gp8aAAA\">eBook: Modernize your IT with managed cloud services<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/cloud.redhat.com\/products\/dedicated\/?intcmp=7013a000002gp8aAAA\">Try for 60 days: Red Hat OpenShift Dedicated<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/containers\/what-is-kubernetes?intcmp=7013a000002gp8aAAA\">What is Kubernetes?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/topics\/edge-computing?intcmp=7013a000002gp8aAAA\">Understanding edge computing<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/architect\/?intcmp=7013a000002gp8aAAA\">Latest articles for IT architects<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Create a new Kotlin project using Quarkus CLI<\/h2>\n<p>I&#8217;ll create a new maven project using the <a href=\"https:\/\/quarkus.io\/guides\/cli-tooling\" title=\"https:\/\/quarkus.io\/guides\/cli-tooling\">Quarkus command line<\/a> as an example. The following command adds Quarkus extensions to enable RESTEasy Reactive, Jackson, and Kotlin extensions:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>quarkus create app reactive-kotlin-example <span class=\"re5\">-x<\/span> kotlin,resteasy-reactive-jackson<\/code><\/span><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">...<br><span class=\"br0\">[<\/span>SUCCESS<span class=\"br0\">]<\/span> &#x2705; \u00a0quarkus project has been successfully generated in:<br>\n--<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">\/<\/span>Users<span class=\"sy0\">\/<\/span>danieloh<span class=\"sy0\">\/<\/span>Downloads<span class=\"sy0\">\/<\/span>demo<span class=\"sy0\">\/<\/span>reactive-kotlin-example<br>\n...<\/div><\/div><\/pre>\n<p>Next, I&#8217;ll use Quarkus Dev Mode, which enables <a href=\"https:\/\/quarkus.io\/vision\/developer-joy#live-coding\" title=\"https:\/\/quarkus.io\/vision\/developer-joy#live-coding\">live coding<\/a> to resolve the performance issue in the inner loop development. It simplifies the development workflow from writing code to accessing the endpoint or refreshing a web browser without recompiling and redeploying cycle. Run the following commands:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw3\">cd<\/span> reactive-kotlin-example<br>\n$ quarkus dev<\/div><\/div><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">...<br>\nINFO \u00a0<span class=\"br0\">[<\/span>io.quarkus<span class=\"br0\">]<\/span> <span class=\"br0\">(<\/span>Quarkus Main Thread<span class=\"br0\">)<\/span> Profile dev activated. Live Coding activated.<br>\nINFO \u00a0<span class=\"br0\">[<\/span>io.quarkus<span class=\"br0\">]<\/span> <span class=\"br0\">(<\/span>Quarkus Main Thread<span class=\"br0\">)<\/span> Installed features: <span class=\"br0\">[<\/span>cdi, kotlin, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx<span class=\"br0\">]<\/span><br><br><span class=\"re5\">--<\/span><br>\nTests paused<br>\nPress <span class=\"br0\">[<\/span>r<span class=\"br0\">]<\/span> to resume testing, <span class=\"br0\">[<\/span>o<span class=\"br0\">]<\/span> Toggle <span class=\"kw3\">test<\/span> output, <span class=\"br0\">[<\/span>:<span class=\"br0\">]<\/span> <span class=\"kw1\">for<\/span> the terminal, <span class=\"br0\">[<\/span>h<span class=\"br0\">]<\/span> <span class=\"kw1\">for<\/span> <span class=\"kw2\">more<\/span> options<span class=\"sy0\">&gt;<\/span><\/div><\/div><\/pre>\n<h2>Make Kotlin behave the Quarkus way<\/h2>\n<p>Kotiln provides a coroutine to run a block of code concurrently, similar to a thread in Java. The coroutine can be suspended in one thread then resume in another thread. Quarkus enables developers to compose suspending functions.<\/p>\n<p>Open the <code>ReactiveGreetingResource.kt<\/code> file in <code>src\/main\/kotlin\/org\/acme<\/code> directory to replace the <strong>hello()<\/strong> method with the following code:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"text geshifilter-text\">@GET<br>\n@Produces(MediaType.TEXT_PLAIN)<br>\nsuspend fun hello() = \"Hello RESTEasy Reactive by Kotlin Suspend function\"<\/div><\/div><\/pre>\n<p>Note: This resource file is generated automatically while you create a new Kotiln project using the Quarkus CLI.<\/p>\n<p>Make sure to access the RESTful API (<code>\/hello<\/code>) if the new suspend function works in the Quarkus development environment. Execute the following <code>curl<\/code> command line in your local terminal, or you can also access the endpoint URL using a web browser:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"sy0\">&amp;<\/span> curl localhost:<span class=\"nu0\">8080<\/span><span class=\"sy0\">\/<\/span>hello<\/code><\/span><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\">Hello RESTEasy Reactive by Kotlin Suspend <span class=\"kw1\">function<\/span><\/code><\/span><\/pre>\n<p>Great! It works well. Now I&#8217;ll enable Java&#8217;s Context and Dependency Injection (CDI) capability in the Kotlin application.<\/p>\n<h2>Enable CDI injection in the Kotlin application<\/h2>\n<p>Reflections and annotations in Kotlin are different from how Java initializes properties. It probably causes developers to have an issue (e.g., <strong>UninitializedPropertyAccessException<\/strong>). Before enabling CDI injection in code, create a new\u00a0<code>GreetingService.kt<\/code>\u00a0service file in the <code>src\/main\/kotlin\/org\/acme<\/code> directory:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"text geshifilter-text\">@ApplicationScoped<br>\nclass GreetingService {<br><br>\n\u00a0 \u00a0 fun greeting(name: String): String {<br>\n\u00a0 \u00a0 \u00a0 \u00a0 return \"Welcome Kotlin in Quarkus, $name\"<br>\n\u00a0 \u00a0 }<br><br>\n}<\/div><\/div><\/pre>\n<p>Go back to the <code>ReactiveGreetingResource.kt<\/code> file. Add the following code to use <strong>@Inject<\/strong> annotation for adopting Kotlin annotation and reflection by <strong>@field: Default<\/strong>:<\/p>\n<pre>\n<div class=\"geshifilter\"><div class=\"text geshifilter-text\">@Inject<br>\n@field: Default<br>\nlateinit var service: GreetingService<br><br>\n@GET<br>\n@Produces(MediaType.TEXT_PLAIN)<br>\n@Path(\"\/{name}\")<br>\nfun greeting(@PathParam(\"name\") name: String): String {<br>\n\u00a0 \u00a0 return service.greeting(name)<br>\n}<\/div><\/div><\/pre>\n<p>Access the new endpoint (<strong>\/hello\/{name}<\/strong>) if the CDI injection works. Execute the following <code>curl<\/code> command in the local terminal, or access the endpoint URL using a web browser:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"sy0\">&amp;<\/span> curl localhost:<span class=\"nu0\">8080<\/span><span class=\"sy0\">\/<\/span>hello<span class=\"sy0\">\/<\/span>Daniel<\/code><\/span><\/pre>\n<p>The output should look like this:<\/p>\n<pre>\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\">Welcome Kotlin <span class=\"kw1\">in<\/span> Quarkus, Daniel<\/code><\/span><\/pre>\n<h2>Wrap up<\/h2>\n<p>You learned how Quarkus enables developers to keep using Kotlin programming APIs for reactive Java applications. Developers benefit from features such as <a href=\"https:\/\/quarkus.io\/guides\/dev-services\" title=\"https:\/\/quarkus.io\/guides\/dev-services\">dev services<\/a> and <a href=\"https:\/\/quarkus.io\/vision\/developer-joy#live-coding\" title=\"https:\/\/quarkus.io\/vision\/developer-joy#live-coding\">live coding<\/a>. They also increase the performance for the cloud environment deployment through a <a href=\"https:\/\/quarkus.io\/guides\/building-native-image\" title=\"https:\/\/quarkus.io\/guides\/building-native-image\">native executable<\/a>. In another article, I&#8217;ll show how to develop data transaction features using Kotlin with Quarkus.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Learn how Quarkus enables developers to keep using Kotlin programming APIs for reactive Java applications.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/cloud-windows-building-containers.png\" width=\"520\" height=\"292\" alt=\"clouds in windows\" title=\"clouds in windows\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Opensource.com. CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/java\" hreflang=\"en\">Java<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/cloud\" hreflang=\"en\">Cloud<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/programming\" hreflang=\"en\">Programming<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/containers\" hreflang=\"en\">Containers<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/03\/cc-by-sa-4-6.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get started with reactive programming with Kotlin on Quarkus Daniel Oh Tue, 03\/22\/2022 &#8211; 03:00 Up Register or Login to like. Moving to the cloud with event-driven architecture raises big concerns for enterprises using multiple programming languages such as Java, C#, JavaScript, Scala, and Groovy to implement business requirements. Because enterprises need to redesign multiple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":63099,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-63098","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=63098"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/63098\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/63099"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=63098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=63098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=63098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}