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
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.
Comments are closed.