• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

Get started with reactive programming with Kotlin on Quarkus

Get started with reactive programming with Kotlin on Quarkus: Get started with reactive programming with Kotlin on Quarkus Daniel Oh Tue, 03/22/2022 – 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 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.

Kotlin 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’s still a hustle to catch up on Kotlin’s new syntax and APIs, especially for Java developers. Luckily, the Quarkus Kotlin extension makes it easier for developers to implement Kotlin applications.

Explore the open source cloud
Free online course: Developing cloud-native applications with microservices arc…
eBook: Modernize your IT with managed cloud services
Try for 60 days: Red Hat OpenShift Dedicated
What is Kubernetes?
Understanding edge computing
Latest articles for IT architects

Create a new Kotlin project using Quarkus CLI

I’ll create a new maven project using the Quarkus command line as an example. The following command adds Quarkus extensions to enable RESTEasy Reactive, Jackson, and Kotlin extensions:

 $   quarkus create app reactive-kotlin-example -x kotlin,resteasy-reactive-jackson

The output should look like this:

 
...
[SUCCESS] ?  quarkus project has been successfully generated in:
--> /Users/danieloh/Downloads/demo/reactive-kotlin-example
...

Next, I’ll use Quarkus Dev Mode, which enables live coding 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:

 
$ cd reactive-kotlin-example
$ quarkus dev

The output should look like this:

 
...
INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, kotlin, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx]

--
Tests paused
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>

Make Kotlin behave the Quarkus way

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.

Open the ReactiveGreetingResource.kt file in src/main/kotlin/org/acme directory to replace the hello() method with the following code:

 
@GET
@Produces(MediaType.TEXT_PLAIN)
suspend fun hello() = "Hello RESTEasy Reactive by Kotlin Suspend function"

Note: This resource file is generated automatically while you create a new Kotiln project using the Quarkus CLI.

Make sure to access the RESTful API (/hello) if the new suspend function works in the Quarkus development environment. Execute the following curl command line in your local terminal, or you can also access the endpoint URL using a web browser:

 & curl localhost:8080/hello

The output should look like this:

 Hello RESTEasy Reactive by Kotlin Suspend function

Great! It works well. Now I’ll enable Java’s Context and Dependency Injection (CDI) capability in the Kotlin application.

Enable CDI injection in the Kotlin application

Reflections and annotations in Kotlin are different from how Java initializes properties. It probably causes developers to have an issue (e.g., UninitializedPropertyAccessException). Before enabling CDI injection in code, create a new GreetingService.kt service file in the src/main/kotlin/org/acme directory:

 
@ApplicationScoped
class GreetingService {

    fun greeting(name: String): String {
        return "Welcome Kotlin in Quarkus, $ name"
    }

}

Go back to the ReactiveGreetingResource.kt file. Add the following code to use @Inject annotation for adopting Kotlin annotation and reflection by @field: Default:

 
@Inject
@field: Default
lateinit var service: GreetingService

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{name}")
fun greeting(@PathParam("name") name: String): String {
    return service.greeting(name)
}

Access the new endpoint (/hello/{name}) if the CDI injection works. Execute the following curl command in the local terminal, or access the endpoint URL using a web browser:

 & curl localhost:8080/hello/Daniel

The output should look like this:

 Welcome Kotlin in Quarkus, Daniel

Wrap up

You learned how Quarkus enables developers to keep using Kotlin programming APIs for reactive Java applications. Developers benefit from features such as dev services and live coding. They also increase the performance for the cloud environment deployment through a native executable. In another article, I’ll show how to develop data transaction features using Kotlin with Quarkus.

Learn how Quarkus enables developers to keep using Kotlin programming APIs for reactive Java applications.

clouds in windows
Image by:

Opensource.com. CC BY-SA 4.0

Java
Cloud
Programming
Containers

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Daniel Oh

Supporting Open Source.

Have you tried: Diving with Sharks?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 22 March 2022 by Alan Category: SoftwareTag: Kotlin, Programming, Quarkus, reactive, Started

Revealed: true cost of Britain’s addiction to factory-farmed chicken

Revealed: true cost of Britain’s addiction to factory-farmed chicken: The first time I …

Read moreRevealed: true cost of Britain’s addiction to factory-farmed chicken

Lodge Suppliers: Backbone of the Hospitality Industry

In the hospitality industry, where guest comfort and satisfaction are paramount, having the right …

Read moreLodge Suppliers: Backbone of the Hospitality Industry

Teach Yourself Afrikaans

[wpramazon asin=”B0081RL2K6″ keyword=”Afrikaans”] Related Afrikaans Products …

Read moreTeach Yourself Afrikaans

About Alan

Previous Post:Nice Sal.pr: sal! Por las mesas photo
Next Post:Free online event: what we’ve learned two years into COVID, and how to deal with the fallout

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top