DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Trending

  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • Mastering Advanced Aggregations in Spark SQL
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs

Get Started With Reactive Programming With Kotlin on Quarkus

In this article, learn how Quarkus enables developers to keep using Kotlin programming APIs for reactive Java applications.

By 
Daniel Oh user avatar
Daniel Oh
DZone Core CORE ·
Mar. 24, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
3.2K Views

Join the DZone community and get the full member experience.

Join For Free

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.

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

Wrapping 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.

Watch the following demo video to showcase this tutorial step by step. Subscribe to bit.ly/danielohtv for learning cloud-native application development with Kubernetes.

Published at DZone with permission of . See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!